]> code.bitgloo.com Git - clyne/smartwatch.git/commitdiff
dynamic ui, widgets
authorClyne Sullivan <tullivan99@gmail.com>
Mon, 25 Mar 2019 20:16:18 +0000 (16:16 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Mon, 25 Mar 2019 20:16:18 +0000 (16:16 -0400)
Makefile
source/controller.cpp
source/rtc.cpp
source/rtc.hpp
source/sharp.cpp
source/sharp.hpp
source/widget.cpp [new file with mode: 0644]
source/widget.hpp [new file with mode: 0644]

index ea44debdc57659a2b0d447f36e3eed8056786371..60a74902d62bef460070cfa35db50e71d32c5394 100755 (executable)
--- a/Makefile
+++ b/Makefile
@@ -116,6 +116,10 @@ $(OUTDFU): $(OUTHEX)
        @$(NRFUTIL) dfu genpkg --dev-type 0x0052 --application $(OUTHEX) \
                $(OUTDFU)
 
+cleancpp:
+       @echo "  CLEAN"
+       @rm -f $(OBJ) $(OUTELF) $(OUTHEX) $(OUTDFU)
+
 clean:
        @echo "  CLEAN"
        @rm -f $(OBJ) $(OUTELF) $(OUTHEX) $(OUTDFU)
index a364c9d65e58d1374d75ce341436c6f7f40241f6..d9ac9611f22a5dca3180dffc454a33e2bb452b10 100755 (executable)
@@ -2,8 +2,9 @@
 
 #include <bluefruit.h>
 
-#include "sharp.hpp"
 #include "rtc.hpp"
+#include "sharp.hpp"
+#include "widget.hpp"
 
 BLEUart bleuart;
 
@@ -45,7 +46,8 @@ void setup(void)
        Bluefruit.Advertising.start(0);                // 0 = Don't stop advertising after n seconds  
 
        Serial.println(F("Ready."));
-       Sharp::setScreen(RTC::showTime);
+       Sharp::addWidget<TimeWidget>();
+       Sharp::addWidget<NotificationWidget>("Welcome to smartwatch");
 }
 
 void loop(void)
@@ -59,18 +61,17 @@ void loop(void)
 void handlePacket(void)
 {
        char buf[64];
-       char *p = buf;
-       do {
-               *p++ = bleuart.read();
-       } while (bleuart.available());
-       *p = '\0';
+       unsigned int i;
+       for (i = 0; bleuart.available() && i < 63; i++)
+               buf[i] = bleuart.read();
+       buf[i] = '\0';
 
        switch (buf[0]) {
        case 'L':
-               RTC::setMessage(buf + 1);
+               //RTC::setMessage(buf + 1);
                break;
        case 'T':
-               Serial.println("Setting time!");
+               Sharp::addWidget<NotificationWidget>("Time updated");
                RTC::setTicks(std::atoi(buf + 1) * 60);
                break;
        default:
index ddb68c0528631136a6e24cb0952f9c09f2732f14..c355c90409fb7f96c412c23e83f198f31ba21d1b 100644 (file)
@@ -4,8 +4,6 @@
 nrf_drv_rtc_t RTC::rtc = NRF_DRV_RTC_INSTANCE(2);
 unsigned int RTC::rtcCount = 0;
 
-char RTC::message[16] = "";
-
 void RTC::begin(void)
 {
        //Initialize RTC instance
@@ -21,22 +19,6 @@ void RTC::begin(void)
        nrf_drv_rtc_enable(&rtc);
 }
 
-void RTC::showTime(Display& display)
-{
-       static unsigned int oldTicks = 0;
-       if (auto t = rtcCount; t != oldTicks) {
-               oldTicks = t;
-
-               display.setCursor(0, 10);
-               display.printf("%2d:%02d:%02d", t / 3600, (t % 3600) / 60,
-                       t % 60);
-               if (*message != '\0') {
-                       display.setCursor(0, 100);
-                       display.printf("%-16s", message);
-               }
-       }
-}
-
 void RTC::handler([[maybe_unused]] nrf_drv_rtc_int_type_t int_type)
 {
   static unsigned char counter = 0;
index 12dade9d8bb40daa7a5f25f44815919da7e9d6a8..09081a0e05d8e47351967ac85fd641631060b5d3 100644 (file)
@@ -1,3 +1,6 @@
+#ifndef RTC_HPP_
+#define RTC_HPP_
+
 #include <rtc/nrf_drv_rtc.h>
 #include "sharp.hpp"
 
@@ -6,8 +9,6 @@ private:
        static nrf_drv_rtc_t rtc;
        static unsigned int rtcCount;
 
-       static char message[16];
-
 public:
        static void begin(void);
 
@@ -19,13 +20,9 @@ public:
                rtcCount = t;
        }
 
-       static void showTime(Display& display);
-
-       inline static void setMessage(const char *s) {
-               strncpy(message, s, 16);
-       }
-
 private:
        static void handler(nrf_drv_rtc_int_type_t type);
 };
 
+#endif // RTC_HPP_
+
index e780ae7b5cc4753a941c0556050802634fc012c7..8fa22f981962833f1fb40fe84442aa01d94414c6 100644 (file)
@@ -1,20 +1,21 @@
 #include "sharp.hpp"
 #include "rtc.hpp"
 
+constexpr unsigned int SHARP_WIDTH = 144;
+constexpr unsigned int SHARP_HEIGHT = 168;
+
 constexpr unsigned int SHARP_SCK = 12;
 constexpr unsigned int SHARP_MOSI = 13;
 constexpr unsigned int SHARP_SS = 14;
 
-Adafruit_SharpMem Sharp::display(SHARP_SCK, SHARP_MOSI, SHARP_SS, 144, 168);
+Adafruit_SharpMem Sharp::display(SHARP_SCK, SHARP_MOSI, SHARP_SS, SHARP_WIDTH,
+       SHARP_HEIGHT);
 TaskHandle_t Sharp::taskHandle;
-bool Sharp::holdRendering = false;
-RenderFunc Sharp::currentScreen;
-
-#define BLACK 0
-#define WHITE 1
+std::vector<Widget *> Sharp::widgets;
 
 void Sharp::begin(void)
 {
+       widgets.reserve(10);
        display.begin();
        display.clearDisplay();
        display.setTextSize(3);
@@ -26,15 +27,19 @@ void Sharp::begin(void)
 
 void Sharp::updateTask([[maybe_unused]] void *arg)
 {
-       static auto old = RTC::ticks();
        while (1) {
-               do {
-                       delay(300);
-               } while (holdRendering);
+               unsigned int y = 0;
+               for (auto& w : widgets) {
+                       w->render(display, y);
+                       y += w->getHeight();
+                       display.drawFastHLine(0, y + 1, SHARP_WIDTH, BLACK);
+                       y += 3;
+                       if (y >= SHARP_HEIGHT)
+                               break;
+               }
 
-               if (currentScreen)
-                       currentScreen(display);
                display.refresh();
+               delay(300);
        }
 }
 
index a122e5d212fa5319b19ed2f5c11a0203824cae1f..277571189fdc73df942b074de95f5a1db8d0fb9d 100644 (file)
@@ -2,33 +2,25 @@
 #define SHARP_HPP_
 
 #include "sharp/Adafruit_SharpMem.h"
+#include "widget.hpp"
 
-#include <functional>
+#include <vector>
 
-using RenderFunc = std::function<void(Adafruit_GFX&)>;
-using Display = Adafruit_GFX;
+#define BLACK 0
+#define WHITE 1
 
 class Sharp {
 private:
        static Adafruit_SharpMem display;
        static TaskHandle_t taskHandle;
-       static bool holdRendering;
-
-       static RenderFunc currentScreen;
 
+       static std::vector<Widget *> widgets;
 public:
        static void begin(void);
 
-       inline static void pause(void) {
-               holdRendering = true;
-       }
-
-       inline static void unpause(void) {
-               holdRendering = false;
-       }
-
-       inline static void setScreen(const RenderFunc& rf) {
-               currentScreen = rf;
+       template<class T, typename... Args>
+       inline static void addWidget(Args... args) {
+               widgets.emplace_back(new T(args...));
        }
 
 private:
diff --git a/source/widget.cpp b/source/widget.cpp
new file mode 100644 (file)
index 0000000..9109185
--- /dev/null
@@ -0,0 +1,20 @@
+#include "rtc.hpp"
+#include "widget.hpp"
+
+void TimeWidget::render(Adafruit_GFX& display, unsigned int ypos) 
+{
+       if (auto t = RTC::ticks(); t != prevTicks) {
+               prevTicks = t;
+               display.setTextSize(3);
+               display.setCursor(0, ypos + 4);
+               display.printf("%2d:%02d:%02d", t / 3600, (t % 3600) /
+                       60, t % 60);
+       }
+}
+
+void NotificationWidget::render(Adafruit_GFX& display, unsigned int ypos) 
+{
+       display.setTextSize(2);
+       display.setCursor(0, ypos);
+       display.printf("%-36s", message);
+}
diff --git a/source/widget.hpp b/source/widget.hpp
new file mode 100644 (file)
index 0000000..afa9441
--- /dev/null
@@ -0,0 +1,54 @@
+#ifndef WIDGET_HPP_
+#define WIDGET_HPP_
+
+class Adafruit_GFX;
+
+#include <cstring>
+
+class Widget {
+private:
+       unsigned int height;
+public:
+       constexpr Widget(unsigned int _height = 0)
+               : height(_height) {}
+       
+       inline unsigned int getHeight(void) const {
+               return height;
+       }
+
+       virtual void render(Adafruit_GFX& display, unsigned int ypos) = 0;
+
+protected:
+       inline void setHeight(unsigned int _height) {
+               height = _height;
+       }
+};
+
+class TimeWidget : public Widget {
+private:
+       unsigned int prevTicks;
+
+public:
+       constexpr TimeWidget(void)
+               : Widget(30), prevTicks(0) {}
+
+       void render(Adafruit_GFX& display, unsigned int ypos) final;
+};
+
+class NotificationWidget : public Widget {
+private:
+       char message[36];
+
+public:
+       NotificationWidget(const char *msg)
+               : Widget(48) {
+               unsigned int i;
+               for (i = 0; msg[i] != '\0' && i < 35; i++)
+                       message[i] = msg[i];
+               message[i] = '\0';
+               setHeight(16 * (i / 12 + 1));
+       }
+
+       void render(Adafruit_GFX& display, unsigned int ypos) final;
+};
+#endif // WIDGET_HPP_