ESP libraries
This commit is contained in:
@@ -36,13 +36,14 @@
|
||||
#endif
|
||||
|
||||
// CORE
|
||||
#include "drivers/SPI/SPI.cpp"
|
||||
|
||||
|
||||
// HARDWARE
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
#include "drivers/ESP8266/SPI/SPI.cpp"
|
||||
#include "core/MyHwESP8266.cpp"
|
||||
#elif defined(ARDUINO_ARCH_AVR)
|
||||
#include "drivers/AVR/SPI/SPI.cpp"
|
||||
#include "core/MyHwATMega328.cpp"
|
||||
#endif
|
||||
|
||||
@@ -66,26 +67,25 @@
|
||||
#define MY_RADIO_FEATURE
|
||||
// SOFTSPI
|
||||
#ifdef MY_SOFTSPI
|
||||
#include "drivers/DigitalIO/SoftI2cMaster.cpp"
|
||||
#include "drivers/DigitalIO/PinIO.cpp"
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
#error Soft SPI is not available on ESP8266
|
||||
#endif
|
||||
#include "drivers/AVR/DigitalIO/SoftI2cMaster.cpp"
|
||||
#include "drivers/AVR/DigitalIO/PinIO.cpp"
|
||||
#endif
|
||||
|
||||
// FLASH
|
||||
#ifdef MY_OTA_FIRMWARE_FEATURE
|
||||
SPIFlash flash(MY_OTA_FLASH_SS, MY_OTA_FLASH_JDECID);
|
||||
#include "drivers/SPIFlash/SPIFlash.cpp"
|
||||
SPIFlash _flash(MY_OTA_FLASH_SS, MY_OTA_FLASH_JDECID);
|
||||
#endif
|
||||
|
||||
#include "core/MyTransport.cpp"
|
||||
#if defined(MY_RADIO_NRF24)
|
||||
#include "drivers/RF24/RF24.cpp"
|
||||
#include "core/MyTransportNRF24.cpp"
|
||||
MyTransportNRF24 transport;
|
||||
|
||||
#elif defined(MY_RADIO_RFM69)
|
||||
#include "drivers/RFM69/RFM69.cpp"
|
||||
#include "core/MyTransportRFM69.cpp"
|
||||
MyTransportRFM69 transport;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -93,10 +93,8 @@
|
||||
// GATEWAY - TRANSPORT
|
||||
#if defined(MY_GATEWAY_FEATURE)
|
||||
// GATEWAY - PROTOCOL
|
||||
#include "core/MyProtocol.cpp"
|
||||
#if defined(MY_GATEWAY_PROTOCOL_DEFAULT)
|
||||
#include "core/MyProtocolDefault.cpp"
|
||||
MyProtocolDefault _protocol;
|
||||
#else
|
||||
#error No gateway protocol specified!
|
||||
#endif
|
||||
@@ -105,29 +103,29 @@
|
||||
#define MY_REPEATER_FEATURE
|
||||
#endif
|
||||
#if defined(MY_GATEWAY_W5100_CLIENT) || defined(MY_GATEWAY_W5100_SERVER)
|
||||
#include "drivers/Ethernet_W5100/utility/socket.cpp"
|
||||
#include "drivers/Ethernet_W5100/utility/w5100.cpp"
|
||||
#include "drivers/Ethernet_W5100/DNS.cpp"
|
||||
#include "drivers/Ethernet_W5100/Ethernet.cpp"
|
||||
#include "drivers/Ethernet_W5100/EthernetUdp.cpp"
|
||||
#include "drivers/Ethernet_W5100/IPAddress.cpp"
|
||||
#include "drivers/AVR/Ethernet_W5100/utility/socket.cpp"
|
||||
#include "drivers/AVR/Ethernet_W5100/utility/w5100.cpp"
|
||||
#include "drivers/AVR/Ethernet_W5100/DNS.cpp"
|
||||
#include "drivers/AVR/Ethernet_W5100/Ethernet.cpp"
|
||||
#include "drivers/AVR/Ethernet_W5100/EthernetUdp.cpp"
|
||||
#include "drivers/AVR/Ethernet_W5100/IPAddress.cpp"
|
||||
|
||||
#if !defined(MY_PORT)
|
||||
#error You must define MY_PORT
|
||||
#endif
|
||||
#if !defined(USE_UDP)
|
||||
#include "drivers/Ethernet_W5100/EthernetServer.cpp"
|
||||
#include "drivers/AVR/Ethernet_W5100/EthernetServer.cpp"
|
||||
#endif
|
||||
#if defined(MY_GATEWAY_W5100_CLIENT) && !defined(MY_CONTROLLER_IP_ADDRESS)
|
||||
#error You must define MY_CONTROLLER_IP_ADDRESS when acting as client
|
||||
#endif
|
||||
|
||||
#if defined(MY_GATEWAY_W5100_CLIENT)
|
||||
#include "drivers/Ethernet_W5100/EthernetClient.cpp"
|
||||
#include "drivers/AVR/Ethernet_W5100/EthernetClient.cpp"
|
||||
#include "core/MyGatewayTransportEthernet.cpp"
|
||||
#endif
|
||||
#elif defined(MY_GATEWAY_SERIAL)
|
||||
#include "core/MyGatewayTransportSerial.cpp"
|
||||
MyGatewayTransportSerial gwTransport(protocol);
|
||||
#endif
|
||||
#else
|
||||
#undef MY_GATEWAY_FEATURE
|
||||
|
||||
+23
-5
@@ -5,8 +5,8 @@
|
||||
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
|
||||
* network topology allowing messages to be routed to nodes.
|
||||
*
|
||||
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
|
||||
* Copyright (C) 2013-2015 Sensnology AB
|
||||
* Created by Tomas Hozza <thozza@gmail.com>
|
||||
* Copyright (C) 2015 Tomas Hozza
|
||||
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
|
||||
*
|
||||
* Documentation: http://www.mysensors.org
|
||||
@@ -17,7 +17,25 @@
|
||||
* version 2 as published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#include "MyTransport.h"
|
||||
#ifndef MyGatewayTransport_h
|
||||
#define MyGatewayTransport_h
|
||||
|
||||
MyTransport::MyTransport() {
|
||||
}
|
||||
#include "MyConfig.h"
|
||||
#include "MyMessage.h"
|
||||
#include "MyProtocol.h"
|
||||
|
||||
|
||||
|
||||
// initialize the driver
|
||||
bool gatewayTransportBegin();
|
||||
|
||||
// Send message to controller
|
||||
bool gatewayTransportSend(MyMessage &message);
|
||||
|
||||
// Check if a new message is available from controller
|
||||
bool gatewayTransportAvailable();
|
||||
|
||||
// Pick up last message received from controller
|
||||
MyMessage& gatewayTransportReceive();
|
||||
|
||||
#endif /* MyGatewayTransportEthernet_h */
|
||||
@@ -64,7 +64,7 @@ bool gatewayTransportSend(MyMessage &message)
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
_ethernetMsg = _protocol.format(message);
|
||||
_ethernetMsg = protocolFormat(message);
|
||||
#ifdef MY_USE_UDP
|
||||
_ethernetServer.beginPacket(_ethernetControllerIP, MY_CONTROLLER_PORT);
|
||||
_ethernetServer.write(_ethernetMsg, strlen(_ethernetMsg));
|
||||
@@ -128,7 +128,7 @@ bool gatewayTransportAvailable()
|
||||
|
||||
if (available) {
|
||||
// Parse message and return parse result
|
||||
available = _protocol.parse(_ethernetMsg, _ethernetInputBuffer);
|
||||
available = protocolParse(_ethernetMsg, _ethernetInputBuffer);
|
||||
}
|
||||
|
||||
return available;
|
||||
|
||||
@@ -24,36 +24,19 @@
|
||||
#include "MyMessage.h"
|
||||
#include "MyProtocol.h"
|
||||
#include "MyProtocolDefault.h"
|
||||
#include "MyGatewayTransport.h"
|
||||
|
||||
#include "drivers/Ethernet_W5100/IPAddress.h"
|
||||
|
||||
// Use this if you have attached a Ethernet ENC28J60 shields
|
||||
//#include <UIPEthernet.h>
|
||||
|
||||
// Use this fo WizNET W5100 module and Arduino Ethernet Shield
|
||||
#include "drivers/Ethernet_W5100/Ethernet.h"
|
||||
|
||||
#ifdef MY_USE_UDP
|
||||
#include "drivers/Ethernet_W5100/EthernetUdp.h"
|
||||
#endif /* MY_USE_UDP */
|
||||
|
||||
#define CONTROLLER_PORT 5003
|
||||
#define IP_RENEWAL_INTERVAL 60000 // in milliseconds
|
||||
#define MAX_RECEIVE_LENGTH 100 // Maximum message length for messages coming from controller
|
||||
|
||||
|
||||
// initialize the driver
|
||||
bool gatewayTransportBegin();
|
||||
|
||||
// Send message to controller
|
||||
bool gatewayTransportSend(MyMessage &message);
|
||||
|
||||
// Check if a new message is available from controller
|
||||
bool gatewayTransportAvailable();
|
||||
|
||||
// Pick up last message received from controller
|
||||
MyMessage& gatewayTransportReceive();
|
||||
|
||||
#ifndef MY_IP_ADDRESS
|
||||
void gatewayTransportRenewIP();
|
||||
#endif
|
||||
|
||||
@@ -17,54 +17,55 @@
|
||||
* version 2 as published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
|
||||
#include "MyGatewayTransportSerial.h"
|
||||
|
||||
char _serialInputString[MAX_RECEIVE_LENGTH]; // A buffer for incoming commands from serial interface
|
||||
int _serialInputPos;
|
||||
MyMessage _serialMsg;
|
||||
|
||||
MyGatewayTransportSerial::MyGatewayTransportSerial(MyProtocol &_protocol) : MyGatewayTransport(_protocol),inputPos(0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool MyGatewayTransportSerial::begin() {
|
||||
bool gatewayTransportBegin() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MyGatewayTransportSerial::send(MyMessage &message) {
|
||||
Serial.print(protocol.format(message));
|
||||
bool gatewayTransportSend(MyMessage &message) {
|
||||
Serial.print(protocolFormat(message));
|
||||
// Serial print is always successful
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MyGatewayTransportSerial::available() {
|
||||
bool gatewayTransportAvailable() {
|
||||
bool available = false;
|
||||
while (Serial.available()) {
|
||||
// get the new byte:
|
||||
char inChar = (char) Serial.read();
|
||||
// if the incoming character is a newline, set a flag
|
||||
// so the main loop can do something about it:
|
||||
if (inputPos < MAX_RECEIVE_LENGTH - 1 && !available) {
|
||||
if (_serialInputPos < MAX_RECEIVE_LENGTH - 1 && !available) {
|
||||
if (inChar == '\n') {
|
||||
inputString[inputPos] = 0;
|
||||
_serialInputString[_serialInputPos] = 0;
|
||||
available = true;
|
||||
} else {
|
||||
// add it to the inputString:
|
||||
inputString[inputPos] = inChar;
|
||||
inputPos++;
|
||||
_serialInputString[_serialInputPos] = inChar;
|
||||
_serialInputPos++;
|
||||
}
|
||||
} else {
|
||||
// Incoming message too long. Throw away
|
||||
inputPos = 0;
|
||||
_serialInputPos = 0;
|
||||
}
|
||||
}
|
||||
if (available) {
|
||||
// Parse message and return parse result
|
||||
available = protocol.parse(msg, inputString);
|
||||
inputPos = 0;
|
||||
available = protocolParse(_serialMsg, _serialInputString);
|
||||
_serialInputPos = 0;
|
||||
}
|
||||
return available;
|
||||
}
|
||||
|
||||
MyMessage & MyGatewayTransportSerial::receive() {
|
||||
MyMessage & gatewayTransportReceive() {
|
||||
// Return the last parsed message
|
||||
return msg;
|
||||
return _serialMsg;
|
||||
}
|
||||
|
||||
@@ -21,32 +21,10 @@
|
||||
#define MyGatewayTransportSerial_h
|
||||
|
||||
#include "MyMessage.h"
|
||||
#include "MyGatewayTransport.h"
|
||||
#include "MyProtocol.h"
|
||||
#include "MyProtocolDefault.h"
|
||||
|
||||
#define MAX_RECEIVE_LENGTH 100 // Maximum message length for messages coming from controller
|
||||
|
||||
class MyGatewayTransportSerial : public MyGatewayTransport
|
||||
{
|
||||
public:
|
||||
// MyGateway constructor
|
||||
MyGatewayTransportSerial(MyProtocol &protocol = *new MyProtocolDefault());
|
||||
|
||||
// initialize the transport driver
|
||||
bool begin();
|
||||
|
||||
// Send message to controller
|
||||
bool send(MyMessage &message);
|
||||
|
||||
// Check if a new message is available from controller
|
||||
bool available();
|
||||
|
||||
// Pick up last message received from controller
|
||||
MyMessage& receive();
|
||||
private:
|
||||
char inputString[MAX_RECEIVE_LENGTH]; // A buffer for incoming commands from serial interface
|
||||
int inputPos;
|
||||
MyMessage msg;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* The MySensors Arduino library handles the wireless radio link and protocol
|
||||
* between your home built sensors/actuators and HA controller of choice.
|
||||
* The sensors forms a self healing radio network with optional repeaters. Each
|
||||
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
|
||||
* network topology allowing messages to be routed to nodes.
|
||||
*
|
||||
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
|
||||
* Copyright (C) 2013-2015 Sensnology AB
|
||||
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
|
||||
*
|
||||
* Documentation: http://www.mysensors.org
|
||||
* Support Forum: http://forum.mysensors.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* version 2 as published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#ifndef MyHw_h
|
||||
#define MyHw_h
|
||||
|
||||
#include "MyConfig.h"
|
||||
#include "MyMessage.h"
|
||||
#include "MyHw.h"
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include <Arduino.h>
|
||||
#endif
|
||||
|
||||
// Implement these as functions or macros
|
||||
/*
|
||||
#define hwDigitalWrite(__pin, __value)
|
||||
#define hwInit() Serial.begin(BAUD_RATE)
|
||||
#define hwWatchdogReset() wdt_reset()
|
||||
#define hwReboot() wdt_enable(WDTO_15MS); while (1)
|
||||
#define hwMillis() millis()
|
||||
|
||||
void hwReadConfigBlock(void* buf, void* adr, size_t length);
|
||||
void hwWriteConfigBlock(void* buf, void* adr, size_t length);
|
||||
void hwWriteConfig(int adr, uint8_t value);
|
||||
uint8_t hwReadConfig(int adr);
|
||||
*/
|
||||
|
||||
void hwSleep(unsigned long ms);
|
||||
bool hwSleep(uint8_t interrupt, uint8_t mode, unsigned long ms);
|
||||
uint8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms);
|
||||
#ifdef MY_DEBUG
|
||||
void hwDebugPrint(const char *fmt, ... );
|
||||
#endif
|
||||
|
||||
#endif // #ifdef ARDUINO_ARCH_ESP8266
|
||||
@@ -20,8 +20,7 @@
|
||||
#ifndef MyHwATMega328_h
|
||||
#define MyHwATMega328_h
|
||||
|
||||
#ifdef ARDUINO_ARCH_AVR
|
||||
|
||||
#include "MyHw.h"
|
||||
#include "MyConfig.h"
|
||||
#include "MyMessage.h"
|
||||
#include <avr/eeprom.h>
|
||||
@@ -92,14 +91,6 @@ enum period_t
|
||||
SLEEP_FOREVER
|
||||
};
|
||||
|
||||
|
||||
void hwSleep(unsigned long ms);
|
||||
bool hwSleep(uint8_t interrupt, uint8_t mode, unsigned long ms);
|
||||
uint8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms);
|
||||
#ifdef MY_DEBUG
|
||||
void hwDebugPrint(const char *fmt, ... );
|
||||
#endif
|
||||
void hwInternalSleep(unsigned long ms);
|
||||
|
||||
#endif // #ifdef ARDUINO_ARCH_AVR
|
||||
#endif
|
||||
|
||||
@@ -17,10 +17,8 @@
|
||||
* version 2 as published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
|
||||
#include "MyHwESP8266.h"
|
||||
#include <EEPROM.h>
|
||||
#include "drivers/ESP8266/EEPROM/EEPROM.cpp"
|
||||
|
||||
/*
|
||||
int8_t pinIntTrigger = 0;
|
||||
@@ -54,7 +52,7 @@ static void hwInitConfigBlock( size_t length = 1024 /*ATMega328 has 1024 bytes*/
|
||||
|
||||
void hwReadConfigBlock(void* buf, void* adr, size_t length)
|
||||
{
|
||||
hw_initConfigBlock();
|
||||
hwInitConfigBlock();
|
||||
uint8_t* dst = static_cast<uint8_t*>(buf);
|
||||
int offs = reinterpret_cast<int>(adr);
|
||||
while (length-- > 0)
|
||||
@@ -65,7 +63,7 @@ void hwReadConfigBlock(void* buf, void* adr, size_t length)
|
||||
|
||||
void hwWriteConfigBlock(void* buf, void* adr, size_t length)
|
||||
{
|
||||
hw_initConfigBlock();
|
||||
hwInitConfigBlock();
|
||||
uint8_t* src = static_cast<uint8_t*>(buf);
|
||||
int offs = reinterpret_cast<int>(adr);
|
||||
while (length-- > 0)
|
||||
@@ -78,16 +76,16 @@ void hwWriteConfigBlock(void* buf, void* adr, size_t length)
|
||||
uint8_t hwReadConfig(int adr)
|
||||
{
|
||||
uint8_t value;
|
||||
hw_readConfigBlock(&value, reinterpret_cast<void*>(adr), 1);
|
||||
hwReadConfigBlock(&value, reinterpret_cast<void*>(adr), 1);
|
||||
return value;
|
||||
}
|
||||
|
||||
void hwWriteConfig(int adr, uint8_t value)
|
||||
{
|
||||
uint8_t curr = hw_readConfig(adr);
|
||||
uint8_t curr = hwReadConfig(adr);
|
||||
if (curr != value)
|
||||
{
|
||||
hw_writeConfigBlock(&value, reinterpret_cast<void*>(adr), 1);
|
||||
hwWriteConfigBlock(&value, reinterpret_cast<void*>(adr), 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,7 +107,7 @@ uint8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t m
|
||||
}
|
||||
|
||||
#ifdef MY_DEBUG
|
||||
void hwDebugPrint(bool isGW, const char *fmt, ... ) {
|
||||
void hwDebugPrint(const char *fmt, ... ) {
|
||||
char fmtBuffer[300];
|
||||
#ifdef MY_GATEWAY_FEATURE
|
||||
// prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
|
||||
@@ -134,5 +132,3 @@ void hwDebugPrint(bool isGW, const char *fmt, ... ) {
|
||||
//Serial.write(freeRam());
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // #ifdef ARDUINO_ARCH_ESP8266
|
||||
|
||||
@@ -16,25 +16,23 @@
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* version 2 as published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
|
||||
#ifndef MyHwESP8266_h
|
||||
#define MyHwESP8266_h
|
||||
|
||||
#include "MyHw.h"
|
||||
#include "MyConfig.h"
|
||||
#include "MyMessage.h"
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include <Arduino.h>
|
||||
#include <SPI.h>
|
||||
//#include <SPI.h>
|
||||
#endif
|
||||
|
||||
// Define these as macros to save valuable space
|
||||
|
||||
#define hwDigitalWrite(__pin, __value) (digitalWrite(__pin, __value))
|
||||
#define hwInit() Serial.begin(BAUD_RATE)
|
||||
#define hwInit() Serial.begin(MY_BAUD_RATE)
|
||||
#define hwWatchdogReset() wdt_reset()
|
||||
#define hwReboot() wdt_enable(WDTO_15MS); while (1)
|
||||
#define hwMillis() millis()
|
||||
@@ -44,28 +42,5 @@ void hwWriteConfigBlock(void* buf, void* adr, size_t length);
|
||||
void hwWriteConfig(int adr, uint8_t value);
|
||||
uint8_t hwReadConfig(int adr);
|
||||
|
||||
enum period_t
|
||||
{
|
||||
SLEEP_15Ms,
|
||||
SLEEP_30MS,
|
||||
SLEEP_60MS,
|
||||
SLEEP_120MS,
|
||||
SLEEP_250MS,
|
||||
SLEEP_500MS,
|
||||
SLEEP_1S,
|
||||
SLEEP_2S,
|
||||
SLEEP_4S,
|
||||
SLEEP_8S,
|
||||
SLEEP_FOREVER
|
||||
};
|
||||
|
||||
|
||||
|
||||
void hwSleep(unsigned long ms);
|
||||
bool hwSleep(uint8_t interrupt, uint8_t mode, unsigned long ms);
|
||||
uint8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms);
|
||||
#ifdef MY_DEBUG
|
||||
void hwDebugPrint(const char *fmt, ... );
|
||||
#endif
|
||||
|
||||
#endif // #ifdef ARDUINO_ARCH_ESP8266
|
||||
|
||||
@@ -22,18 +22,12 @@
|
||||
|
||||
#include "MyMessage.h"
|
||||
|
||||
class MyProtocol
|
||||
{
|
||||
public:
|
||||
// MyProtocol constructor
|
||||
MyProtocol();
|
||||
// parse(message, inputString)
|
||||
// parse a string into a message element
|
||||
// returns true if successfully parsed the input string
|
||||
virtual bool parse(MyMessage &message, char *inputString) = 0;
|
||||
// parse(message, inputString)
|
||||
// parse a string into a message element
|
||||
// returns true if successfully parsed the input string
|
||||
bool protocolParse(MyMessage &message, char *inputString);
|
||||
|
||||
// Format MyMessage to the protocol represenataion
|
||||
virtual char *format(MyMessage &message) = 0;
|
||||
// Format MyMessage to the protocol represenataion
|
||||
char *protocolFormat(MyMessage &message);
|
||||
|
||||
};
|
||||
#endif
|
||||
|
||||
@@ -21,9 +21,10 @@
|
||||
#include "MyProtocolDefault.h"
|
||||
#include "MyTransport.h"
|
||||
|
||||
MyProtocolDefault::MyProtocolDefault() : MyProtocol() {}
|
||||
char fmtBuffer[MAX_FORMAT_BUFFER_LENGTH];
|
||||
char convBuffer[MAX_PAYLOAD*2+1];
|
||||
|
||||
bool MyProtocolDefault::parse(MyMessage &message, char *inputString) {
|
||||
bool protocolParse(MyMessage &message, char *inputString) {
|
||||
char *str, *p, *value=NULL;
|
||||
uint8_t bvalue[MAX_PAYLOAD];
|
||||
uint8_t blen = 0;
|
||||
@@ -58,8 +59,8 @@ bool MyProtocolDefault::parse(MyMessage &message, char *inputString) {
|
||||
blen = 0;
|
||||
uint8_t val;
|
||||
while (*str) {
|
||||
val = h2i(*str++) << 4;
|
||||
val += h2i(*str++);
|
||||
val = protocolH2i(*str++) << 4;
|
||||
val += protocolH2i(*str++);
|
||||
bvalue[blen] = val;
|
||||
blen++;
|
||||
}
|
||||
@@ -89,13 +90,12 @@ bool MyProtocolDefault::parse(MyMessage &message, char *inputString) {
|
||||
return true;
|
||||
}
|
||||
|
||||
char * MyProtocolDefault::format(MyMessage &message) {
|
||||
char * protocolFormat(MyMessage &message) {
|
||||
snprintf_P(fmtBuffer, MAX_FORMAT_BUFFER_LENGTH, PSTR("%d;%d;%d;%d;%d;%s\n"), message.sender, message.sensor, mGetCommand(message), mGetAck(message), message.type, message.getString(convBuffer));
|
||||
return fmtBuffer;
|
||||
}
|
||||
|
||||
|
||||
uint8_t MyProtocolDefault::h2i(char c) {
|
||||
uint8_t protocolH2i(char c) {
|
||||
uint8_t i = 0;
|
||||
if (c <= '9')
|
||||
i += c - '0';
|
||||
@@ -105,3 +105,5 @@ uint8_t MyProtocolDefault::h2i(char c) {
|
||||
i += c - 'A' + 10;
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -21,22 +21,10 @@
|
||||
#define MyProtocolDefault_h
|
||||
|
||||
#include "MyConfig.h"
|
||||
#include "MyProtocol.h"
|
||||
#include "MyTransport.h"
|
||||
|
||||
#define MAX_FORMAT_BUFFER_LENGTH 120 // Max buffersize when formatting messages
|
||||
|
||||
uint8_t protocolH2i(char c);
|
||||
|
||||
class MyProtocolDefault : public MyProtocol
|
||||
{
|
||||
public:
|
||||
MyProtocolDefault();
|
||||
bool parse(MyMessage &message, char *inputString);
|
||||
char *format(MyMessage &message);
|
||||
private:
|
||||
char fmtBuffer[MAX_FORMAT_BUFFER_LENGTH];
|
||||
char convBuffer[MAX_PAYLOAD*2+1];
|
||||
|
||||
uint8_t h2i(char c);
|
||||
};
|
||||
#endif
|
||||
|
||||
@@ -109,7 +109,7 @@ bool isValidFirmware() {
|
||||
// init crc
|
||||
uint16_t crc = ~0;
|
||||
for (uint16_t i = 0; i < _fc.blocks * FIRMWARE_BLOCK_SIZE; ++i) {
|
||||
crc ^= flash.readByte(i + FIRMWARE_START_OFFSET);
|
||||
crc ^= _flash.readByte(i + FIRMWARE_START_OFFSET);
|
||||
for (int8_t j = 0; j < 8; ++j) {
|
||||
if (crc & 1)
|
||||
crc = (crc >> 1) ^ 0xA001;
|
||||
@@ -217,7 +217,7 @@ void begin() {
|
||||
_failedTransmissions = 0;
|
||||
|
||||
// Setup radio
|
||||
if (!transport.init()) {
|
||||
if (!transportInit()) {
|
||||
debug(PSTR("radio init fail\n"));
|
||||
while(1); // Nothing more we can do
|
||||
}
|
||||
@@ -311,7 +311,7 @@ ControllerConfig getConfig() {
|
||||
#if defined(MY_RADIO_FEATURE)
|
||||
void requestNodeId() {
|
||||
debug(PSTR("req id\n"));
|
||||
transport.setAddress(_nc.nodeId);
|
||||
transportSetAddress(_nc.nodeId);
|
||||
build(_msg, _nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_ID_REQUEST, false).set("");
|
||||
sendWrite(_nc.parentNodeId, _msg);
|
||||
wait(2000);
|
||||
@@ -323,7 +323,7 @@ void requestNodeId() {
|
||||
|
||||
void setupNode() {
|
||||
// Open reading pipe for messages directed to this node (set write pipe to same)
|
||||
transport.setAddress(_nc.nodeId);
|
||||
transportSetAddress(_nc.nodeId);
|
||||
// Present node and request config
|
||||
#ifndef MY_GATEWAY_FEATURE
|
||||
if (_nc.nodeId != AUTO) {
|
||||
@@ -563,7 +563,7 @@ boolean sendWrite(uint8_t to, MyMessage &message) {
|
||||
#ifdef MY_LEDS_BLINKING_FEATURE
|
||||
txBlink(1);
|
||||
#endif
|
||||
bool ok = transport.send(to, &message, min(MAX_MESSAGE_LENGTH, HEADER_SIZE + length));
|
||||
bool ok = transportSend(to, &message, min(MAX_MESSAGE_LENGTH, HEADER_SIZE + length));
|
||||
|
||||
debug(PSTR("send: %d-%d-%d-%d s=%d,c=%d,t=%d,pt=%d,l=%d,sg=%d,st=%s:%s\n"),
|
||||
message.sender,message.last, to, message.destination, message.sensor, mGetCommand(message), message.type,
|
||||
@@ -660,7 +660,7 @@ void process() {
|
||||
|
||||
#if defined(MY_RADIO_FEATURE)
|
||||
uint8_t to = 0;
|
||||
if (!transport.available(&to))
|
||||
if (!transportAvailable(&to))
|
||||
{
|
||||
#ifdef MY_OTA_FIRMWARE_FEATURE
|
||||
unsigned long enter = hwMillis();
|
||||
@@ -692,7 +692,7 @@ void process() {
|
||||
(void)signer.checkTimer(); // Manage signing timeout
|
||||
#endif
|
||||
|
||||
uint8_t len = transport.receive((uint8_t *)&_msg);
|
||||
uint8_t len = transportReceive((uint8_t *)&_msg);
|
||||
(void)len; //until somebody makes use of 'len'
|
||||
#ifdef MY_LEDS_BLINKING_FEATURE
|
||||
rxBlink(1);
|
||||
@@ -842,14 +842,14 @@ void process() {
|
||||
// copy new FW config
|
||||
memcpy(&_fc,firmwareConfigResponse,sizeof(NodeFirmwareConfig));
|
||||
// Init flash
|
||||
if (!flash.initialize()) {
|
||||
if (!_flash.initialize()) {
|
||||
debug(PSTR("flash init fail\n"));
|
||||
_fwUpdateOngoing = false;
|
||||
} else {
|
||||
// erase lower 32K -> max flash size for ATMEGA328
|
||||
flash.blockErase32K(0);
|
||||
_flash.blockErase32K(0);
|
||||
// wait until flash erased
|
||||
while ( flash.busy() );
|
||||
while ( _flash.busy() );
|
||||
_fwBlock = _fc.blocks;
|
||||
_fwUpdateOngoing = true;
|
||||
// reset flags
|
||||
@@ -865,9 +865,9 @@ void process() {
|
||||
// extract FW block
|
||||
ReplyFWBlock *firmwareResponse = (ReplyFWBlock *)_msg.data;
|
||||
// write to flash
|
||||
flash.writeBytes( ((_fwBlock - 1) * FIRMWARE_BLOCK_SIZE) + FIRMWARE_START_OFFSET, firmwareResponse->data, FIRMWARE_BLOCK_SIZE);
|
||||
_flash.writeBytes( ((_fwBlock - 1) * FIRMWARE_BLOCK_SIZE) + FIRMWARE_START_OFFSET, firmwareResponse->data, FIRMWARE_BLOCK_SIZE);
|
||||
// wait until flash written
|
||||
while ( flash.busy() );
|
||||
while ( _flash.busy() );
|
||||
_fwBlock--;
|
||||
if (!_fwBlock) {
|
||||
// We're finished! Do a checksum and reboot.
|
||||
@@ -877,7 +877,7 @@ void process() {
|
||||
// All seems ok, write size and signature to flash (DualOptiboot will pick this up and flash it)
|
||||
uint16_t fwsize = FIRMWARE_BLOCK_SIZE * _fc.blocks;
|
||||
uint8_t OTAbuffer[10] = {'F','L','X','I','M','G',':',(fwsize >> 8),fwsize,':'};
|
||||
flash.writeBytes(0, OTAbuffer, 10);
|
||||
_flash.writeBytes(0, OTAbuffer, 10);
|
||||
// Write the new firmware config to eeprom
|
||||
hwWriteConfigBlock((void*)&_fc, (void*)EEPROM_FIRMWARE_TYPE_ADDRESS, sizeof(NodeFirmwareConfig));
|
||||
hwReboot();
|
||||
@@ -986,7 +986,7 @@ void sleep(unsigned long ms) {
|
||||
} else {
|
||||
#endif
|
||||
#if defined(MY_RADIO_FEATURE)
|
||||
transport.powerDown();
|
||||
transportPowerDown();
|
||||
#endif
|
||||
hwSleep(ms);
|
||||
#if defined(MY_OTA_FIRMWARE_FEATURE)
|
||||
@@ -1003,7 +1003,7 @@ bool sleep(uint8_t interrupt, uint8_t mode, unsigned long ms) {
|
||||
} else {
|
||||
#endif
|
||||
#if defined(MY_RADIO_FEATURE)
|
||||
transport.powerDown();
|
||||
transportPowerDown();
|
||||
#endif
|
||||
return hwSleep(interrupt, mode, ms) ;
|
||||
#if defined(MY_OTA_FIRMWARE_FEATURE)
|
||||
@@ -1020,7 +1020,7 @@ int8_t sleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode
|
||||
} else {
|
||||
#endif
|
||||
#if defined(MY_RADIO_FEATURE)
|
||||
transport.powerDown();
|
||||
transportPowerDown();
|
||||
#endif
|
||||
return hwSleep(interrupt1, mode1, interrupt2, mode2, ms) ;
|
||||
#if defined(MY_OTA_FIRMWARE_FEATURE)
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
|
||||
#include "Version.h" // Auto generated by bot
|
||||
#include "MyConfig.h"
|
||||
#include "MyTransport.h"
|
||||
#ifdef MY_SIGNING_FEATURE
|
||||
#include "MySigning.h"
|
||||
#include "MySigningNone.h"
|
||||
|
||||
@@ -29,37 +29,5 @@
|
||||
#define GATEWAY_ADDRESS ((uint8_t)0)
|
||||
#define BROADCAST_ADDRESS ((uint8_t)0xFF)
|
||||
|
||||
class MyTransport
|
||||
{
|
||||
public:
|
||||
// MyTransport constructor
|
||||
// different parameters would be needed depending on radio module (e.g. pins connected etc.)
|
||||
// keeping these parameters as #define's in MyConfig to streamline the driver interface
|
||||
MyTransport();
|
||||
// init()
|
||||
// different parameters would be needed depending on radio module (e.g. channel, power, crc, etc.)
|
||||
// keeping these parameters as #define's in MyConfig to streamline the driver interface
|
||||
virtual bool init() = 0;
|
||||
// setAddress(address)
|
||||
// set the node address and start listening to that address (and the broadcast address)
|
||||
virtual void setAddress(uint8_t address) = 0;
|
||||
// getAddress()
|
||||
// returns the current address from the radio driver
|
||||
virtual uint8_t getAddress() = 0;
|
||||
// send(to, data, len)
|
||||
// reliable transmission of the data with given length (in bytes) to the destination address
|
||||
// returns true if successfully submitted
|
||||
virtual bool send(uint8_t to, const void* data, uint8_t len) = 0;
|
||||
// available(to)
|
||||
// returns true if a new packet arrived in the rx buffer
|
||||
// populates "to" parameter with the address the packet was sent to (either own address or broadcast)
|
||||
virtual bool available(uint8_t *to) = 0;
|
||||
// receive(data)
|
||||
// returns length of received packet
|
||||
// populates "data" parameter with received data
|
||||
virtual uint8_t receive(void* data) = 0;
|
||||
// powers down the radio
|
||||
virtual void powerDown() = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -17,76 +17,72 @@
|
||||
* version 2 as published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#include "MyTransport.h"
|
||||
#include "MyTransportNRF24.h"
|
||||
|
||||
MyTransportNRF24::MyTransportNRF24()
|
||||
:
|
||||
MyTransport(),
|
||||
rf24(MY_RF24_CE_PIN, MY_RF24_CS_PIN)
|
||||
{
|
||||
}
|
||||
RF24 _rf24(MY_RF24_CE_PIN, MY_RF24_CS_PIN);
|
||||
uint8_t _address;
|
||||
uint8_t _paLevel;
|
||||
|
||||
bool MyTransportNRF24::init() {
|
||||
bool transportInit() {
|
||||
// Start up the radio library
|
||||
rf24.begin();
|
||||
_rf24.begin();
|
||||
|
||||
if (!rf24.isPVariant()) {
|
||||
if (!_rf24.isPVariant()) {
|
||||
return false;
|
||||
}
|
||||
rf24.setAutoAck(1);
|
||||
rf24.setAutoAck(BROADCAST_PIPE,false); // Turn off auto ack for broadcast
|
||||
rf24.enableAckPayload();
|
||||
rf24.setChannel(MY_RF24_CHANNEL);
|
||||
rf24.setPALevel(_paLevel);
|
||||
rf24.setDataRate(MY_RF24_DATARATE);
|
||||
rf24.setRetries(5,15);
|
||||
rf24.setCRCLength(RF24_CRC_16);
|
||||
rf24.enableDynamicPayloads();
|
||||
_rf24.setAutoAck(1);
|
||||
_rf24.setAutoAck(BROADCAST_PIPE,false); // Turn off auto ack for broadcast
|
||||
_rf24.enableAckPayload();
|
||||
_rf24.setChannel(MY_RF24_CHANNEL);
|
||||
_rf24.setPALevel(_paLevel);
|
||||
_rf24.setDataRate(MY_RF24_DATARATE);
|
||||
_rf24.setRetries(5,15);
|
||||
_rf24.setCRCLength(RF24_CRC_16);
|
||||
_rf24.enableDynamicPayloads();
|
||||
|
||||
// All nodes listen to broadcast pipe (for FIND_PARENT_RESPONSE messages)
|
||||
rf24.openReadingPipe(BROADCAST_PIPE, TO_ADDR(BROADCAST_ADDRESS));
|
||||
_rf24.openReadingPipe(BROADCAST_PIPE, TO_ADDR(BROADCAST_ADDRESS));
|
||||
return true;
|
||||
}
|
||||
|
||||
void MyTransportNRF24::setAddress(uint8_t address) {
|
||||
void transportSetAddress(uint8_t address) {
|
||||
_address = address;
|
||||
rf24.openReadingPipe(WRITE_PIPE, TO_ADDR(address));
|
||||
rf24.openReadingPipe(CURRENT_NODE_PIPE, TO_ADDR(address));
|
||||
rf24.startListening();
|
||||
_rf24.openReadingPipe(WRITE_PIPE, TO_ADDR(address));
|
||||
_rf24.openReadingPipe(CURRENT_NODE_PIPE, TO_ADDR(address));
|
||||
_rf24.startListening();
|
||||
}
|
||||
|
||||
uint8_t MyTransportNRF24::getAddress() {
|
||||
uint8_t transportGetAddress() {
|
||||
return _address;
|
||||
}
|
||||
|
||||
bool MyTransportNRF24::send(uint8_t to, const void* data, uint8_t len) {
|
||||
bool transportSend(uint8_t to, const void* data, uint8_t len) {
|
||||
// Make sure radio has powered up
|
||||
rf24.powerUp();
|
||||
rf24.stopListening();
|
||||
rf24.openWritingPipe(TO_ADDR(to));
|
||||
bool ok = rf24.write(data, len, to == BROADCAST_ADDRESS);
|
||||
rf24.startListening();
|
||||
_rf24.powerUp();
|
||||
_rf24.stopListening();
|
||||
_rf24.openWritingPipe(TO_ADDR(to));
|
||||
bool ok = _rf24.write(data, len, to == BROADCAST_ADDRESS);
|
||||
_rf24.startListening();
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool MyTransportNRF24::available(uint8_t *to) {
|
||||
bool transportAvailable(uint8_t *to) {
|
||||
uint8_t pipe = 255;
|
||||
boolean avail = rf24.available(&pipe);
|
||||
boolean avail = _rf24.available(&pipe);
|
||||
(void)avail; //until somebody makes use of 'avail'
|
||||
if (pipe == CURRENT_NODE_PIPE)
|
||||
*to = _address;
|
||||
else if (pipe == BROADCAST_PIPE)
|
||||
*to = BROADCAST_ADDRESS;
|
||||
return (rf24.available() && pipe < 6);
|
||||
return (_rf24.available() && pipe < 6);
|
||||
}
|
||||
|
||||
uint8_t MyTransportNRF24::receive(void* data) {
|
||||
uint8_t len = rf24.getDynamicPayloadSize();
|
||||
rf24.read(data, len);
|
||||
uint8_t transportReceive(void* data) {
|
||||
uint8_t len = _rf24.getDynamicPayloadSize();
|
||||
_rf24.read(data, len);
|
||||
return len;
|
||||
}
|
||||
|
||||
void MyTransportNRF24::powerDown() {
|
||||
rf24.powerDown();
|
||||
void transportPowerDown() {
|
||||
_rf24.powerDown();
|
||||
}
|
||||
|
||||
@@ -32,21 +32,12 @@
|
||||
#define CURRENT_NODE_PIPE ((uint8_t)1)
|
||||
#define BROADCAST_PIPE ((uint8_t)2)
|
||||
|
||||
class MyTransportNRF24 : public MyTransport
|
||||
{
|
||||
public:
|
||||
MyTransportNRF24();
|
||||
bool init();
|
||||
void setAddress(uint8_t address);
|
||||
uint8_t getAddress();
|
||||
bool send(uint8_t to, const void* data, uint8_t len);
|
||||
bool available(uint8_t *to);
|
||||
uint8_t receive(void* data);
|
||||
void powerDown();
|
||||
private:
|
||||
RF24 rf24;
|
||||
uint8_t _address;
|
||||
uint8_t _paLevel;
|
||||
};
|
||||
bool transportInit();
|
||||
void transportSetAddress(uint8_t address);
|
||||
uint8_t transportGetAddress();
|
||||
bool transportSend(uint8_t to, const void* data, uint8_t len);
|
||||
bool transportAvailable(uint8_t *to);
|
||||
uint8_t transportReceive(void* data);
|
||||
void transportPowerDown();
|
||||
|
||||
#endif
|
||||
|
||||
@@ -20,52 +20,49 @@
|
||||
#include "MyTransport.h"
|
||||
#include "MyTransportRFM69.h"
|
||||
|
||||
MyTransportRFM69::MyTransportRFM69()
|
||||
:
|
||||
MyTransport(),
|
||||
radio(MY_RF69_SPI_CS, MY_RF69_IRQ_PIN, MY_IS_RFM69HW, MY_RF69_IRQ_NUM)
|
||||
{
|
||||
}
|
||||
RFM69 _radio(MY_RF69_SPI_CS, MY_RF69_IRQ_PIN, MY_IS_RFM69HW, MY_RF69_IRQ_NUM);
|
||||
uint8_t _address;
|
||||
|
||||
bool MyTransportRFM69::init() {
|
||||
|
||||
bool transportInit() {
|
||||
// Start up the radio library (_address will be set later by the MySensors library)
|
||||
radio.initialize(MY_RFM69_FREQUENCY, _address, MY_RFM69_NETWORKID);
|
||||
_radio.initialize(MY_RFM69_FREQUENCY, _address, MY_RFM69_NETWORKID);
|
||||
#ifdef MY_RFM69_ENABLE_ENCRYPTION
|
||||
radio.encrypt(RFM69_ENCRYPTKEY);
|
||||
_radio.encrypt(RFM69_ENCRYPTKEY);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
void MyTransportRFM69::setAddress(uint8_t address) {
|
||||
void transportSetAddress(uint8_t address) {
|
||||
_address = address;
|
||||
radio.setAddress(address);
|
||||
_radio.setAddress(address);
|
||||
}
|
||||
|
||||
uint8_t MyTransportRFM69::getAddress() {
|
||||
uint8_t transportGetAddress() {
|
||||
return _address;
|
||||
}
|
||||
|
||||
bool MyTransportRFM69::send(uint8_t to, const void* data, uint8_t len) {
|
||||
return radio.sendWithRetry(to,data,len);
|
||||
bool transportSend(uint8_t to, const void* data, uint8_t len) {
|
||||
return _radio.sendWithRetry(to,data,len);
|
||||
}
|
||||
|
||||
bool MyTransportRFM69::available(uint8_t *to) {
|
||||
if (radio.TARGETID == BROADCAST_ADDRESS)
|
||||
bool transportAvailable(uint8_t *to) {
|
||||
if (_radio.TARGETID == BROADCAST_ADDRESS)
|
||||
*to = BROADCAST_ADDRESS;
|
||||
else
|
||||
*to = _address;
|
||||
return radio.receiveDone();
|
||||
return _radio.receiveDone();
|
||||
}
|
||||
|
||||
uint8_t MyTransportRFM69::receive(void* data) {
|
||||
memcpy(data,(const void *)radio.DATA, radio.DATALEN);
|
||||
uint8_t transportReceive(void* data) {
|
||||
memcpy(data,(const void *)_radio.DATA, _radio.DATALEN);
|
||||
// Send ack back if this message wasn't a broadcast
|
||||
if (radio.TARGETID != RF69_BROADCAST_ADDR)
|
||||
radio.ACKRequested();
|
||||
radio.sendACK();
|
||||
return radio.DATALEN;
|
||||
if (_radio.TARGETID != RF69_BROADCAST_ADDR)
|
||||
_radio.ACKRequested();
|
||||
_radio.sendACK();
|
||||
return _radio.DATALEN;
|
||||
}
|
||||
|
||||
void MyTransportRFM69::powerDown() {
|
||||
radio.sleep();
|
||||
void transportPowerDown() {
|
||||
_radio.sleep();
|
||||
}
|
||||
|
||||
@@ -27,20 +27,12 @@
|
||||
#include "drivers/SPI/SPI.h"
|
||||
|
||||
|
||||
class MyTransportRFM69 : public MyTransport
|
||||
{
|
||||
public:
|
||||
MyTransportRFM69();
|
||||
bool init();
|
||||
void setAddress(uint8_t address);
|
||||
uint8_t getAddress();
|
||||
bool send(uint8_t to, const void* data, uint8_t len);
|
||||
bool available(uint8_t *to);
|
||||
uint8_t receive(void* data);
|
||||
void powerDown();
|
||||
private:
|
||||
RFM69 radio;
|
||||
uint8_t _address;
|
||||
};
|
||||
bool transportInit();
|
||||
void transportSetAddress(uint8_t address);
|
||||
uint8_t transportGetAddress();
|
||||
bool transportSend(uint8_t to, const void* data, uint8_t len);
|
||||
bool transportAvailable(uint8_t *to);
|
||||
uint8_t transportReceive(void* data);
|
||||
void transportPowerDown();
|
||||
|
||||
#endif
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user