Skip to content

Commit

Permalink
Code cleanup (#787)
Browse files Browse the repository at this point in the history
* Cleanup Core

* SAMD adjustments
  • Loading branch information
tekka007 authored and henrikekblad committed Mar 15, 2017
1 parent 481bd36 commit 61a415c
Show file tree
Hide file tree
Showing 44 changed files with 163 additions and 149 deletions.
48 changes: 32 additions & 16 deletions MyConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,35 @@
* Serial and debug options
***********************************/

// Enable MY_DEBUG in sketch to show debug prints. This option will add a lot to the size of the
// final sketch but is helpful to see what is actually is happening during development
/**
* @def MY_DEBUG
* @brief Enable MY_DEBUG in sketch to show debug prints. This option will add a lot to the size of the
* final sketch but is helpful to see what is actually is happening during development
*/
//#define MY_DEBUG

// Enable MY_SPECIAL_DEBUG in sketch to activate I_DEBUG messages if MY_DEBUG is disabled.
// I_DEBUG requests are:
// R: routing info (only repeaters): received msg XXYY (as stream), where XX is the node and YY the routing node
// V: CPU voltage
// F: CPU frequency
// M: free memory
// E: clear MySensors EEPROM area and reboot (i.e. "factory" reset)
/**
* @def MY_SPECIAL_DEBUG
* @brief Enable MY_SPECIAL_DEBUG in sketch to activate I_DEBUG messages if MY_DEBUG is disabled.
* I_DEBUG requests are:
* R: routing info (only repeaters): received msg XXYY (as stream), where XX is the node and YY the routing node
* V: CPU voltage
* F: CPU frequency
* M: free memory
* E: clear MySensors EEPROM area and reboot (i.e. "factory" reset)
*/
//#define MY_SPECIAL_DEBUG

// Enable MY_DEBUG_VERBOSE_SIGNING flag for verbose debug prints related to signing.
// Requires DEBUG to be enabled.
// This will add even more to the size of the final sketch!
//#define MY_DEBUG_VERBOSE_SIGNING

// Enable this in sketch if you want to use TX(1), RX(0) as normal I/O pin
/**
* @def MY_DISABLED_SERIAL
* @brief Enable MY_DISABLED_SERIAL in sketch if you want to use TX(1), RX(0) as normal I/O pins
*/
//#define MY_DISABLED_SERIAL

// Enable MY_CORE_ONLY flag if you want to use core functions without loading the framework
/**
* @def MY_CORE_ONLY
* @brief Enable MY_CORE_ONLY flag if you want to use core functions without loading the framework
*/
//#define MY_CORE_ONLY

// Turn off debug if serial pins is used for other stuff
Expand Down Expand Up @@ -358,6 +365,12 @@
/**********************************
* Message Signing Settings
***********************************/
/**
* @def Enable MY_DEBUG_VERBOSE_SIGNING
* @brief Flag for verbose debug prints related to signing. Requires DEBUG to be enabled. This will add even more to the size of the final sketch!
*/
//#define MY_DEBUG_VERBOSE_SIGNING

/**
* @def MY_SIGNING_ATSHA204
* @brief Enables HW backed signing functionality in library.
Expand Down Expand Up @@ -945,6 +958,9 @@
// Doxygen specific constructs, not included when built normally
// This is used to enable disabled macros/definitions to be included in the documentation as well.
#if DOXYGEN
#define MY_DEBUG
#define MY_SPECIAL_DEBUG
#define MY_DISABLED_SERIAL
#define MY_SIGNING_ATSHA204
#define MY_SIGNING_SOFT
#define MY_SIGNING_REQUEST_SIGNATURES
Expand Down
8 changes: 7 additions & 1 deletion core/MyCapabilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@
#define MY_CAP_RXBUF "-"
#endif

#if defined(MY_RF24_ENABLE_ENCRYPTION) || defined(MY_RFM69_ENABLE_ENCRYPTION)
#define MY_CAP_ENCR "X"
#else
#define MY_CAP_ENCR "-"
#endif


#define MY_CAPABILITIES MY_CAP_RESET MY_CAP_RADIO MY_CAP_OTA_FW MY_CAP_TYPE MY_CAP_ARCH MY_CAP_SIGN MY_CAP_RXBUF
#define MY_CAPABILITIES MY_CAP_RESET MY_CAP_RADIO MY_CAP_OTA_FW MY_CAP_TYPE MY_CAP_ARCH MY_CAP_SIGN MY_CAP_RXBUF MY_CAP_ENCR

#endif /* MyCapabilities_h */
2 changes: 1 addition & 1 deletion core/MyEepromAddresses.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2016 Sensnology AB
* Copyright (C) 2013-2017 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
Expand Down
27 changes: 13 additions & 14 deletions core/MyGatewayTransportEthernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,28 +138,27 @@ bool gatewayTransportInit(void)
#endif /* MY_GATEWAY_ESP8266 */

#if defined(MY_GATEWAY_CLIENT_MODE)
#if defined(MY_CONTROLLER_URL_ADDRESS)
if (client.connect(MY_CONTROLLER_URL_ADDRESS, MY_PORT)) {
#if defined(MY_CONTROLLER_URL_ADDRESS)
if (client.connect(MY_CONTROLLER_URL_ADDRESS, MY_PORT)) {
#else
if (client.connect(_ethernetControllerIP, MY_PORT)) {
if (client.connect(_ethernetControllerIP, MY_PORT)) {
#endif
debug(PSTR("Eth: connect\n"));
_w5100_spi_en(false);
gatewayTransportSend(buildGw(_msgTmp, I_GATEWAY_READY).set(MSG_GW_STARTUP_COMPLETE));
_w5100_spi_en(true);
presentNode();
}
else {
client.stop();
debug(PSTR("Eth: Failed to connect\n"));
}
debug(PSTR("Eth: connect\n"));
_w5100_spi_en(false);
gatewayTransportSend(buildGw(_msgTmp, I_GATEWAY_READY).set(MSG_GW_STARTUP_COMPLETE));
_w5100_spi_en(true);
presentNode();
} else {
client.stop();
debug(PSTR("Eth: Failed to connect\n"));
}
#elif defined(MY_USE_UDP)
_ethernetServer.begin(_ethernetGatewayPort);
#else
#if defined(MY_GATEWAY_LINUX) && defined(MY_IP_ADDRESS)
_ethernetServer.begin(_ethernetGatewayIP);
#else
// we have to use pointers due to the constructor of EthernetServer
// we have to use pointers due to the constructor of EthernetServer
_ethernetServer.begin();
#endif
#endif /* USE_UDP */
Expand Down
2 changes: 1 addition & 1 deletion core/MyGatewayTransportMQTTClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2016 Sensnology AB
* Copyright (C) 2013-2017 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
Expand Down
2 changes: 1 addition & 1 deletion core/MyGatewayTransportSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2016 Sensnology AB
* Copyright (C) 2013-2017 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
Expand Down
2 changes: 1 addition & 1 deletion core/MyHw.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2015 Sensnology AB
* Copyright (C) 2013-2017 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
Expand Down
28 changes: 14 additions & 14 deletions core/MyHwAVR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2015 Sensnology AB
* Copyright (C) 2013-2017 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
Expand All @@ -17,10 +17,17 @@
* version 2 as published by the Free Software Foundation.
*/

#ifdef ARDUINO_ARCH_AVR

#include "MyHwAVR.h"

bool hwInit(void)
{
#if !defined(MY_DISABLED_SERIAL)
MY_SERIALDEVICE.begin(MY_BAUD_RATE);
#endif
return true;
}


#define INVALID_INTERRUPT_NUM (0xFFu)

volatile uint8_t _wokeUpByInterrupt =
Expand Down Expand Up @@ -197,14 +204,13 @@ int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mo
return ret;
}


bool hwUniqueID(unique_id_t* uniqueID)
{
// not implemented yet
(void)uniqueID;
return false;
}
#if defined(MY_DEBUG) || defined(MY_SPECIAL_DEBUG)

uint16_t hwCPUVoltage()
{
// Measure Vcc against 1.1V Vref
Expand Down Expand Up @@ -261,18 +267,17 @@ uint16_t hwFreeMem()
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
#endif

#ifdef MY_DEBUG
void hwDebugPrint(const char *fmt, ... )
{
char fmtBuffer[MY_SERIAL_OUTPUT_SIZE];
#ifdef MY_GATEWAY_FEATURE
// prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
snprintf_P(fmtBuffer, sizeof(fmtBuffer), PSTR("0;255;%d;0;%d;"), C_INTERNAL, I_LOG_MESSAGE);
snprintf_P(fmtBuffer, sizeof(fmtBuffer), PSTR("0;255;%d;0;%d;%lu "), C_INTERNAL, I_LOG_MESSAGE,
hwMillis());
MY_SERIALDEVICE.print(fmtBuffer);
#else
// prepend timestamp (AVR nodes)
// prepend timestamp
MY_SERIALDEVICE.print(hwMillis());
MY_SERIALDEVICE.print(" ");
#endif
Expand All @@ -289,9 +294,4 @@ void hwDebugPrint(const char *fmt, ... )
va_end (args);
MY_SERIALDEVICE.print(fmtBuffer);
MY_SERIALDEVICE.flush();

//MY_SERIALDEVICE.write(freeRam());
}
#endif

#endif // #ifdef ARDUINO_ARCH_AVR
29 changes: 2 additions & 27 deletions core/MyHwAVR.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2015 Sensnology AB
* Copyright (C) 2013-2017 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
Expand Down Expand Up @@ -38,35 +38,12 @@
#define MY_SERIALDEVICE Serial
#endif

#if defined __AVR_ATmega328P__
#ifndef sleep_bod_disable
#define sleep_bod_disable() \
do { \
unsigned char tempreg; \
__asm__ __volatile__("in %[tempreg], %[mcucr]" "\n\t" \
"ori %[tempreg], %[bods_bodse]" "\n\t" \
"out %[mcucr], %[tempreg]" "\n\t" \
"andi %[tempreg], %[not_bodse]" "\n\t" \
"out %[mcucr], %[tempreg]" \
: [tempreg] "=&d" (tempreg) \
: [mcucr] "I" _SFR_IO_ADDR(MCUCR), \
[bods_bodse] "i" (_BV(BODS) | _BV(BODSE)), \
[not_bodse] "i" (~_BV(BODSE))); \
} while (0)
#endif
#endif


// Define these as macros to save valuable space
#define hwDigitalWrite(__pin, __value) digitalWriteFast(__pin, __value)
#define hwDigitalRead(__pin) digitalReadFast(__pin)
#define hwPinMode(__pin, __value) pinModeFast(__pin, __value)

#if defined(MY_DISABLED_SERIAL)
#define hwInit()
#else
#define hwInit() MY_SERIALDEVICE.begin(MY_BAUD_RATE)
#endif
bool hwInit(void);

#define hwWatchdogReset() wdt_reset()
#define hwReboot() wdt_enable(WDTO_15MS); while (1)
Expand All @@ -77,8 +54,6 @@
#define hwReadConfigBlock(__buf, __pos, __length) eeprom_read_block((void*)(__buf), (void*)(__pos), (__length))
#define hwWriteConfigBlock(__buf, __pos, __length) eeprom_update_block((void*)(__buf), (void*)(__pos), (__length))



enum period_t {
SLEEP_15MS,
SLEEP_30MS,
Expand Down
21 changes: 10 additions & 11 deletions core/MyHwESP8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2015 Sensnology AB
* Copyright (C) 2013-2017 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
Expand All @@ -20,13 +20,14 @@
#include "MyHwESP8266.h"
#include <EEPROM.h>

void hwInit(void)
bool hwInit(void)
{
#if !defined(MY_DISABLED_SERIAL)
MY_SERIALDEVICE.begin(MY_BAUD_RATE, SERIAL_8N1, MY_ESP8266_SERIAL_MODE, 1);
MY_SERIALDEVICE.setDebugOutput(true);
#endif
EEPROM.begin(EEPROM_size);
return true;
}

void hwReadConfigBlock(void* buf, void* addr, size_t length)
Expand Down Expand Up @@ -109,16 +110,14 @@ ADC_MODE(ADC_VCC);
ADC_MODE(ADC_TOUT);
#endif

#if defined(MY_DEBUG) || defined(MY_SPECIAL_DEBUG)

uint16_t hwCPUVoltage()
{
#if defined(MY_SPECIAL_DEBUG)
// in mV, requires ADC_VCC set
return ESP.getVcc();
#else
// not possible
return 0;
return FUNCTION_NOT_SUPPORTED;
#endif
}

Expand All @@ -132,16 +131,19 @@ uint16_t hwFreeMem()
{
return ESP.getFreeHeap();
}
#endif

#ifdef MY_DEBUG
void hwDebugPrint(const char *fmt, ... )
{
char fmtBuffer[MY_SERIAL_OUTPUT_SIZE];
#ifdef MY_GATEWAY_FEATURE
// prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
snprintf_P(fmtBuffer, sizeof(fmtBuffer), PSTR("0;255;%d;0;%d;"), C_INTERNAL, I_LOG_MESSAGE);
snprintf_P(fmtBuffer, sizeof(fmtBuffer), PSTR("0;255;%d;0;%d;%lu "), C_INTERNAL, I_LOG_MESSAGE,
hwMillis());
MY_SERIALDEVICE.print(fmtBuffer);
#else
// prepend timestamp
MY_SERIALDEVICE.print(hwMillis());
MY_SERIALDEVICE.print(" ");
#endif
va_list args;
va_start (args, fmt );
Expand All @@ -156,7 +158,4 @@ void hwDebugPrint(const char *fmt, ... )
va_end (args);
MY_SERIALDEVICE.print(fmtBuffer);
MY_SERIALDEVICE.flush();

//MY_SERIALDEVICE.write(freeRam());
}
#endif
4 changes: 2 additions & 2 deletions core/MyHwESP8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2015 Sensnology AB
* Copyright (C) 2013-2017 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
Expand Down Expand Up @@ -43,7 +43,7 @@
#define hwMillis() millis()
#define hwRandomNumberInit() randomSeed(RANDOM_REG32)

void hwInit(void);
bool hwInit(void);
void hwReadConfigBlock(void* buf, void* adr, size_t length);
void hwWriteConfigBlock(void* buf, void* adr, size_t length);
void hwWriteConfig(const int addr, uint8_t value);
Expand Down

0 comments on commit 61a415c

Please sign in to comment.