Skip to content

Commit 22b5c92

Browse files
committed
Merge pull request arduino#181 from Yveaux/master
ESP8266 Gateway support
2 parents 01c86cf + 49f880e commit 22b5c92

18 files changed

+705
-30
lines changed

libraries/MySensors/MyHwATMega328.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* version 2 as published by the Free Software Foundation.
1818
*/
1919

20+
#ifdef ARDUINO_ARCH_AVR
21+
2022
#include "MyHw.h"
2123
#include "MyHwATMega328.h"
2224

@@ -215,3 +217,4 @@ void MyHwATMega328::debugPrint(bool isGW, const char *fmt, ... ) {
215217
}
216218
#endif
217219

220+
#endif // #ifdef ARDUINO_ARCH_AVR

libraries/MySensors/MyHwATMega328.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#ifndef MyHwATMega328_h
2121
#define MyHwATMega328_h
2222

23+
#ifdef ARDUINO_ARCH_AVR
24+
2325
#include "MyHw.h"
2426
#include "MyConfig.h"
2527
#include "MyMessage.h"
@@ -116,3 +118,4 @@ class MyHwATMega328 : public MyHw
116118
void internalSleep(unsigned long ms);
117119
};
118120
#endif
121+
#endif // #ifdef ARDUINO_ARCH_AVR

libraries/MySensors/MyHwESP8266.cpp

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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

libraries/MySensors/MyHwESP8266.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
#ifndef MyHwESP8266_h
23+
#define MyHwESP8266_h
24+
25+
#include "MyHw.h"
26+
#include "MyConfig.h"
27+
#include "MyMessage.h"
28+
29+
30+
#ifdef __cplusplus
31+
#include <Arduino.h>
32+
#include <SPI.h>
33+
#endif
34+
35+
// Define these as macros to save valuable space
36+
37+
#define hw_digitalWrite(__pin, __value) (digitalWrite(__pin, __value))
38+
#define hw_init() Serial.begin(BAUD_RATE)
39+
#define hw_watchdogReset() wdt_reset()
40+
#define hw_reboot() wdt_enable(WDTO_15MS); while (1)
41+
#define hw_millis() millis()
42+
43+
void hw_readConfigBlock(void* buf, void* adr, size_t length);
44+
void hw_writeConfigBlock(void* buf, void* adr, size_t length);
45+
void hw_writeConfig(int adr, uint8_t value);
46+
uint8_t hw_readConfig(int adr);
47+
48+
enum period_t
49+
{
50+
SLEEP_15Ms,
51+
SLEEP_30MS,
52+
SLEEP_60MS,
53+
SLEEP_120MS,
54+
SLEEP_250MS,
55+
SLEEP_500MS,
56+
SLEEP_1S,
57+
SLEEP_2S,
58+
SLEEP_4S,
59+
SLEEP_8S,
60+
SLEEP_FOREVER
61+
};
62+
63+
class MyHwESP8266 : public MyHw
64+
{
65+
public:
66+
MyHwESP8266();
67+
68+
/* void init();
69+
void watchdogReset();
70+
void reboot();
71+
unsigned long millis();
72+
uint8_t readConfig(uint8_t pos);
73+
void writeConfig(uint8_t pos, uint8_t value);
74+
void readConfigBlock(void* buf, void * pos, size_t length);
75+
void writeConfigBlock(void* pos, void* buf, size_t length); */
76+
77+
void sleep(unsigned long ms);
78+
bool sleep(uint8_t interrupt, uint8_t mode, unsigned long ms);
79+
uint8_t sleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms);
80+
#ifdef DEBUG
81+
void debugPrint(bool isGW, const char *fmt, ... );
82+
#endif
83+
};
84+
#endif
85+
86+
#endif // #ifdef ARDUINO_ARCH_ESP8266

libraries/MySensors/MyMessage.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ typedef enum {
163163

164164

165165

166+
#ifndef BIT
166167
#define BIT(n) ( 1<<(n) )
168+
#endif
167169
// Create a bitmask of length len.
168170
#define BIT_MASK(len) ( BIT(len)-1 )
169171
// Create a bitfield mask of length starting at bit 'start'.

libraries/MySensors/MySensor.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,12 @@ bool MySensor::isValidFirmware() {
9595

9696
#ifdef WITH_LEDS_BLINKING
9797
void MySensor::handleLedsBlinking() {
98-
static unsigned long next_time = hw_millis() + ledBlinkPeriod;
99-
10098
// Just return if it is not the time...
10199
// http://playground.arduino.cc/Code/TimingRollover
102-
if ((long)(hw_millis() - next_time) < 0)
100+
if ((long)(hw_millis() - blink_next_time) < 0)
103101
return;
104102
else
105-
next_time = hw_millis() + ledBlinkPeriod;
103+
blink_next_time = hw_millis() + ledBlinkPeriod;
106104

107105
// do the actual blinking
108106
if(countRx && countRx != 255) {

libraries/MySensors/MySensor.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@
4141

4242
// Set the hardware driver to use (initialized by MySensor-class)
4343
//#if defined __AVR_ATmega328P__
44+
#if defined(ARDUINO_ARCH_ESP8266)
45+
#include "MyHwESP8266.h"
46+
typedef MyHwESP8266 MyHwDriver;
47+
#elif defined(ARDUINO_ARCH_AVR)
4448
#include "MyHwATMega328.h"
4549
typedef MyHwATMega328 MyHwDriver;
50+
#endif
4651
//#endif
4752

4853

@@ -356,6 +361,7 @@ class MySensor
356361

357362
unsigned long ledBlinkPeriod;
358363
void handleLedsBlinking(); // do the actual blinking
364+
unsigned long blink_next_time;
359365
#endif
360366

361367
MyTransport& radio;

libraries/MySensors/MySigningAtsha204.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* HMAC-SHA256 authentication with a readout-protected key.
2525
*
2626
*/
27+
#if defined(ARDUINO_ARCH_AVR)
2728

2829
#include "MySigning.h"
2930
#include "MySigningAtsha204.h"
@@ -266,3 +267,4 @@ uint8_t* MySigningAtsha204::sha256(const uint8_t* data, size_t sz) {
266267
DEBUG_SIGNING_PRINTBUF(F("SHA:"), &rx_buffer[SHA204_BUFFER_POS_DATA], 32);
267268
return &rx_buffer[SHA204_BUFFER_POS_DATA];
268269
}
270+
#endif // #if defined(ARDUINO_ARCH_AVR)

0 commit comments

Comments
 (0)