]> code.bitgloo.com Git - clyne/smartwatch.git/commitdiff
added input: scrolling and tapping
authorClyne Sullivan <tullivan99@gmail.com>
Thu, 28 Mar 2019 18:47:22 +0000 (14:47 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Thu, 28 Mar 2019 18:47:22 +0000 (14:47 -0400)
Makefile
source/controller.cpp
source/sharp.cpp
source/sharp.hpp
source/widget.cpp
source/widget.hpp

index 60a74902d62bef460070cfa35db50e71d32c5394..961769da7f26ace227afd34ea589120833fe5fd4 100755 (executable)
--- a/Makefile
+++ b/Makefile
@@ -14,7 +14,7 @@ MCUFLAGS = -mcpu=cortex-m4 -mthumb -mabi=aapcs \
 
 CFLAGS = $(MCUFLAGS) --std=gnu99
 CXXFLAGS = $(MCUFLAGS) --std=c++17 -fno-builtin -fno-exceptions \
-       -fno-strict-aliasing
+       -fno-strict-aliasing 
 
 SFLAGS = $(MCUFLAGS) -x assembler-with-cpp \
        -DCONFIG_GPIO_AS_PINRESET \
@@ -35,6 +35,7 @@ CSRC = $(wildcard $(ARDUINO)/cores/nRF5/freertos/Source/*.c) \
        $(ARDUINO)/cores/nRF5/nordic/nrfx/mdk/system_nrf52.c \
        $(ARDUINO)/cores/nRF5/wiring.c \
        $(ARDUINO)/cores/nRF5/wiring_digital.c \
+       $(ARDUINO)/cores/nRF5/wiring_analog_nRF52.c \
        $(ARDUINO)/libraries/FileSystem/src/littlefs/lfs.c \
        $(ARDUINO)/libraries/FileSystem/src/littlefs/lfs_util.c \
        $(ARDUINO)/libraries/Bluefruit52Lib/src/utility/bootloader_util.c \
index d9ac9611f22a5dca3180dffc454a33e2bb452b10..3aa6c3a08bd03b787cbd68e2cc1c99c4c29a7d85 100755 (executable)
@@ -48,6 +48,13 @@ void setup(void)
        Serial.println(F("Ready."));
        Sharp::addWidget<TimeWidget>();
        Sharp::addWidget<NotificationWidget>("Welcome to smartwatch");
+       Sharp::addWidget<NotificationWidget>("Hi");
+       Sharp::addWidget<NotificationWidget>("Once upon a time, there lived an old man. His name was Gerg.");
+       Sharp::addWidget<NotificationWidget>("NOTICE: Play more games");
+       Sharp::addWidget<NotificationWidget>("2 new messages");
+       Sharp::addWidget<NotificationWidget>("you have mail");
+       Sharp::addWidget<NotificationWidget>("GGGGGGGGGGGGGGGGGGGGGGG");
+       Sharp::addWidget<NotificationWidget>("ABCDEFGHIJKLMNOPQRSTUZWXYZ");
 }
 
 void loop(void)
@@ -55,6 +62,34 @@ void loop(void)
        if (bleuart.available())
                handlePacket();
 
+       static int last = 0;
+       static bool scrolled = false;
+       int val = analogRead(A5);
+       if (val >= 10) {
+               if (last == 0) {
+                       scrolled = false;
+                       last = val;
+               }
+
+               auto diff = val - last;
+               if (diff > 50) {
+                       Sharp::setScrollVelocity(1);
+                       scrolled = true;
+               } else if (diff < -50) {
+                       Sharp::setScrollVelocity(-1);
+                       scrolled = true;
+               } else {
+                       Sharp::setScrollVelocity(0);
+               }
+       } else {
+               if (last != 0 && !scrolled) {
+                       int ypos = last / 70 * (SHARP_HEIGHT / 10);
+                       Sharp::sendInput(ypos);
+               }
+               last = 0;
+               Sharp::setScrollVelocity(0);
+       }
+
        delay(10);
 }
 
index 8fa22f981962833f1fb40fe84442aa01d94414c6..857244503314bc23a6e1e2764912c1344c30c06d 100644 (file)
@@ -1,9 +1,6 @@
 #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;
@@ -13,6 +10,9 @@ Adafruit_SharpMem Sharp::display(SHARP_SCK, SHARP_MOSI, SHARP_SS, SHARP_WIDTH,
 TaskHandle_t Sharp::taskHandle;
 std::vector<Widget *> Sharp::widgets;
 
+int Sharp::topY = 0;
+int Sharp::scrollVelocity = 0;
+
 void Sharp::begin(void)
 {
        widgets.reserve(10);
@@ -25,21 +25,56 @@ void Sharp::begin(void)
                &taskHandle);
 }
 
+void Sharp::sendInput(int ypos)
+{
+       if (ypos < 0 || ypos > SHARP_HEIGHT)
+               return;
+
+       int y = topY;
+       for (unsigned int i = 0; i < widgets.size(); i++) {
+               y += widgets[i]->getHeight();
+               if (ypos < y) {
+                       if (widgets[i]->onPress()) {
+                               delete widgets[i];
+                               widgets.erase(widgets.begin() + i);
+                               display.clearDisplay();
+                       }
+                       break;
+               }
+
+               y += 3;
+               if (y >= SHARP_HEIGHT)
+                       break;
+       }
+}
+
 void Sharp::updateTask([[maybe_unused]] void *arg)
 {
+       static unsigned int counter = 0;
        while (1) {
-               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 (counter++ == 3) {
+                       counter = 0;
+
+                       auto y = topY;
+                       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;
+                       }
+
+                       display.refresh();
+               } else {
+                       topY += scrollVelocity * 20;
+                       if (scrollVelocity != 0)
+                               display.clearDisplay();
+                       if (topY > 0)
+                               topY = 0;
                }
 
-               display.refresh();
-               delay(300);
+               delay(50);
        }
 }
 
index 277571189fdc73df942b074de95f5a1db8d0fb9d..3c1d34ee2f2dac32ede86146f1cd7250f871859f 100644 (file)
@@ -9,12 +9,19 @@
 #define BLACK 0
 #define WHITE 1
 
+constexpr int SHARP_WIDTH = 144;
+constexpr int SHARP_HEIGHT = 168;
+
 class Sharp {
 private:
        static Adafruit_SharpMem display;
        static TaskHandle_t taskHandle;
 
        static std::vector<Widget *> widgets;
+
+       static int topY;
+       static int scrollVelocity;
+
 public:
        static void begin(void);
 
@@ -23,6 +30,12 @@ public:
                widgets.emplace_back(new T(args...));
        }
 
+       inline static void setScrollVelocity(int vel) {
+               scrollVelocity = vel;
+       }
+
+       static void sendInput(int ypos);
+
 private:
        static void updateTask(void *);
 };
index 91091852b6dadd60bd71862ad22921220ab0a9bb..08b9dc635ba5d636632dd80abee4161b598735c3 100644 (file)
@@ -1,7 +1,7 @@
 #include "rtc.hpp"
 #include "widget.hpp"
 
-void TimeWidget::render(Adafruit_GFX& display, unsigned int ypos) 
+void TimeWidget::render(Adafruit_GFX& display, int ypos) 
 {
        if (auto t = RTC::ticks(); t != prevTicks) {
                prevTicks = t;
@@ -12,7 +12,7 @@ void TimeWidget::render(Adafruit_GFX& display, unsigned int ypos)
        }
 }
 
-void NotificationWidget::render(Adafruit_GFX& display, unsigned int ypos) 
+void NotificationWidget::render(Adafruit_GFX& display, int ypos) 
 {
        display.setTextSize(2);
        display.setCursor(0, ypos);
index afa9441db779cb7aef18b9084624ee10f24f7601..f94ccdb38c01a845a37d2b6cd2e5d0d3ea12a701 100644 (file)
@@ -3,6 +3,7 @@
 
 class Adafruit_GFX;
 
+#include <bluefruit.h>
 #include <cstring>
 
 class Widget {
@@ -16,7 +17,8 @@ public:
                return height;
        }
 
-       virtual void render(Adafruit_GFX& display, unsigned int ypos) = 0;
+       virtual void render(Adafruit_GFX& display, int ypos) = 0;
+       virtual bool onPress(void) = 0;
 
 protected:
        inline void setHeight(unsigned int _height) {
@@ -32,7 +34,10 @@ public:
        constexpr TimeWidget(void)
                : Widget(30), prevTicks(0) {}
 
-       void render(Adafruit_GFX& display, unsigned int ypos) final;
+       void render(Adafruit_GFX& display, int ypos) final;
+       bool onPress(void) final {
+               return false;
+       }
 };
 
 class NotificationWidget : public Widget {
@@ -49,6 +54,10 @@ public:
                setHeight(16 * (i / 12 + 1));
        }
 
-       void render(Adafruit_GFX& display, unsigned int ypos) final;
+       void render(Adafruit_GFX& display, int ypos) final;
+       bool onPress(void) final {
+               return true;
+       }
 };
+
 #endif // WIDGET_HPP_