#include "wiring_shift.h"
#include "WInterrupts.h"
+#ifndef __cplusplus
// undefine stdlib's abs if encountered
#ifdef abs
#undef abs
#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)
#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__
* 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)
{
#include <bluefruit.h>
-#include "driverSharp.h"
+#include "sharp.hpp"
#include "rtc.hpp"
BLEUart bleuart;
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
Serial.println(F("Ready."));
+ Sharp::setScreen(RTC::showTime);
}
void loop(void)
switch (buf[0]) {
case 'L':
- Sharp::setMessage(buf + 1);
+ RTC::setMessage(buf + 1);
break;
case 'T':
Serial.println("Setting time!");
+++ /dev/null
-#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();
- }
- }
-}
-
+++ /dev/null
-#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 *);
-};
-
+++ /dev/null
-#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;
-}
-
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
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;
#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);
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);
};
--- /dev/null
+#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();
+ }
+}
+
--- /dev/null
+#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_
+