|
| 1 | +#include <ArduinoUniqueID.h> |
| 2 | +#include <EEPROM.h> |
| 3 | + |
| 4 | +#include <tuxp.h> |
| 5 | +#include <thing.h> |
| 6 | +#include <debug.h> |
| 7 | +#include <radio_module_adaptation.h> |
| 8 | + |
| 9 | +#include "mcu_board_adaptation.h" |
| 10 | + |
| 11 | +#if defined(ARDUINO_UNO) && defined(USE_SOFTWARE_SERIAL) |
| 12 | +SoftwareSerial softwareSerial(SOFTWARE_SERIAL_RX_PIN, SOFTWARE_SERIAL_TX_PIN); |
| 13 | +#endif |
| 14 | + |
| 15 | +static char *modelName; |
| 16 | + |
| 17 | +void debugOutputImpl(const char out[]) { |
| 18 | + Serial.println(out); |
| 19 | +} |
| 20 | + |
| 21 | +void printToSerialPort(char title[], uint8_t message[], int size) { |
| 22 | +#if defined(ENABLE_DEBUG) |
| 23 | + |
| 24 | + Serial.print(title); |
| 25 | + |
| 26 | + if (size == 0) { |
| 27 | + Serial.println(F("0 bytes. {}.")); |
| 28 | + } else { |
| 29 | + Serial.print(size); |
| 30 | + Serial.print(F(" bytes. {")); |
| 31 | + for (int i = 0; i < size; i++) { |
| 32 | + Serial.print(message[i]); |
| 33 | + if (i == size - 1) |
| 34 | + break; |
| 35 | + |
| 36 | + Serial.print(F(",")); |
| 37 | + } |
| 38 | + |
| 39 | + Serial.println(F("}.")); |
| 40 | + } |
| 41 | + |
| 42 | +#endif |
| 43 | +} |
| 44 | + |
| 45 | +void configureSerial() { |
| 46 | + Serial.begin(9600); |
| 47 | + while (!Serial) { |
| 48 | + delay(200); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +void initializeEepRom(int eepRomLength) { |
| 53 | + for (int i = 0; i < eepRomLength - 3; i++) |
| 54 | + EEPROM.write(i, 0); |
| 55 | + |
| 56 | + EEPROM.write(eepRomLength - 3, 0xfd); |
| 57 | + EEPROM.write(eepRomLength - 2, 0xfe); |
| 58 | + EEPROM.write(eepRomLength - 1, 0xff); |
| 59 | +} |
| 60 | + |
| 61 | +void (* resetFunc) () = 0; |
| 62 | + |
| 63 | +void resetImpl() { |
| 64 | + resetFunc(); |
| 65 | +} |
| 66 | + |
| 67 | +long getTimeImpl() { |
| 68 | + return millis(); |
| 69 | +} |
| 70 | + |
| 71 | +int readAddressFromEepRom(uint8_t *address, int position) { |
| 72 | + for (int i = 0; i < SIZE_RADIO_ADDRESS; i++) |
| 73 | + *(address + i) = EEPROM.read(position + i); |
| 74 | + |
| 75 | + return position + SIZE_RADIO_ADDRESS; |
| 76 | +} |
| 77 | + |
| 78 | +uint8_t getDacStateByteValue(DacState dacState) { |
| 79 | + if (dacState == NONE) |
| 80 | + return 0; |
| 81 | + else if (dacState == INITIAL) |
| 82 | + return 1; |
| 83 | + else if (dacState == INTRODUCTING) |
| 84 | + return 2; |
| 85 | + else if (dacState == ALLOCATING) |
| 86 | + return 3; |
| 87 | + else if (dacState == ALLOCATED) |
| 88 | + return 4; |
| 89 | + else if (dacState == CONFIGURED) |
| 90 | + return 5; |
| 91 | + else |
| 92 | + return 0; |
| 93 | +} |
| 94 | + |
| 95 | +DacState getDacStateByByte(uint8_t iDacState) { |
| 96 | + if (iDacState == 0) |
| 97 | + return NONE; |
| 98 | + else if (iDacState == 1) |
| 99 | + return INITIAL; |
| 100 | + else if (iDacState == 2) |
| 101 | + return INTRODUCTING; |
| 102 | + else if (iDacState == 3) |
| 103 | + return ALLOCATING; |
| 104 | + else if (iDacState == 4) |
| 105 | + return ALLOCATED; |
| 106 | + else if (iDacState == 5) |
| 107 | + return CONFIGURED; |
| 108 | + else |
| 109 | + return NONE; |
| 110 | +} |
| 111 | + |
| 112 | +void debugOutThingInfo(ThingInfo *thingInfo) { |
| 113 | +#ifdef ENABLE_DEBUG |
| 114 | + char buffer[64]; |
| 115 | + if (thingInfo->dacState == ALLOCATED || |
| 116 | + thingInfo->dacState == CONFIGURED) { |
| 117 | + sprintf(buffer, "Thing ID: %s. DAC state: %d. Address: {%d, %d, %d}.", |
| 118 | + thingInfo->thingId, getDacStateByteValue(thingInfo->dacState), |
| 119 | + thingInfo->address[0], thingInfo->address[1], thingInfo->address[2]); |
| 120 | + } else { |
| 121 | + sprintf(buffer, "Thing ID: %s. DAC state: %d.", |
| 122 | + thingInfo->thingId == NULL ? "NULL" : thingInfo->thingId, getDacStateByteValue(thingInfo->dacState)); |
| 123 | + } |
| 124 | + debugOut(buffer); |
| 125 | +#endif |
| 126 | +} |
| 127 | + |
| 128 | +void loadThingInfoImpl(ThingInfo *thingInfo) { |
| 129 | + uint8_t thingIdSize = EEPROM.read(0); |
| 130 | + |
| 131 | + if (thingIdSize == 0) { |
| 132 | +#ifdef ENABLE_DEBUG |
| 133 | + Serial.println(F("No thing info in storage. Use initial value.")); |
| 134 | +#endif |
| 135 | + |
| 136 | + thingInfo->thingId = NULL; |
| 137 | + thingInfo->dacState = NONE; |
| 138 | + thingInfo->address = NULL; |
| 139 | + thingInfo->uplinkChannelBegin = -1; |
| 140 | + thingInfo->uplinkChannelEnd = -1; |
| 141 | + thingInfo->uplinkAddressHighByte = 0xff; |
| 142 | + thingInfo->uplinkAddressLowByte = 0xff; |
| 143 | + } else { |
| 144 | + int position = 1; |
| 145 | + thingInfo->thingId = malloc(sizeof(char) * (thingIdSize + 1)); |
| 146 | + for (int i = 0; i < thingIdSize; i++) { |
| 147 | + *((thingInfo->thingId) + i) = EEPROM.read(i + 1); |
| 148 | + } |
| 149 | + *((thingInfo->thingId) + thingIdSize) = '\0'; |
| 150 | + position += thingIdSize; |
| 151 | + |
| 152 | + uint8_t iDacState = EEPROM.read(position); |
| 153 | + thingInfo->dacState = getDacStateByByte(iDacState); |
| 154 | + position++; |
| 155 | + |
| 156 | + if (thingInfo->dacState == ALLOCATED || |
| 157 | + thingInfo->dacState == CONFIGURED) { |
| 158 | + EEPROM.get(position, thingInfo->uplinkChannelBegin); |
| 159 | + position += 2; |
| 160 | + |
| 161 | + EEPROM.get(position, thingInfo->uplinkChannelEnd); |
| 162 | + position += 2; |
| 163 | + |
| 164 | + thingInfo->uplinkAddressHighByte = EEPROM.read(position); |
| 165 | + position++; |
| 166 | + thingInfo->uplinkAddressLowByte = EEPROM.read(position); |
| 167 | + position++; |
| 168 | + |
| 169 | + thingInfo->address = malloc(SIZE_RADIO_ADDRESS); |
| 170 | + readAddressFromEepRom(thingInfo->address, position); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + #ifdef ENABLE_DEBUG |
| 175 | + Serial.println(F("ThingInfo loaded.")); |
| 176 | + #endif |
| 177 | + debugOutThingInfo(thingInfo); |
| 178 | +} |
| 179 | + |
| 180 | +int writeAddressToEepRom(uint8_t *address, int position) { |
| 181 | + for (int i = 0; i < SIZE_RADIO_ADDRESS; i++) |
| 182 | + EEPROM.write(position + i , *(address + i)); |
| 183 | + |
| 184 | + return position + SIZE_RADIO_ADDRESS; |
| 185 | +} |
| 186 | + |
| 187 | +void saveThingInfoImpl(ThingInfo *thingInfo) { |
| 188 | + if (thingInfo->thingId == NULL) { |
| 189 | +#ifdef ENABLE_DEBUG |
| 190 | + Serial.println(F("NULL thing ID. Can't save thing info.")); |
| 191 | +#endif |
| 192 | + return; |
| 193 | + } |
| 194 | + |
| 195 | + int thingIdSize = strlen(thingInfo->thingId); |
| 196 | + EEPROM.write(0, thingIdSize); |
| 197 | + for (int i = 0; i < thingIdSize; i++) { |
| 198 | + EEPROM.write(i + 1, *((thingInfo->thingId) + i)); |
| 199 | + } |
| 200 | + |
| 201 | + int position = 1 + thingIdSize; |
| 202 | + EEPROM.write(position, getDacStateByteValue(thingInfo->dacState)); |
| 203 | + position++; |
| 204 | + |
| 205 | + if (thingInfo->dacState == ALLOCATED || |
| 206 | + thingInfo->dacState == CONFIGURED) { |
| 207 | + EEPROM.put(position, thingInfo->uplinkChannelBegin); |
| 208 | + position += 2; |
| 209 | + EEPROM.put(position, thingInfo->uplinkChannelEnd); |
| 210 | + position += 2; |
| 211 | + |
| 212 | + EEPROM.write(position, thingInfo->uplinkAddressHighByte); |
| 213 | + position++; |
| 214 | + EEPROM.write(position, thingInfo->uplinkAddressLowByte); |
| 215 | + position++; |
| 216 | + |
| 217 | + position = writeAddressToEepRom(thingInfo->address, position); |
| 218 | + } |
| 219 | + |
| 220 | + #ifdef ENABLE_DEBUG |
| 221 | + Serial.println(F("ThingInfo saved.")); |
| 222 | + #endif |
| 223 | + |
| 224 | + debugOutThingInfo(thingInfo); |
| 225 | +} |
| 226 | + |
| 227 | +void resetAll() { |
| 228 | + EEPROM.write(EEPROM.length() - 1, 0); |
| 229 | +} |
| 230 | + |
| 231 | +void configureMcuBoard(const char *_modelName) { |
| 232 | + int modelNameLength = strlen(_modelName); |
| 233 | + modelName = malloc(sizeof(char) * (modelNameLength + 1)); |
| 234 | + strcpy(modelName, _modelName); |
| 235 | + |
| 236 | +#ifdef ENABLE_DEBUG |
| 237 | + configureSerial(); |
| 238 | + Serial.println("Serial has configured."); |
| 239 | + setDebugOutputter(debugOutputImpl); |
| 240 | +#endif |
| 241 | + |
| 242 | + registerThingInfoLoader(loadThingInfoImpl); |
| 243 | + registerThingInfoSaver(saveThingInfoImpl); |
| 244 | + registerResetter(resetImpl); |
| 245 | + registerTimer(getTimeImpl); |
| 246 | + |
| 247 | + uint16_t eepRomLength = EEPROM.length(); |
| 248 | + uint8_t lastByteOfEepRom = EEPROM.read(eepRomLength - 1); |
| 249 | + uint8_t nextToLastByteOfEepRom = EEPROM.read(eepRomLength - 2); |
| 250 | + uint8_t theThirdToLastByteOfEepRom = EEPROM.read(eepRomLength - 3); |
| 251 | + |
| 252 | + if (lastByteOfEepRom != 0xff || |
| 253 | + nextToLastByteOfEepRom != 0xfe || |
| 254 | + theThirdToLastByteOfEepRom != 0xfd) { |
| 255 | +#ifdef ENABLE_DEBUG |
| 256 | + Serial.println(F("EEPROM not initialized. Initlize it now.")); |
| 257 | +#endif |
| 258 | + initializeEepRom(eepRomLength); |
| 259 | + } |
| 260 | +} |
0 commit comments