FastclockRF/src/main.cpp

296 lines
8.9 KiB
C++

/*
* Copyright (c) 2014-2017 Cesanta Software Limited
* All rights reserved
*/
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RadioHead/RH_NRF24.h"
#include "RadioHead/RHDatagram.h"
// #include <Fonts/Picopixel.h>
#include <Fonts/Org_01.h>
#include <Fonts/TomThumb.h>
#include <Fonts/FreeMonoBold9pt7b.h>
#include "common/cs_dbg.h"
#include "mgos.h"
#include "mgos_app.h"
#include "mgos_timers.h"
#include "mgos_shadow.h"
#include "config.h"
#include "clockMsg.h"
static Adafruit_SSD1306 *display = nullptr;
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ 0x00, 0xc0, // B00000000, B11000000,
0x01, 0xc0, // B00000001, B11000000,
0x01, 0xc0, // B00000001, B11000000,
0x03, 0xe0, // B00000011, B11100000,
0xf3, 0xe0, // B11110011, B11100000,
0xfe, 0xf8, // B11111110, B11111000,
0x7e, 0xff, // B01111110, B11111111,
0x33, 0x9f, // B00110011, B10011111,
0x1f, 0xfc, // B00011111, B11111100,
0x0d, 0x70, // B00001101, B01110000,
0x1b, 0xa0, // B00011011, B10100000,
0x3f, 0xe0, // B00111111, B11100000,
0x3f, 0xf0, // B00111111, B11110000,
0x7c, 0xf0, // B01111100, B11110000,
0x70, 0x70, // B01110000, B01110000,
0x00, 0x30 // B00000000, B00110000
};
#if defined(CHECK_DISPLAY_RESOLUTION)
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
#endif
// Singleton instance of the radio driver
RH_NRF24 nrf24(PIN_NRF24_CSN, PIN_NRF24_CE);
// Address RH_BROADCAST_ADDRESS can be used for broadcasts as destination
RHDatagram Datagram(nrf24, THIS_ADRESS);
static void setSmallTextSize(void) { display->setFont(&TomThumb); }
static uint8_t getSmallTextHeight() { return TomThumb.yAdvance; }
//static uint8_t getSmallTextCharsPerLine() { return 39; }
static void setNormalTextSize(void) { display->setFont(&Org_01); }
static uint8_t getNormalTextHeight() { return Org_01.yAdvance; }
//static uint8_t getNormalTextCharsPerLine() { return 24; }
static void setLargeTextSize(void) { display->setFont(&FreeMonoBold9pt7b); }
//static uint8_t getLargeTextHeight() { return FreeMonoBold9pt7b.yAdvance; }
//static uint8_t getLargeTextCharsPerLine() { return 12; }
static void timer_cb(void *arg);
static void fastclockRF_receive_cb(void *arg);
static void fastclockRF_send_cb(void *arg);
static void initFastclockRF_cb(void *arg);
static void displayBegin(boolean reset) {
//display->begin(SSD1306_SWITCHCAPVCC, 0x3C, reset /* reset */);
display->begin(); (void) reset;
}
static void initDisplay_cb(void *arg) {
static int step=0;
(void) arg;
display = new Adafruit_SSD1306(16 /* RST GPIO */, Adafruit_SSD1306::RES_128_32);
switch (step) {
case 0:
displayBegin(true);
display->display();
LOG(LL_INFO, ("*** Display initialized, height=%d, width=%d", display->height(), display->width()));
break;
case 1:
displayBegin(false);
display->clearDisplay();
display->drawPixel(10, 10, WHITE);
display->drawPixel(12, 12, WHITE);
display->drawPixel(14, 14, WHITE);
display->drawPixel(16, 16, WHITE);
display->display();
LOG(LL_INFO, ("*** Pixel drawn, height=%d, width=%d", display->height(), display->width()));
break;
case 2:
displayBegin(false);
display->drawLine(0, display->height()-1, display->width(), display->height()/2, WHITE);
display->drawCircle(display->width()-20, display->height()-10, 10, WHITE);
display->display();
LOG(LL_INFO, ("*** Line & Circle drawn"));
break;
case 3:
displayBegin(false);
display->drawBitmap(60, 0, logo16_glcd_bmp, 16, 16, 1);
display->display();
LOG(LL_INFO, ("*** Icon/Bitmap drawn"));
break;
default: // do nothing
break;
}
++step;
if (step <= 3) mgos_set_timer(600 /* ms */, false /* repeat */, initDisplay_cb, NULL);
}
void setup(void) {
LOG(LL_INFO, ("*** Setup started"));
LOG(LL_INFO, ("*** Setting timer"));
mgos_set_timer(2000 /* ms */, false /* repeat */, initDisplay_cb, NULL);
mgos_set_timer(5000 /* ms */, true /* repeat */, timer_cb, NULL);
mgos_set_timer(7000 /* ms */, false /* repeat */, initFastclockRF_cb, NULL);
LOG(LL_INFO, ("*** Setup done"));
}
static void show_dashboard(int hour, int minute) {
display->clearDisplay();
display->setTextColor(WHITE, BLACK);
// ***** clock name *****
setNormalTextSize();
display->setCursor(0, getNormalTextHeight()-1);
display->printf("N-RE");
// ****** speed *****
setNormalTextSize();
display->setCursor(55, 2*getNormalTextHeight()-1);
display->printf("1:3,5");
// ***** time *****
setLargeTextSize();
display->setCursor(0, display->height()-1);
display->printf("%02d:%02d", hour, minute);
// ***** halt/go *****
setSmallTextSize();
display->setTextColor(BLACK, WHITE);
display->fillRect(55, display->height() - 2*getSmallTextHeight()-3, 5*4+2, getSmallTextHeight(), WHITE);
display->setCursor(57, display->height() - getSmallTextHeight()-3); display->printf("HALT");
display->setTextColor(WHITE, BLACK);
// **** weekday *****
setSmallTextSize();
display->setCursor(60, display->height()); display->printf("Mo");
// ***** # of clients *****
setSmallTextSize();
display->setCursor(55, getNormalTextHeight()); display->printf("7 -->");
// ***** client list *****
display->writeFastVLine(79, 0, display->height(), WHITE);
for (int i=0; i<5; ++i) {
display->setTextColor(BLACK, WHITE);
display->fillRect(81, i * getSmallTextHeight(), 3*3+1, getSmallTextHeight(), WHITE);
display->setCursor(82, (i+1) * getSmallTextHeight());
display->printf("%02d", i);
display->setTextColor(WHITE, BLACK);
display->setCursor(82+3*3+1+1, (i+1) * getSmallTextHeight());
display->printf("Client-%d", i);
}
display->display();
}
static long lastSentTimeTick = 0;
static long msPerModelSecond = 1000; // 500 = real time
static struct clock_s {
uint8_t day;
uint8_t hour;
uint8_t minute;
uint8_t second;
uint16_t millisecond;
} fastclock;
static void incrementClockByMilliseconds(int amount) {
fastclock.millisecond += amount;
if (fastclock.millisecond >= 1000) {
fastclock.millisecond -= 1000;
fastclock.second++;
if (fastclock.second >= 60) {
fastclock.second -= 60;
fastclock.minute++;
if (fastclock.minute >= 60) {
fastclock.minute -= 60;
fastclock.hour++;
if (fastclock.hour >= 24) {
fastclock.hour -=24;
fastclock.day++;
if (fastclock.day >= 7) {
fastclock.day -= 7;
}
}
}
}
}
LOG(LL_INFO, ("*** new clock: %02d:%02d:%02d.%03d day %d", fastclock.hour, fastclock.minute, fastclock.second, fastclock.millisecond, fastclock.day));
}
static void fastclockRF_receive_cb(void *arg) {
(void) arg;
// check for incoming messages
if (Datagram.available())
{
// Should be a message for us now
uint8_t buf[RH_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
uint8_t from, to, id, flags;
if (Datagram.recvfrom(buf, &len, &from, &to, &id, &flags)) {
LOG(LL_INFO, ("got request: %s", (char*)buf));
}
else
{
LOG(LL_INFO, ("*** Datagram.recvfrom failed"));
}
}
}
static struct clockMsg_s clockMsg;
static void fastclockRF_send_cb(void *arg) {
(void) arg;
clockMsg.msgType = msgType_Clock;
clockMsg.hour = fastclock.hour;
clockMsg.minute = fastclock.minute;
clockMsg.second = fastclock.second;
// send clock info as a broadcast message
LOG(LL_INFO, ("*** Sending clock packet (broadcast)"));
if (Datagram.sendto((uint8_t *) &clockMsg, sizeof(clockMsg), RH_BROADCAST_ADDRESS)) {
LOG(LL_INFO, ("%02d:%02d:%02d - Sent new clock tick", fastclock.hour, fastclock.minute, fastclock.second));
}
}
static void timeTick_cb(void *arg) {
(void) arg;
long newTimeTick = millis();
int fastclockTimeAdvance = (newTimeTick - lastSentTimeTick) / msPerModelSecond;
incrementClockByMilliseconds(fastclockTimeAdvance);
lastSentTimeTick += fastclockTimeAdvance * msPerModelSecond;
}
static void initFastclockRF_cb(void *arg) {
(void) arg;
LOG(LL_INFO, ("*** Setting up RF"));
lastSentTimeTick = millis();
fastclock.day = 0;
fastclock.hour = 0;
fastclock.minute = 0;
fastclock.second = 0;
fastclock.millisecond = 0;
mgos_set_timer(200 /* ms */, true /* repeat */, fastclockRF_receive_cb, NULL);
mgos_set_timer(500 /* ms */, true /* repeat */, timeTick_cb, NULL);
mgos_set_timer(4000 /* ms */, true /* repeat */, fastclockRF_send_cb, NULL);
}
static void timer_cb(void *arg) {
// static int hour = 0, minute = 0;
show_dashboard(fastclock.hour, fastclock.minute);
LOG(LL_INFO, ("%02d:%02d", fastclock.hour, fastclock.minute));
/*
minute++;
if (minute >= 60) { hour++; minute=0; }
if (hour >= 24) { hour=0; }
*/
(void) arg;
}
#if 0
void loop(void) {
/* do not use loop(), use timers instead; otherwise the watchdog timer reboots your device */
}
#endif