]> code.bitgloo.com Git - clyne/smartwatch.git/commitdiff
cleanup, display screen abstraction
authorClyne Sullivan <tullivan99@gmail.com>
Thu, 14 Mar 2019 18:44:18 +0000 (14:44 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Thu, 14 Mar 2019 18:44:18 +0000 (14:44 -0400)
arduino/cores/nRF5/Arduino.h
arduino/cores/nRF5/common_func.h
arduino/libraries/Bluefruit52Lib/src/utility/bonding.cpp
source/controller.cpp
source/driverSharp.cpp [deleted file]
source/driverSharp.h [deleted file]
source/packetParser.cpp [deleted file]
source/rtc.cpp
source/rtc.hpp
source/sharp.cpp [new file with mode: 0644]
source/sharp.hpp [new file with mode: 0644]

index fb0685356055cb6ebd012a966f7e38f3da1206eb..b77a87214fff6c5b668912308d4619180bb71565 100755 (executable)
@@ -86,6 +86,7 @@ uint32_t setLoopStacksize(void);
 #include "wiring_shift.h"
 #include "WInterrupts.h"
 
+#ifndef __cplusplus
 // undefine stdlib's abs if encountered
 #ifdef abs
 #undef abs
@@ -94,6 +95,8 @@ uint32_t setLoopStacksize(void);
 #define min(a,b) ((a)<(b)?(a):(b))
 #define max(a,b) ((a)>(b)?(a):(b))
 #define abs(x) ((x)>0?(x):-(x))
+#endif // __cplusplus
+
 #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
 #define round(x)     ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
 #define radians(deg) ((deg)*DEG_TO_RAD)
index 6862dab0df1d6e9694e01f6cdea8a1a2500b259d..081426e0035ca6f2e0871bf80eb7dbec674fca69 100755 (executable)
 #define __swap32(x)    __REV(x)                   ///< built-in function to swap Endian of 32-bit number
 #define __swap16(u16)  ((uint16_t) __REV16(u16))  ///< built-in function to swap Endian of 16-bit number
 
-#ifndef __cplusplus
-#ifndef max
-#define max(a, b) ((a) > (b) ? (a) : (b))
-#endif // max
-#ifndef min
-#define min(a, b) ((a) < (b) ? (a) : (b))
-#endif // min
-#endif // __cplusplus
-
-#define maxof(a,b) max(a, b)
-#define minof(a,b) min(a, b)
+#define maxof(a, b) ((a) > (b) ? (a) : (b))
+#define minof(a, b) ((a) < (b) ? (a) : (b))
 
 /*------------------------------------------------------------------*/
 /* Count number of arguments of __VA_ARGS__
index e9f9289cdc9fd59e1240953b83067640ed85f880..02908c244cd9c0b37f1a92ebc835bcdad458b40a 100755 (executable)
@@ -56,7 +56,7 @@
  * Each field has an 1-byte preceding length
  *------------------------------------------------------------------*/
 #define SVC_CONTEXT_FLAG      (BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS | BLE_GATTS_SYS_ATTR_FLAG_USR_SRVCS)
-#define BOND_FNAME_LEN        max(sizeof(BOND_FNAME_PRPH), sizeof(BOND_FNAME_CNTR))
+#define BOND_FNAME_LEN        std::max(sizeof(BOND_FNAME_PRPH), sizeof(BOND_FNAME_CNTR))
 
 static void get_fname (char* fname, uint8_t role, uint16_t ediv)
 {
index fc1b70be71f9c0517e87bc29e4aa7bba9323f658..a364c9d65e58d1374d75ce341436c6f7f40241f6 100755 (executable)
@@ -2,7 +2,7 @@
 
 #include <bluefruit.h>
 
-#include "driverSharp.h"
+#include "sharp.hpp"
 #include "rtc.hpp"
 
 BLEUart bleuart;
@@ -45,6 +45,7 @@ void setup(void)
        Bluefruit.Advertising.start(0);                // 0 = Don't stop advertising after n seconds  
 
        Serial.println(F("Ready."));
+       Sharp::setScreen(RTC::showTime);
 }
 
 void loop(void)
@@ -66,7 +67,7 @@ void handlePacket(void)
 
        switch (buf[0]) {
        case 'L':
-               Sharp::setMessage(buf + 1);
+               RTC::setMessage(buf + 1);
                break;
        case 'T':
                Serial.println("Setting time!");
diff --git a/source/driverSharp.cpp b/source/driverSharp.cpp
deleted file mode 100644 (file)
index 4cdc6f1..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-#include "driverSharp.h"
-#include "rtc.hpp"
-
-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);
-TaskHandle_t Sharp::taskHandle;
-bool Sharp::holdRendering = false;
-char Sharp::message[16] = "";
-
-#define BLACK 0
-#define WHITE 1
-
-void Sharp::begin(void)
-{
-       display.begin();
-       display.clearDisplay();
-       display.setTextSize(3);
-       display.setTextColor(BLACK, WHITE);
-
-       xTaskCreate(updateTask, "sharp", 512, nullptr, TASK_PRIO_LOW,
-               &taskHandle);
-}
-
-void Sharp::updateTask([[maybe_unused]] void *arg)
-{
-       static auto old = RTC::ticks();
-       while (1) {
-               do {
-                       delay(300);
-               } while (holdRendering);
-
-               if (auto t = RTC::ticks(); t != old) {
-                       old = 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);
-                       }
-                       display.refresh();
-               }
-       }
-}
-
diff --git a/source/driverSharp.h b/source/driverSharp.h
deleted file mode 100644 (file)
index 986205b..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-#include "sharp/Adafruit_SharpMem.h"
-
-class Sharp {
-private:
-       static Adafruit_SharpMem display;
-       static TaskHandle_t taskHandle;
-       static bool holdRendering;
-       static char message[16];
-
-public:
-       static void begin(void);
-
-       inline static void pause(void) {
-               holdRendering = true;
-       }
-
-       inline static void unpause(void) {
-               holdRendering = false;
-       }
-
-       inline static void setMessage(const char *s) {
-               strncpy(message, s, 16);
-       }
-
-private:
-       static void updateTask(void *);
-};
-
diff --git a/source/packetParser.cpp b/source/packetParser.cpp
deleted file mode 100755 (executable)
index 8f0194f..0000000
+++ /dev/null
@@ -1,134 +0,0 @@
-#include <string.h>
-#include <Arduino.h>
-#include <bluefruit.h>
-
-
-#define PACKET_ACC_LEN                  (15)
-#define PACKET_GYRO_LEN                 (15)
-#define PACKET_MAG_LEN                  (15)
-#define PACKET_QUAT_LEN                 (19)
-#define PACKET_BUTTON_LEN               (5)
-#define PACKET_COLOR_LEN                (6)
-#define PACKET_LOCATION_LEN             (15)
-
-//    READ_BUFSIZE            Size of the read buffer for incoming packets
-#define READ_BUFSIZE                    (20)
-
-
-/* Buffer to hold incoming characters */
-uint8_t packetbuffer[READ_BUFSIZE+1];
-
-/**************************************************************************/
-/*!
-    @brief  Casts the four bytes at the specified address to a float
-*/
-/**************************************************************************/
-float parsefloat(uint8_t *buffer) 
-{
-  float f;
-  memcpy(&f, buffer, 4);
-  return f;
-}
-
-/**************************************************************************/
-/*! 
-    @brief  Prints a hexadecimal value in plain characters
-    @param  data      Pointer to the byte data
-    @param  numBytes  Data length in bytes
-*/
-/**************************************************************************/
-void printHex(const uint8_t * data, const uint32_t numBytes)
-{
-  uint32_t szPos;
-  for (szPos=0; szPos < numBytes; szPos++) 
-  {
-    Serial.print(F("0x"));
-    // Append leading 0 for small values
-    if (data[szPos] <= 0xF)
-    {
-      Serial.print(F("0"));
-      Serial.print(data[szPos] & 0xf, HEX);
-    }
-    else
-    {
-      Serial.print(data[szPos] & 0xff, HEX);
-    }
-    // Add a trailing space if appropriate
-    if ((numBytes > 1) && (szPos != numBytes - 1))
-    {
-      Serial.print(F(" "));
-    }
-  }
-  Serial.println();
-}
-
-/**************************************************************************/
-/*!
-    @brief  Waits for incoming data and parses it
-*/
-/**************************************************************************/
-uint8_t readPacket(BLEUart *ble_uart, uint16_t timeout) 
-{
-  uint16_t origtimeout = timeout, replyidx = 0;
-
-  memset(packetbuffer, 0, READ_BUFSIZE);
-
-  while (timeout--) {
-    if (replyidx >= 20) break;
-    if ((packetbuffer[1] == 'A') && (replyidx == PACKET_ACC_LEN))
-      break;
-    if ((packetbuffer[1] == 'G') && (replyidx == PACKET_GYRO_LEN))
-      break;
-    if ((packetbuffer[1] == 'M') && (replyidx == PACKET_MAG_LEN))
-      break;
-    if ((packetbuffer[1] == 'Q') && (replyidx == PACKET_QUAT_LEN))
-      break;
-    if ((packetbuffer[1] == 'B') && (replyidx == PACKET_BUTTON_LEN))
-      break;
-    if ((packetbuffer[1] == 'C') && (replyidx == PACKET_COLOR_LEN))
-      break;
-    if ((packetbuffer[1] == 'L') && (replyidx == PACKET_LOCATION_LEN))
-      break;
-
-    while (ble_uart->available()) {
-      char c =  ble_uart->read();
-      if (c == '!') {
-        replyidx = 0;
-      }
-      packetbuffer[replyidx] = c;
-      replyidx++;
-      timeout = origtimeout;
-    }
-    
-    if (timeout == 0) break;
-    delay(1);
-  }
-
-  packetbuffer[replyidx] = 0;  // null term
-
-  if (!replyidx)  // no data or timeout 
-    return 0;
-  if (packetbuffer[0] != '!')  // doesn't start with '!' packet beginning
-    return 0;
-  
-  // check checksum!
-  uint8_t xsum = 0;
-  uint8_t checksum = packetbuffer[replyidx-1];
-  
-  for (uint8_t i=0; i<replyidx-1; i++) {
-    xsum += packetbuffer[i];
-  }
-  xsum = ~xsum;
-
-  // Throw an error message if the checksum's don't match
-  if (xsum != checksum)
-  {
-    Serial.print("Checksum mismatch in packet : ");
-    printHex(packetbuffer, replyidx+1);
-    return 0;
-  }
-  
-  // checksum passed!
-  return replyidx;
-}
-
index c355c90409fb7f96c412c23e83f198f31ba21d1b..ddb68c0528631136a6e24cb0952f9c09f2732f14 100644 (file)
@@ -4,6 +4,8 @@
 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
@@ -19,6 +21,22 @@ 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 7f64100d0143b39e941d0239046c0f5849157c32..12dade9d8bb40daa7a5f25f44815919da7e9d6a8 100644 (file)
@@ -1,10 +1,13 @@
 #include <rtc/nrf_drv_rtc.h>
+#include "sharp.hpp"
 
 class RTC {
 private:
        static nrf_drv_rtc_t rtc;
        static unsigned int rtcCount;
 
+       static char message[16];
+
 public:
        static void begin(void);
 
@@ -16,6 +19,12 @@ 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);
 };
diff --git a/source/sharp.cpp b/source/sharp.cpp
new file mode 100644 (file)
index 0000000..e780ae7
--- /dev/null
@@ -0,0 +1,40 @@
+#include "sharp.hpp"
+#include "rtc.hpp"
+
+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);
+TaskHandle_t Sharp::taskHandle;
+bool Sharp::holdRendering = false;
+RenderFunc Sharp::currentScreen;
+
+#define BLACK 0
+#define WHITE 1
+
+void Sharp::begin(void)
+{
+       display.begin();
+       display.clearDisplay();
+       display.setTextSize(3);
+       display.setTextColor(BLACK, WHITE);
+
+       xTaskCreate(updateTask, "sharp", 512, nullptr, TASK_PRIO_LOW,
+               &taskHandle);
+}
+
+void Sharp::updateTask([[maybe_unused]] void *arg)
+{
+       static auto old = RTC::ticks();
+       while (1) {
+               do {
+                       delay(300);
+               } while (holdRendering);
+
+               if (currentScreen)
+                       currentScreen(display);
+               display.refresh();
+       }
+}
+
diff --git a/source/sharp.hpp b/source/sharp.hpp
new file mode 100644 (file)
index 0000000..a122e5d
--- /dev/null
@@ -0,0 +1,39 @@
+#ifndef SHARP_HPP_
+#define SHARP_HPP_
+
+#include "sharp/Adafruit_SharpMem.h"
+
+#include <functional>
+
+using RenderFunc = std::function<void(Adafruit_GFX&)>;
+using Display = Adafruit_GFX;
+
+class Sharp {
+private:
+       static Adafruit_SharpMem display;
+       static TaskHandle_t taskHandle;
+       static bool holdRendering;
+
+       static RenderFunc currentScreen;
+
+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;
+       }
+
+private:
+       static void updateTask(void *);
+};
+
+#endif // SHARP_HPP_
+