Skip to content

Commit

Permalink
feat(atrai): loading animation
Browse files Browse the repository at this point in the history
  • Loading branch information
felixerdy committed May 31, 2024
1 parent f7d4f40 commit ae39442
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 16 deletions.
96 changes: 81 additions & 15 deletions senseBox-bike-atrai/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,39 @@ Adafruit_SSD1306 SBDisplay::display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RES
QRCode SBDisplay::qrcode;
Adafruit_MAX17048 SBDisplay::maxlipo;

TaskHandle_t xBicycleAnimationTaskHandle;
bool isBicycleAnimationShowing = false;
String loadingMessage;
float loadingProgress;

void SBDisplay::bicycleAnimationTask(void *pvParameter) {
int dsplW = 128;
int dsplH = 64;
int prgsW = 120;
int prgsH = 2;

while (1) {
for (int i = 0; i < 36; i++) {
display.clearDisplay();
display.drawBitmap(32, -10, bicycle_loading_bitmap[i], 64, 64, 1); //this displays each frame hex value
drawProgressbar(4, (dsplH - 12) - prgsH - 8, prgsW, prgsH, loadingProgress * 100);
display.setCursor(4, dsplH - 12);
display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
display.println(loadingMessage);
drawBattery(0, 0, 16, 4);
display.display();
delay(100);

if (!isBicycleAnimationShowing) {
vTaskDelete(NULL);
}
}

vTaskDelay(0);
}
}

void SBDisplay::begin() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3D);
display.setRotation(2);
Expand All @@ -22,49 +55,81 @@ void SBDisplay::drawProgressbar(int x, int y, int width, int height, int progres
progress = progress > 100 ? 100 : progress; // set the progress value to 100
progress = progress < 0 ? 0 : progress; // start the counting to 0-100
float bar = ((float)(width - 1) / 100) * progress;
display.drawRect(x, y, width, height, WHITE);
display.fillRect(x + 2, y + 2, bar, height - 4, WHITE); // initailize the graphics fillRect(int x, int y, int width, int height)
display.drawRect(x, y, width, height + 4, WHITE);
display.fillRect(x + 2, y + 2, bar - 2, height, WHITE); // initailize the graphics fillRect(int x, int y, int width, int height)
}

void SBDisplay::drawBattery(int x, int y, int width, int height) {
drawProgressbar(x, y, width, height, maxlipo.cellPercent());
display.fillRect(x + width, y + 2, 2, height, WHITE);
if (maxlipo.chargeRate() > 0) {
display.setCursor(x + width + 4, y + 1);
display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
display.println("+");
display.setCursor(0, 0);
}
}



void SBDisplay::showLoading(String msg, float val) {
int dsplW = 128;
int dsplH = 64;
int prgsW = 120;
int prgsH = 8;
display.clearDisplay();
drawProgressbar(4, (dsplH - 12) - prgsH - 4, prgsW, prgsH, val * 100);
display.setCursor(4, dsplH - 12);
display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
display.println(msg);
display.display();
if (!isBicycleAnimationShowing) {
isBicycleAnimationShowing = true;
xTaskCreate(&bicycleAnimationTask, "bicycle_animation_task", 1536, (void *)NULL, 1, &xBicycleAnimationTaskHandle);
}
loadingMessage = msg;
loadingProgress = val;
// int dsplW = 128;
// int dsplH = 64;
// int prgsW = 120;
// int prgsH = 8;
// display.clearDisplay();
// drawProgressbar(4, (dsplH - 12) - prgsH - 4, prgsW, prgsH, val * 100);
// display.setCursor(4, dsplH - 12);
// display.setTextSize(1);
// display.setTextColor(WHITE, BLACK);
// display.println(msg);
// display.display();
}

void SBDisplay::showSystemStatus() {
if (isBicycleAnimationShowing) {
isBicycleAnimationShowing = false;
}

display.clearDisplay();


// display.clearDisplay();
display.setCursor(0, 0);
display.println("");
display.setTextSize(1);
display.setTextColor(WHITE, BLACK);

display.println(F("Batt Percent: "));
display.setTextSize(2);
display.setTextSize(1);
display.print(maxlipo.cellPercent(), 1);
display.println(" %");

display.println("");

display.setTextSize(1);
display.println(F("(Dis)Charge rate : "));
display.setTextSize(2);
display.setTextSize(1);
display.print(maxlipo.chargeRate(), 1);
display.println(" %/hr");

drawBattery(0, 0, 16, 4);

display.display();
}

void SBDisplay::drawQrCode(const char *qrStr, const char *lines[]) {
if (isBicycleAnimationShowing) {
isBicycleAnimationShowing = false;
}

display.clearDisplay();
display.setTextSize(1);

Expand Down Expand Up @@ -98,5 +163,6 @@ void SBDisplay::drawQrCode(const char *qrStr, const char *lines[]) {
display.setCursor(cursor_start_x, cursor_start_y + font_height * i);
display.println(lines[i]);
}
drawBattery(0, 0, 16, 4);
display.display();
}
7 changes: 7 additions & 0 deletions senseBox-bike-atrai/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#include <QRCodeGenerator.h>
#include <Adafruit_MAX1704X.h>

#include <Arduino.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "bicycle_loading_bitmap.h"

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Expand All @@ -19,6 +24,8 @@ class SBDisplay {
static void drawQrCode(const char *qrStr, const char *lines[]);
static void showLoading(String msg, float val);
static void drawProgressbar(int x, int y, int width, int height, int progress);
static void drawBattery(int x, int y, int width, int height);
static void bicycleAnimationTask(void *pvParams);

private:
static Adafruit_SSD1306 display;
Expand Down
3 changes: 2 additions & 1 deletion senseBox-bike-atrai/senseBox-bike-atrai.ino
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void setMeasurements() {
pm4 = m.mc_4p0;
pm1 = m.mc_1p0;

distance = getVl53l8cxMin() / 10; // mm in cm
distance = getVl53l8cxMin() / 10; // mm in cm
}

// starts bluetooth and sets the name according to the Bluetooth Bee
Expand Down Expand Up @@ -149,6 +149,7 @@ void setup() {
Serial.println("Bluetooth done!");
delay(500);
SBDisplay::showLoading("Start measurements...", 1);
delay(500);
}

void loop() {
Expand Down

0 comments on commit ae39442

Please sign in to comment.