diff options
Diffstat (limited to 'source')
-rwxr-xr-x | source/controller.cpp | 133 | ||||
-rw-r--r-- | source/driverSharp.cpp | 40 | ||||
-rw-r--r-- | source/driverSharp.h | 23 | ||||
-rw-r--r-- | source/rtc.cpp | 32 | ||||
-rw-r--r-- | source/rtc.hpp | 18 |
5 files changed, 132 insertions, 114 deletions
diff --git a/source/controller.cpp b/source/controller.cpp index c4ea12c..bd316b5 100755 --- a/source/controller.cpp +++ b/source/controller.cpp @@ -1,115 +1,54 @@ -/********************************************************************* - This is an example for our nRF52 based Bluefruit LE modules - - Pick one up today in the adafruit shop! - - Adafruit invests time and resources providing this open source code, - please support Adafruit and open-source hardware by purchasing - products from Adafruit! - - MIT license, check LICENSE for more information - All text above, and the splash screen below must be included in - any redistribution -*********************************************************************/ - // sharp takes < ~0.05uA #include <bluefruit.h> -#include <cmath> #include "driverSharp.h" +#include "rtc.hpp" BLEUart bleuart; -// Function prototypes for packetparser.cpp -uint8_t readPacket (BLEUart *ble_uart, uint16_t timeout); -float parsefloat (uint8_t *buffer); -void printHex (const uint8_t * data, const uint32_t numBytes); - -// Packet buffer -extern uint8_t packetbuffer[]; - -void startAdv(void); - -void setup(void) +void app_error_handler_bare([[maybe_unused]] uint32_t error_code) { - Serial.begin(115200); - while ( !Serial ) delay(10); // for nrf52840 with native usb - - Serial.println(F("Initializing...")); - - sharpInit(); - - Bluefruit.begin(); - // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 - Bluefruit.setTxPower(-20);//4); - Bluefruit.setName("Bluefruit52"); - - // Configure and start the BLE Uart service - bleuart.begin(); - - // Set up and start advertising - startAdv(); - - Serial.println(F("Ready.")); + Serial.println("Received an error"); + while (1); } -void startAdv(void) +void setup(void) { - // Advertising packet - Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); - Bluefruit.Advertising.addTxPower(); - - // Include the BLE UART (AKA 'NUS') 128-bit UUID - Bluefruit.Advertising.addService(bleuart); - - // Secondary Scan Response packet (optional) - // Since there is no room for 'Name' in Advertising packet - Bluefruit.ScanResponse.addName(); - - /* Start Advertising - * - Enable auto advertising if disconnected - * - Interval: fast mode = 20 ms, slow mode = 152.5 ms - * - Timeout for fast mode is 30 seconds - * - Start(timeout) with timeout = 0 will advertise forever (until connected) - * - * For recommended advertising interval - * https://developer.apple.com/library/content/qa/qa1931/_index.html - */ - Bluefruit.Advertising.restartOnDisconnect(true); - Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms - Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode - Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds + Serial.begin(115200); + while (!Serial) + delay(10); + + Serial.println(F("Initializing...")); + + RTC::begin(); + Sharp::begin(); + + Bluefruit.begin(); + // Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 + Bluefruit.setTxPower(-20); + Bluefruit.setName("smartwatch"); + + // Configure and start the BLE Uart service + bleuart.begin(); + + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + Bluefruit.Advertising.addService(bleuart); + Bluefruit.ScanResponse.addName(); + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + 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.")); } -/**************************************************************************/ -/*! - @brief Constantly poll for new command or response data -*/ -/**************************************************************************/ void loop(void) { - //static unsigned int i = 0; - //auto y = 8.7L * std::sin(i++ / -0.5L); - //bleuart.print(i); - //bleuart.print(','); - //bleuart.println((int)y); - //delay(900); - // Wait for new data to arrive - - uint8_t len = readPacket(&bleuart, 500); - if (len == 0) return; - - // Buttons - if (packetbuffer[1] == 'B') { - uint8_t buttnum = packetbuffer[2] - '0'; - boolean pressed = packetbuffer[3] - '0'; - Serial.print ("Button "); Serial.print(buttnum); - if (pressed) { - Serial.println(" pressed"); - } else { - Serial.println(" released"); - } - } + if (bleuart.available()) + Serial.print((char)bleuart.read()); + delay(10); } diff --git a/source/driverSharp.cpp b/source/driverSharp.cpp index 2dbcec6..c68790e 100644 --- a/source/driverSharp.cpp +++ b/source/driverSharp.cpp @@ -1,34 +1,42 @@ -#include "sharp/Adafruit_SharpMem.h" +#include "driverSharp.h" +#include "rtc.hpp" -#define SHARP_SCK 12 -#define SHARP_MOSI 13 -#define SHARP_SS 6 +constexpr unsigned int SHARP_SCK = 12; +constexpr unsigned int SHARP_MOSI = 13; +constexpr unsigned int SHARP_SS = 14; -Adafruit_SharpMem display(SHARP_SCK, SHARP_MOSI, SHARP_SS, 144, 168); +Adafruit_SharpMem Sharp::display(SHARP_SCK, SHARP_MOSI, SHARP_SS, 144, 168); +TaskHandle_t Sharp::taskHandle; +bool Sharp::holdRendering = false; #define BLACK 0 #define WHITE 1 -static TaskHandle_t sharpHandle; -void sharpTask(void *arg); - -void sharpInit(void) +void Sharp::begin(void) { display.begin(); display.clearDisplay(); display.setTextSize(3); - display.setTextColor(BLACK); - display.setCursor(0, 0); - display.println("Hello!"); + display.setTextColor(BLACK, WHITE); - xTaskCreate(sharpTask, "sharp", 512, nullptr, TASK_PRIO_LOW, &sharpHandle); + xTaskCreate(updateTask, "sharp", 512, nullptr, TASK_PRIO_LOW, + &taskHandle); } -void sharpTask([[maybe_unused]] void *arg) +void Sharp::updateTask([[maybe_unused]] void *arg) { + static auto old = RTC::ticks(); while (1) { - display.refresh(); - delay(500); + do { + delay(300); + } while (holdRendering); + + if (auto t = RTC::ticks(); t != old) { + old = t; + display.setCursor(0, 60); + display.printf("%2d:%02d", t / 60, t % 60); + display.refresh(); + } } } diff --git a/source/driverSharp.h b/source/driverSharp.h index f99c03c..799c828 100644 --- a/source/driverSharp.h +++ b/source/driverSharp.h @@ -1,2 +1,23 @@ +#include "sharp/Adafruit_SharpMem.h" + +class Sharp { +private: + static Adafruit_SharpMem display; + static TaskHandle_t taskHandle; + static bool holdRendering; + +public: + static void begin(void); + + inline static void pause(void) { + holdRendering = true; + } + + inline static void unpause(void) { + holdRendering = false; + } + +private: + static void updateTask(void *); +}; -void sharpInit(void); diff --git a/source/rtc.cpp b/source/rtc.cpp new file mode 100644 index 0000000..c355c90 --- /dev/null +++ b/source/rtc.cpp @@ -0,0 +1,32 @@ +#include "rtc.hpp" +#include <bluefruit.h> + +nrf_drv_rtc_t RTC::rtc = NRF_DRV_RTC_INSTANCE(2); +unsigned int RTC::rtcCount = 0; + +void RTC::begin(void) +{ + //Initialize RTC instance + nrf_drv_rtc_config_t rtc_config = { + 4095, + RTC_DEFAULT_CONFIG_IRQ_PRIORITY, + RTC_US_TO_TICKS(NRF_MAXIMUM_LATENCY_US, RTC_DEFAULT_CONFIG_FREQUENCY), + RTC_DEFAULT_CONFIG_RELIABLE + }; + + Serial.println(nrf_drv_rtc_init(&rtc, &rtc_config, handler)); + nrf_drv_rtc_tick_enable(&rtc, true); + nrf_drv_rtc_enable(&rtc); +} + +void RTC::handler([[maybe_unused]] nrf_drv_rtc_int_type_t int_type) +{ + static unsigned char counter = 0; + if (int_type == NRF_DRV_RTC_INT_TICK) { + if (++counter == 8) { + counter = 0; + rtcCount++; + } + } +} + diff --git a/source/rtc.hpp b/source/rtc.hpp new file mode 100644 index 0000000..1eb71a8 --- /dev/null +++ b/source/rtc.hpp @@ -0,0 +1,18 @@ +#include <rtc/nrf_drv_rtc.h> + +class RTC { +private: + static nrf_drv_rtc_t rtc; + static unsigned int rtcCount; + +public: + static void begin(void); + + inline static unsigned int ticks(void) { + return rtcCount; + } + +private: + static void handler(nrf_drv_rtc_int_type_t type); +}; + |