RadioShuttle Lib for the STM32 L4 Heltec Board

Dependents:   Turtle_RadioShuttle

Committer:
Helmut Tschemernjak
Date:
Sun Apr 14 18:35:26 2019 +0200
Revision:
13:591254bed18b
Parent:
12:11b2d36e9217
Updated RadioStatus to be in common with mbed and Arduino

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Helmut64 0:0c31756924a2 1 /*
Helmut64 0:0c31756924a2 2 * The file is licensed under the Apache License, Version 2.0
Helmut64 0:0c31756924a2 3 * (c) 2019 Helmut Tschemernjak
Helmut64 0:0c31756924a2 4 * 30826 Garbsen (Hannover) Germany
Helmut64 0:0c31756924a2 5 */
Helmut64 0:0c31756924a2 6
Helmut64 0:0c31756924a2 7
Helmut64 0:0c31756924a2 8 #ifdef ARDUINO
Helmut64 0:0c31756924a2 9 #include <Arduino.h>
Helmut64 0:0c31756924a2 10 #include "arduino-mbed.h"
Helmut64 0:0c31756924a2 11 #include <time.h>
Helmut64 0:0c31756924a2 12 #define FEATURE_LORA 1
Helmut64 0:0c31756924a2 13 #endif
Helmut64 0:0c31756924a2 14 #ifdef __MBED__
Helmut64 0:0c31756924a2 15 #include "mbed.h"
Helmut64 0:0c31756924a2 16 #include "PinMap.h"
Helmut64 0:0c31756924a2 17 #endif
Helmut64 0:0c31756924a2 18 #include "RadioStatusInterface.h"
Helmut64 0:0c31756924a2 19 #include "RadioStatus.h"
Helmut64 0:0c31756924a2 20 #ifdef FEATURE_LORA
Helmut64 0:0c31756924a2 21
Helmut64 0:0c31756924a2 22
Helmut64 0:0c31756924a2 23 MyRadioStatus::MyRadioStatus()
Helmut64 0:0c31756924a2 24 {
Helmut64 0:0c31756924a2 25 _totalTX = 0;
Helmut64 0:0c31756924a2 26 _totalRX = 0;
Helmut64 0:0c31756924a2 27 _totalError = 0;
Helmut64 0:0c31756924a2 28 _totalTimeout = 0;
Helmut64 0:0c31756924a2 29
Helmut64 0:0c31756924a2 30 ledTX = NULL;
Helmut64 0:0c31756924a2 31 ledRX = NULL;
Helmut64 0:0c31756924a2 32 ledTimeout = NULL;
Helmut64 0:0c31756924a2 33 inverted = false;
Helmut64 0:0c31756924a2 34
Helmut64 0:0c31756924a2 35 #ifdef TARGET_STM32L0
Helmut64 0:0c31756924a2 36 ledTX = new DigitalOut(LED3); // blue
Helmut64 0:0c31756924a2 37 *ledTX = 0;
Helmut64 0:0c31756924a2 38 ledRX = new DigitalOut(LED4); // red
Helmut64 0:0c31756924a2 39 *ledRX = 0;
Helmut64 0:0c31756924a2 40 ledTimeout = new DigitalOut(LED1); // green
Helmut64 0:0c31756924a2 41 *ledTimeout = 0;
Helmut64 0:0c31756924a2 42 #endif
Helmut64 0:0c31756924a2 43 #ifdef HELTECL432_REV1
Helmut64 0:0c31756924a2 44 ledTX = new DigitalOut(STATUS_LED); // green
Helmut64 0:0c31756924a2 45 *ledTX = 0;
Helmut64 0:0c31756924a2 46 ledRX = new DigitalOut(LED2); // red
Helmut64 0:0c31756924a2 47 *ledRX = 0;
Helmut64 0:0c31756924a2 48 #endif
Helmut64 0:0c31756924a2 49 #ifdef MyHOME_BOARD_REV4
Helmut64 0:0c31756924a2 50 ledTX = new DigitalOut(LED2); // yellow
Helmut64 0:0c31756924a2 51 *ledTX = 0;
Helmut64 0:0c31756924a2 52 ledRX = new DigitalOut(STATUS_LED); // red
Helmut64 0:0c31756924a2 53 *ledRX = 0;
Helmut64 0:0c31756924a2 54 ledTimeout = new DigitalOut(LED3); // green
Helmut64 0:0c31756924a2 55 *ledTimeout = 0;
Helmut64 0:0c31756924a2 56 #endif
Helmut64 0:0c31756924a2 57 #ifdef __SAMD21G18A__
Helmut64 0:0c31756924a2 58 inverted = true;
Helmut64 0:0c31756924a2 59 #ifdef PIN_LED_TXL
Helmut64 0:0c31756924a2 60 ledTX = new DigitalOut(PIN_LED_TXL); // yellow
Helmut64 0:0c31756924a2 61 *ledTX = 1;
Helmut64 0:0c31756924a2 62 #endif
Helmut64 0:0c31756924a2 63 #ifdef PIN_LED_RXL
Helmut64 0:0c31756924a2 64 ledRX = new DigitalOut(25); // red
Helmut64 0:0c31756924a2 65 *ledRX = 1;
Helmut64 0:0c31756924a2 66 #endif
Helmut64 0:0c31756924a2 67 #endif
Helmut64 0:0c31756924a2 68 #ifdef ARDUINO_ESP32_DEV // ESP32_ECO_POWER_REV_1
Helmut64 0:0c31756924a2 69 ledTX = new DigitalOut(2); // green
Helmut64 0:0c31756924a2 70 *ledTX = 0;
Helmut64 0:0c31756924a2 71 ledRX = new DigitalOut(12); // red
Helmut64 0:0c31756924a2 72 *ledRX = 0;
Helmut64 0:0c31756924a2 73 #endif
Helmut Tschemernjak 13:591254bed18b 74 #if defined(ARDUINO_Heltec_WIFI_LoRa_32) || defined(ARDUINO_WIFI_LORA_32) \
Helmut Tschemernjak 13:591254bed18b 75 || defined(ARDUINO_WIFI_LORA_32_V2) || defined(ARDUINO_WIRELESS_STICK) \
Helmut Tschemernjak 13:591254bed18b 76 || defined(ARDUINO_WIRELESS_STICK_LITE) // the Heltec boards
Helmut Tschemernjak 13:591254bed18b 77 ledTX = new DigitalOut(25); // white
Helmut Tschemernjak 13:591254bed18b 78 *ledTX = 0;
Helmut Tschemernjak 13:591254bed18b 79 #endif
Helmut Tschemernjak 13:591254bed18b 80
Helmut Tschemernjak 13:591254bed18b 81 #ifdef HAS_OLED_DISPLAY
Helmut64 0:0c31756924a2 82 invertedDisplay = false;
Helmut64 0:0c31756924a2 83 _line1[0] = 0;
Helmut64 0:0c31756924a2 84 _line2[0] = 0;
Helmut64 0:0c31756924a2 85 _line3[0] = 0;
Helmut64 0:0c31756924a2 86 _line4[0] = 0;
Helmut64 0:0c31756924a2 87 _line5[0] = 0;
Helmut Tschemernjak 13:591254bed18b 88 displayReset = NULL;
Helmut Tschemernjak 13:591254bed18b 89 #if defined(ARDUINO_Heltec_WIFI_LoRa_32) || defined(ARDUINO_WIFI_LORA_32) \
Helmut Tschemernjak 13:591254bed18b 90 || defined(ARDUINO_WIFI_LORA_32_V2) || defined(ARDUINO_WIRELESS_STICK)
Helmut Tschemernjak 12:11b2d36e9217 91 #define DISPLAY_ADDRESS 0x3c
Helmut Tschemernjak 12:11b2d36e9217 92 #define DISPLAY_SDA 4
Helmut Tschemernjak 12:11b2d36e9217 93 #define DISPLAY_SCL 15
Helmut Tschemernjak 13:591254bed18b 94 #define DISPLAY_RESET 16
Helmut64 0:0c31756924a2 95 displayReset = new DigitalOut(DISPLAY_RESET);
Helmut Tschemernjak 12:11b2d36e9217 96 #endif
Helmut Tschemernjak 13:591254bed18b 97 #ifdef ARDUINO_ESP32_DEV // ESP32_ECO_POWER_REV_1
Helmut Tschemernjak 13:591254bed18b 98 #define DISPLAY_ADDRESS 0x3c
Helmut Tschemernjak 13:591254bed18b 99 #define DISPLAY_SDA 21
Helmut Tschemernjak 13:591254bed18b 100 #define DISPLAY_SCL 22
Helmut Tschemernjak 13:591254bed18b 101 #endif
Helmut Tschemernjak 13:591254bed18b 102 if (displayReset) {
Helmut Tschemernjak 13:591254bed18b 103 *displayReset = 0;
Helmut Tschemernjak 13:591254bed18b 104 wait_ms(50);
Helmut Tschemernjak 13:591254bed18b 105 *displayReset = 1;
Helmut Tschemernjak 13:591254bed18b 106 }
Helmut Tschemernjak 12:11b2d36e9217 107 display = new SSD1306(DISPLAY_ADDRESS, DISPLAY_SDA, DISPLAY_SCL);
Helmut64 0:0c31756924a2 108 display->init();
Helmut64 0:0c31756924a2 109 // display->flipScreenVertically();
Helmut64 0:0c31756924a2 110 display->setFont(ArialMT_Plain_16); // ArialMT_Plain_10);
Helmut64 0:0c31756924a2 111 display->clear();
Helmut64 0:0c31756924a2 112 display->drawString(0, 0, "RadioShuttle 1.4");
Helmut64 0:0c31756924a2 113 display->setFont(ArialMT_Plain_10);
Helmut64 0:0c31756924a2 114 int yoff = 17;
Helmut64 0:0c31756924a2 115 display->drawString(0, yoff, "Peer-to-Peer LoRa Protcol");
Helmut64 0:0c31756924a2 116 yoff += 12;
Helmut64 0:0c31756924a2 117 display->drawString(0, yoff, "Efficient, Fast, Secure");
Helmut64 0:0c31756924a2 118 yoff += 12;
Helmut64 0:0c31756924a2 119 display->drawString(0, yoff, "www.radioshuttle.de");
Helmut64 0:0c31756924a2 120 display->display();
Helmut64 0:0c31756924a2 121 #endif
Helmut64 0:0c31756924a2 122 }
Helmut64 0:0c31756924a2 123
Helmut64 0:0c31756924a2 124 MyRadioStatus::~MyRadioStatus()
Helmut64 0:0c31756924a2 125 {
Helmut64 0:0c31756924a2 126 if (ledTX) {
Helmut64 0:0c31756924a2 127 if (inverted)
Helmut64 0:0c31756924a2 128 *ledTX = 1;
Helmut64 0:0c31756924a2 129 else
Helmut64 0:0c31756924a2 130 *ledTX = 0;
Helmut64 0:0c31756924a2 131 delete ledTX;
Helmut64 0:0c31756924a2 132 }
Helmut64 0:0c31756924a2 133 if (ledRX) {
Helmut64 0:0c31756924a2 134 if (inverted)
Helmut64 0:0c31756924a2 135 *ledRX = 1;
Helmut64 0:0c31756924a2 136 else
Helmut64 0:0c31756924a2 137 *ledRX = 0;
Helmut64 0:0c31756924a2 138 delete ledRX;
Helmut64 0:0c31756924a2 139 }
Helmut64 0:0c31756924a2 140 if (ledTimeout) {
Helmut64 0:0c31756924a2 141 if (inverted)
Helmut64 0:0c31756924a2 142 *ledTimeout = 1;
Helmut64 0:0c31756924a2 143 else
Helmut64 0:0c31756924a2 144 *ledTimeout = 0;
Helmut64 0:0c31756924a2 145 delete ledTimeout;
Helmut64 0:0c31756924a2 146 }
Helmut64 0:0c31756924a2 147 }
Helmut64 0:0c31756924a2 148
Helmut64 0:0c31756924a2 149 void
Helmut64 0:0c31756924a2 150 MyRadioStatus::TXStart(int AppID, int toStation, int length, int dBm)
Helmut64 0:0c31756924a2 151 {
Helmut64 0:0c31756924a2 152 UNUSED(AppID);
Helmut64 0:0c31756924a2 153 UNUSED(toStation);
Helmut64 0:0c31756924a2 154 UNUSED(length);
Helmut64 0:0c31756924a2 155 UNUSED(dBm);
Helmut64 0:0c31756924a2 156 if (ledTX) {
Helmut64 0:0c31756924a2 157 if (inverted)
Helmut64 0:0c31756924a2 158 *ledTX = 0;
Helmut64 0:0c31756924a2 159 else
Helmut64 0:0c31756924a2 160 *ledTX = 1;
Helmut64 0:0c31756924a2 161 }
Helmut Tschemernjak 13:591254bed18b 162 #ifdef HAS_OLED_DISPLAY
Helmut64 0:0c31756924a2 163 snprintf(_line2, sizeof(_line2), "TX(%d) ID(%d) %d dBm", length, toStation, dBm);
Helmut64 0:0c31756924a2 164 UpdateDisplay(true);
Helmut64 0:0c31756924a2 165 #endif
Helmut64 0:0c31756924a2 166 _totalTX++;
Helmut64 0:0c31756924a2 167 }
Helmut64 0:0c31756924a2 168
Helmut64 0:0c31756924a2 169 void
Helmut64 0:0c31756924a2 170 MyRadioStatus::TXComplete(void)
Helmut64 0:0c31756924a2 171 {
Helmut64 0:0c31756924a2 172 if (ledTX) {
Helmut64 0:0c31756924a2 173 if (inverted)
Helmut64 0:0c31756924a2 174 *ledTX = 1;
Helmut64 0:0c31756924a2 175 else
Helmut64 0:0c31756924a2 176 *ledTX = 0;
Helmut64 0:0c31756924a2 177 }
Helmut Tschemernjak 13:591254bed18b 178 #ifdef HAS_OLED_DISPLAY
Helmut64 0:0c31756924a2 179 UpdateDisplay(false);
Helmut64 0:0c31756924a2 180 #endif
Helmut64 0:0c31756924a2 181 }
Helmut64 0:0c31756924a2 182
Helmut64 0:0c31756924a2 183 void
Helmut64 0:0c31756924a2 184 MyRadioStatus::RxDone(int size, int rssi, int snr)
Helmut64 0:0c31756924a2 185 {
Helmut64 0:0c31756924a2 186 UNUSED(size);
Helmut64 0:0c31756924a2 187 UNUSED(rssi);
Helmut64 0:0c31756924a2 188 UNUSED(snr);
Helmut64 0:0c31756924a2 189 if (ledRX) {
Helmut64 0:0c31756924a2 190 if (inverted)
Helmut64 0:0c31756924a2 191 *ledRX = 0;
Helmut64 0:0c31756924a2 192 else
Helmut64 0:0c31756924a2 193 *ledRX = 1;
Helmut64 0:0c31756924a2 194 }
Helmut64 0:0c31756924a2 195 _totalRX++;
Helmut Tschemernjak 13:591254bed18b 196 #ifdef HAS_OLED_DISPLAY
Helmut64 0:0c31756924a2 197 snprintf(_line3, sizeof(_line3), "RX(%d) RSSI(%d) SNR(%d)", size, rssi, snr);
Helmut64 0:0c31756924a2 198 UpdateDisplay(true);
Helmut64 0:0c31756924a2 199 #endif
Helmut64 0:0c31756924a2 200 }
Helmut64 0:0c31756924a2 201
Helmut64 0:0c31756924a2 202 void
Helmut64 0:0c31756924a2 203 MyRadioStatus::RxCompleted(void)
Helmut64 0:0c31756924a2 204 {
Helmut64 0:0c31756924a2 205 if (ledRX) {
Helmut64 0:0c31756924a2 206 if (inverted)
Helmut64 0:0c31756924a2 207 *ledRX = 1;
Helmut64 0:0c31756924a2 208 else
Helmut64 0:0c31756924a2 209 *ledRX = 0;
Helmut64 0:0c31756924a2 210 }
Helmut Tschemernjak 13:591254bed18b 211 #ifdef HAS_OLED_DISPLAY
Helmut64 0:0c31756924a2 212 UpdateDisplay(false);
Helmut64 0:0c31756924a2 213 #endif
Helmut64 0:0c31756924a2 214 }
Helmut64 0:0c31756924a2 215
Helmut64 0:0c31756924a2 216 void
Helmut64 0:0c31756924a2 217 MyRadioStatus::MessageTimeout(int App, int toStation)
Helmut64 0:0c31756924a2 218 {
Helmut64 0:0c31756924a2 219 UNUSED(App);
Helmut64 0:0c31756924a2 220 UNUSED(toStation);
Helmut64 0:0c31756924a2 221 if (ledTimeout)
Helmut64 0:0c31756924a2 222 *ledTimeout = 1;
Helmut64 0:0c31756924a2 223 _totalTimeout++;
Helmut Tschemernjak 13:591254bed18b 224 #ifdef HAS_OLED_DISPLAY
Helmut64 0:0c31756924a2 225 UpdateDisplay(false);
Helmut64 0:0c31756924a2 226 #endif
Helmut64 0:0c31756924a2 227 }
Helmut64 0:0c31756924a2 228
Helmut64 0:0c31756924a2 229
Helmut64 0:0c31756924a2 230 void
Helmut64 0:0c31756924a2 231 MyRadioStatus::UpdateDisplay(bool invertDisplay)
Helmut64 0:0c31756924a2 232 {
Helmut64 0:0c31756924a2 233 UNUSED(invertDisplay);
Helmut Tschemernjak 13:591254bed18b 234 #ifdef HAS_OLED_DISPLAY
Helmut64 0:0c31756924a2 235 int yoff = 0;
Helmut64 0:0c31756924a2 236 int hight = 12;
Helmut64 0:0c31756924a2 237 time_t t = time(NULL);
Helmut64 0:0c31756924a2 238 struct tm mytm;
Helmut64 0:0c31756924a2 239 localtime_r(&t, &mytm);
Helmut64 0:0c31756924a2 240
Helmut64 0:0c31756924a2 241 snprintf(_line1, sizeof(_line1), "%s (%d) %02d:%02d:%02d", _radioType, _stationID,
Helmut64 0:0c31756924a2 242 mytm.tm_hour, mytm.tm_min, mytm.tm_sec);
Helmut64 0:0c31756924a2 243 snprintf(_line4, sizeof(_line4), "Packets RX(%d) TX(%d)", _totalRX, _totalTX);
Helmut64 0:0c31756924a2 244 snprintf(_line5, sizeof(_line5), "RXErr(%d) TOut(%d) %.2f %d", _totalError, _totalTimeout, (double)_frequency/1000000.0, _spreadingFactor);
Helmut64 0:0c31756924a2 245
Helmut64 0:0c31756924a2 246 if (invertDisplay)
Helmut64 0:0c31756924a2 247 display->invertDisplay();
Helmut64 0:0c31756924a2 248 else
Helmut64 0:0c31756924a2 249 display->normalDisplay();
Helmut64 0:0c31756924a2 250
Helmut64 0:0c31756924a2 251 display->setFont(ArialMT_Plain_10);
Helmut64 0:0c31756924a2 252 display->clear();
Helmut64 0:0c31756924a2 253
Helmut64 0:0c31756924a2 254 display->drawString(0, yoff, String(_line1));
Helmut64 0:0c31756924a2 255 yoff += hight;
Helmut64 0:0c31756924a2 256 display->drawString(0, yoff, String(_line2));
Helmut64 0:0c31756924a2 257 yoff += hight;
Helmut64 0:0c31756924a2 258 display->drawString(0, yoff, String(_line3));
Helmut64 0:0c31756924a2 259 yoff += hight;
Helmut64 0:0c31756924a2 260 display->drawString(0, yoff, String(_line4));
Helmut64 0:0c31756924a2 261 yoff += hight;
Helmut64 0:0c31756924a2 262
Helmut64 0:0c31756924a2 263 display->display();
Helmut64 0:0c31756924a2 264 #endif
Helmut64 0:0c31756924a2 265 }
Helmut64 0:0c31756924a2 266
Helmut64 0:0c31756924a2 267 #endif // FEATURE_LORA