|
| 1 | +/** |
| 2 | + * The MySensors Arduino library handles the wireless radio link and protocol |
| 3 | + * between your home built sensors/actuators and HA controller of choice. |
| 4 | + * The sensors forms a self healing radio network with optional repeaters. Each |
| 5 | + * repeater and gateway builds a routing tables in EEPROM which keeps track of the |
| 6 | + * network topology allowing messages to be routed to nodes. |
| 7 | + * |
| 8 | + * Created by Henrik Ekblad <[email protected]> |
| 9 | + * Copyright (C) 2013-2015 Sensnology AB |
| 10 | + * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors |
| 11 | + * |
| 12 | + * Documentation: http://www.mysensors.org |
| 13 | + * Support Forum: http://forum.mysensors.org |
| 14 | + * |
| 15 | + * This program is free software; you can redistribute it and/or |
| 16 | + * modify it under the terms of the GNU General Public License |
| 17 | + * version 2 as published by the Free Software Foundation. |
| 18 | + */ |
| 19 | + |
| 20 | +#ifdef ARDUINO_ARCH_ESP8266 |
| 21 | + |
| 22 | +#include "MyHw.h" |
| 23 | +#include "MyHwESP8266.h" |
| 24 | +#include <EEPROM.h> |
| 25 | + |
| 26 | +/* |
| 27 | +int8_t pinIntTrigger = 0; |
| 28 | +void wakeUp() //place to send the interrupts |
| 29 | +{ |
| 30 | + pinIntTrigger = 1; |
| 31 | +} |
| 32 | +void wakeUp2() //place to send the second interrupts |
| 33 | +{ |
| 34 | + pinIntTrigger = 2; |
| 35 | +} |
| 36 | +
|
| 37 | +// Watchdog Timer interrupt service routine. This routine is required |
| 38 | +// to allow automatic WDIF and WDIE bit clearance in hardware. |
| 39 | +ISR (WDT_vect) |
| 40 | +{ |
| 41 | + // WDIE & WDIF is cleared in hardware upon entering this ISR |
| 42 | + wdt_disable(); |
| 43 | +} |
| 44 | +*/ |
| 45 | + |
| 46 | +static void hw_initConfigBlock( size_t length = 1024 /*ATMega328 has 1024 bytes*/ ) |
| 47 | +{ |
| 48 | + static bool initDone = false; |
| 49 | + if (!initDone) |
| 50 | + { |
| 51 | + EEPROM.begin(length); |
| 52 | + initDone = true; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +void hw_readConfigBlock(void* buf, void* adr, size_t length) |
| 57 | +{ |
| 58 | + hw_initConfigBlock(); |
| 59 | + uint8_t* dst = static_cast<uint8_t*>(buf); |
| 60 | + int offs = reinterpret_cast<int>(adr); |
| 61 | + while (length-- > 0) |
| 62 | + { |
| 63 | + *dst++ = EEPROM.read(offs++); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +void hw_writeConfigBlock(void* buf, void* adr, size_t length) |
| 68 | +{ |
| 69 | + hw_initConfigBlock(); |
| 70 | + uint8_t* src = static_cast<uint8_t*>(buf); |
| 71 | + int offs = reinterpret_cast<int>(adr); |
| 72 | + while (length-- > 0) |
| 73 | + { |
| 74 | + EEPROM.write(offs++, *src++); |
| 75 | + } |
| 76 | + EEPROM.commit(); |
| 77 | +} |
| 78 | + |
| 79 | +uint8_t hw_readConfig(int adr) |
| 80 | +{ |
| 81 | + uint8_t value; |
| 82 | + hw_readConfigBlock(&value, reinterpret_cast<void*>(adr), 1); |
| 83 | + return value; |
| 84 | +} |
| 85 | + |
| 86 | +void hw_writeConfig(int adr, uint8_t value) |
| 87 | +{ |
| 88 | + uint8_t curr = hw_readConfig(adr); |
| 89 | + if (curr != value) |
| 90 | + { |
| 91 | + hw_writeConfigBlock(&value, reinterpret_cast<void*>(adr), 1); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +MyHwESP8266::MyHwESP8266() : MyHw() |
| 98 | +{ |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +/* |
| 103 | +
|
| 104 | +// The following was redifined as macros to save space |
| 105 | +
|
| 106 | +inline uint8_t MyHwATMega328::readConfig(uint8_t pos) { |
| 107 | + return eeprom_read_byte((uint8_t*)pos); |
| 108 | +} |
| 109 | +
|
| 110 | +inline void MyHwATMega328::writeConfig(uint8_t pos, uint8_t value) { |
| 111 | + eeprom_update_byte((uint8_t*)pos, value); |
| 112 | +} |
| 113 | +
|
| 114 | +inline void MyHwATMega328::readConfigBlock(void* buf, void * pos, size_t length) { |
| 115 | + eeprom_read_block(buf, (void*)pos, length); |
| 116 | +} |
| 117 | +
|
| 118 | +inline void MyHwATMega328::writeConfigBlock(void* pos, void* buf, size_t length) { |
| 119 | + eeprom_write_block((void*)pos, (void*)buf, length); |
| 120 | +} |
| 121 | +
|
| 122 | +
|
| 123 | +inline void MyHwATMega328::init() { |
| 124 | + Serial.begin(BAUD_RATE); |
| 125 | +} |
| 126 | +
|
| 127 | +inline void MyHwATMega328::watchdogReset() { |
| 128 | + wdt_reset(); |
| 129 | +} |
| 130 | +
|
| 131 | +inline void MyHwATMega328::reboot() { |
| 132 | + wdt_enable(WDTO_15MS); for (;;); |
| 133 | +} |
| 134 | +
|
| 135 | +inline unsigned long MyHwATMega328::millis() { |
| 136 | + return ::millis(); |
| 137 | +} |
| 138 | +*/ |
| 139 | + |
| 140 | + |
| 141 | +void MyHwESP8266::sleep(unsigned long ms) { |
| 142 | + // TODO: Not supported! |
| 143 | +} |
| 144 | + |
| 145 | +bool MyHwESP8266::sleep(uint8_t interrupt, uint8_t mode, unsigned long ms) { |
| 146 | + // TODO: Not supported! |
| 147 | + return false; |
| 148 | +} |
| 149 | + |
| 150 | +inline uint8_t MyHwESP8266::sleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms) { |
| 151 | + // TODO: Not supported! |
| 152 | + return 0; |
| 153 | +} |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | +#ifdef DEBUG |
| 158 | +void MyHwESP8266::debugPrint(bool isGW, const char *fmt, ... ) { |
| 159 | + char fmtBuffer[300]; |
| 160 | + if (isGW) { |
| 161 | + // prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE) |
| 162 | + snprintf_P(fmtBuffer, 299, PSTR("0;0;%d;0;%d;"), C_INTERNAL, I_LOG_MESSAGE); |
| 163 | + Serial.print(fmtBuffer); |
| 164 | + } |
| 165 | + va_list args; |
| 166 | + va_start (args, fmt ); |
| 167 | + va_end (args); |
| 168 | + if (isGW) { |
| 169 | + // Truncate message if this is gateway node |
| 170 | + vsnprintf_P(fmtBuffer, 60, fmt, args); |
| 171 | + fmtBuffer[59] = '\n'; |
| 172 | + fmtBuffer[60] = '\0'; |
| 173 | + } else { |
| 174 | + vsnprintf_P(fmtBuffer, 299, fmt, args); |
| 175 | + } |
| 176 | + va_end (args); |
| 177 | + Serial.print(fmtBuffer); |
| 178 | + Serial.flush(); |
| 179 | + |
| 180 | + //Serial.write(freeRam()); |
| 181 | +} |
| 182 | +#endif |
| 183 | + |
| 184 | +#endif // #ifdef ARDUINO_ARCH_ESP8266 |
0 commit comments