]> code.bitgloo.com Git - clyne/smartwatch.git/commitdiff
better scrolling, vibration
authorClyne Sullivan <tullivan99@gmail.com>
Sat, 30 Mar 2019 21:17:58 +0000 (17:17 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Sat, 30 Mar 2019 21:17:58 +0000 (17:17 -0400)
source/controller.cpp
source/sharp.cpp
source/sharp.hpp
source/vibrate.hpp [new file with mode: 0644]

index 73bc4408445b51f0a37e8c60ef8fe872a80b1405..defae8ab262e8ae34673c8512767558b4b4758a5 100755 (executable)
@@ -22,6 +22,7 @@
 
 #include "rtc.hpp"
 #include "sharp.hpp"
+#include "vibrate.hpp"
 #include "widget.hpp"
 
 BLEUart bleuart;
@@ -40,8 +41,7 @@ void setup(void)
        while (!Serial)
                delay(10);
 
-       Serial.println(F("Initializing..."));
-
+       Vibrate::begin();
        RTC::begin();
        Sharp::begin();
 
@@ -63,16 +63,13 @@ void setup(void)
        Bluefruit.Advertising.setFastTimeout(30);      // number of seconds in fast mode
        Bluefruit.Advertising.start(0);                // 0 = Don't stop advertising after n seconds  
 
-       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");
+}
+
+static int touchToCoord(int val)
+{
+       return val / 70 * (SHARP_HEIGHT / 10);
 }
 
 void loop(void)
@@ -90,22 +87,15 @@ void loop(void)
                }
 
                auto diff = val - last;
-               if (diff > 50) {
-                       Sharp::setScrollVelocity(1);
+               if (std::abs(diff) > 50) {
+                       Sharp::setScroll(touchToCoord(diff));
                        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);
-               }
+               if (last != 0 && !scrolled)
+                       Sharp::sendInput(touchToCoord(last));
                last = 0;
-               Sharp::setScrollVelocity(0);
+               Sharp::setScroll();
        }
 
        delay(10);
@@ -121,9 +111,11 @@ void handlePacket(void)
 
        switch (buf[0]) {
        case 'L':
-               //RTC::setMessage(buf + 1);
+               Vibrate::pulse();
+               Sharp::addWidget<NotificationWidget>(buf + 1);
                break;
        case 'T':
+               Vibrate::pulse();
                Sharp::addWidget<NotificationWidget>("Time updated");
                RTC::setTicks(std::atoi(buf + 1) * 60);
                break;
index 4b7aa94bb46ed77d721f139639eb4f63d656a465..ab0788b19921d4a1b916055f11c094f79076171f 100644 (file)
@@ -31,7 +31,6 @@ TaskHandle_t Sharp::taskHandle;
 std::vector<Widget *> Sharp::widgets;
 
 int Sharp::topY = 0;
-int Sharp::scrollVelocity = 0;
 
 void Sharp::begin(void)
 {
@@ -70,31 +69,26 @@ void Sharp::sendInput(int ypos)
 
 void Sharp::updateTask([[maybe_unused]] void *arg)
 {
-       static unsigned int counter = 0;
-       while (1) {
-               if (counter++ == 3) {
-                       counter = 0;
+       static int oldTopY = 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;
-                       }
+       while (1) {
+               if (oldTopY != topY) {
+                       oldTopY = topY;
+                       display.clearDisplay();
+               }
 
-                       display.refresh();
-               } else {
-                       topY += scrollVelocity * 20;
-                       if (scrollVelocity != 0)
-                               display.clearDisplay();
-                       if (topY > 0)
-                               topY = 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;
                }
 
-               delay(50);
+               display.refresh();
+               delay(150);
        }
 }
 
index f1b663129b5112ee4870c6b65a4307fa9c69db04..155acecd1cc7389be8d30ba58f079e2ba8eb6f6f 100644 (file)
@@ -40,7 +40,6 @@ private:
        static std::vector<Widget *> widgets;
 
        static int topY;
-       static int scrollVelocity;
 
 public:
        static void begin(void);
@@ -50,8 +49,13 @@ public:
                widgets.emplace_back(new T(args...));
        }
 
-       inline static void setScrollVelocity(int vel) {
-               scrollVelocity = vel;
+       inline static void setScroll(int scr = 0) {
+               static int oldTopY = 0;
+               if (scr == 0) {
+                       oldTopY = topY;
+               } else {
+                       topY = minof(0, oldTopY + scr);
+               }
        }
 
        static void sendInput(int ypos);
diff --git a/source/vibrate.hpp b/source/vibrate.hpp
new file mode 100644 (file)
index 0000000..55eb511
--- /dev/null
@@ -0,0 +1,42 @@
+/**
+ * @file vibrate.hpp
+ * @brief Provides access to the vibration motor.
+ *
+ * Copyright (C) 2019 Clyne Sullivan
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef VIBRATE_HPP_
+#define VIBRATE_HPP_
+
+class Vibrate {
+private:
+       constexpr static unsigned int motorPin = 27;
+       constexpr static unsigned int pulseDuration = 200;
+
+public:
+       inline static void begin(void) {
+               pinMode(motorPin, OUTPUT);
+               digitalWrite(motorPin, false);
+       }
+
+       inline static void pulse(void) {
+               digitalWrite(motorPin, true);
+               delay(pulseDuration);
+               digitalWrite(motorPin, false);
+       }
+};
+
+#endif // VIBRATE_HPP_