From c78be523bbcdc802929ce8f6fb119836dcbf99d5 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 28 Feb 2015 10:54:19 +0100 Subject: o Creating a way to configure the code for multiple boards. Look at config.h --- Makefile | 7 ++- app.cpp | 129 ++++++++++++++++++++++++-------------------- app.h | 6 +-- config.h | 86 +++++++++++++++++++++++++++++ trygvisio_soil_moisture.ino | 5 ++ 5 files changed, 171 insertions(+), 62 deletions(-) create mode 100644 config.h diff --git a/Makefile b/Makefile index 286c386..3f4c604 100644 --- a/Makefile +++ b/Makefile @@ -6,8 +6,13 @@ MONITOR_PORT ?= /dev/ttyACM0 ARDUINO_LIBS = BLE SPI EEPROM +ifdef CONFIG_MK +include $(CONFIG_MK) +endif + ARDMK_DIR ?= /usr/share/arduino -include $(ARDMK_DIR)/Arduino.mk $(ARDMK_DIR)/Arduino.mk: - @if [ ! -r $(ARDMK_DIR)/Arduino.mk ]; then echo "You need to install the arduino-mk package"; exit 1; fi + @if [ ! -r $(ARDMK_DIR)/Arduino.mk ]; then echo "You need to install the arduino-mk package"; fi + @false diff --git a/app.cpp b/app.cpp index a46734e..2d84700 100644 --- a/app.cpp +++ b/app.cpp @@ -4,6 +4,14 @@ #include #include +#if SM_BOARD_VERSION == 1 +#define SENSOR_COUNT 2 +#elif SM_BOARD_VERSION == 2 +#define SENSOR_COUNT 4 +#else +#error "SM_BOARD_VERSION must be set. See config.h" +#endif + // http://bleaklow.com/2010/09/05/progmem_and_gcc_bug_34734.html #undef PROGMEM #define PROGMEM __attribute__((section(".progmem.data"))) @@ -22,17 +30,13 @@ struct sm_sensor { uint16_t update_interval; uint8_t name_length; char name[SENSOR_NAME_LEN]; - -#define PROTOTYPE_VERSION 2 -#define SENSOR_COUNT 4 - } sensors[SENSOR_COUNT] = { // A Pin, D1 Pin, D2 Pin, Value, Warning Value, Update Interval, Length of Name, Name -#if PROTOTYPE_VERSION == 1 +#if SM_BOARD_VERSION == 1 { 8, 3, 5, 0, 10, 0, 9, "Sensor #1"}, { 10, 11, 12, 0, 10, 0, 9, "Sensor #2"}, -#elif PROTOTYPE_VERSION == 2 +#elif SM_BOARD_VERSION == 2 { A0, 1, 2, 0, 10, 0, 3, "wat"}, { A1, 3, 5, 0, 10, 0, 4, "woot"}, { A2, 8, 10, 0, 10, 0, 3, "foo"}, @@ -48,62 +52,17 @@ static boolean connected; static boolean blink; static unsigned long last_blink; +#if PERSISTENT_CONFIGURATION_SUPPORT == 1 +static boolean setup_settings(); static boolean load_settings(); static void store_settings(); static void set_default_settings(); +#endif void sm_setup() { - /* - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 16; j++) { - uint8_t value = EEPROM.read(i * 16 + j); - Serial.print(value, HEX); - Serial.print(" "); - Serial.print((char)value); - Serial.print(" "); - } - Serial.println(); - } - - for (int i = 0; i < SENSOR_COUNT; i++) { - struct sm_sensor s = sensors[i]; - - Serial.print("name len="); - Serial.print(s.name_length, DEC); - Serial.print(": ->"); - Serial.write((const uint8_t*) s.name, s.name_length); - Serial.print("<-"); - Serial.println(); - } -*/ -// set_default_settings(); - /* - if (!load_settings()) { - Serial.println("setting default settings"); - set_default_settings(); - - Serial.println("saving settings"); - store_settings(); - } else { - Serial.println("Settings loaded"); - } - /**/ -/**/ - for (int i = 0; i < SENSOR_COUNT; i++) { - Serial.print("XX: i="); - Serial.print(i, DEC); - - struct sm_sensor& s = sensors[i]; - - Serial.print(", name len="); - Serial.print(s.name_length, DEC); - Serial.print(": ->"); - Serial.write(s.name, s.name_length); - Serial.print("<-"); - Serial.println(); - } - /**/ - Serial.println("done!!"); +#if PERSISTENT_CONFIGURATION_SUPPORT == 1 + setup_settings(); +#endif for (int i = 0; i < SENSOR_COUNT; i++) { struct sm_sensor s = sensors[i]; @@ -133,6 +92,7 @@ void sm_on_disconnect() { digitalWrite(LED_PIN, LOW); } +#if PERSISTENT_CONFIGURATION_SUPPORT == 1 static int store_string(int index, uint8_t len, char* chars) { EEPROM.write(index++, len); @@ -210,6 +170,60 @@ static void store_settings() { Serial.println("Settings saved"); } +static void setup_settings() { + /* + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 16; j++) { + uint8_t value = EEPROM.read(i * 16 + j); + Serial.print(value, HEX); + Serial.print(" "); + Serial.print((char)value); + Serial.print(" "); + } + Serial.println(); + } + + for (int i = 0; i < SENSOR_COUNT; i++) { + struct sm_sensor s = sensors[i]; + + Serial.print("name len="); + Serial.print(s.name_length, DEC); + Serial.print(": ->"); + Serial.write((const uint8_t*) s.name, s.name_length); + Serial.print("<-"); + Serial.println(); + } +*/ +// set_default_settings(); +/* + if (!load_settings()) { + Serial.println("setting default settings"); + set_default_settings(); + + Serial.println("saving settings"); + store_settings(); + } else { + Serial.println("Settings loaded"); + } +*/ + for (int i = 0; i < SENSOR_COUNT; i++) { + Serial.print("XX: i="); + Serial.print(i, DEC); + + struct sm_sensor& s = sensors[i]; + + Serial.print(", name len="); + Serial.print(s.name_length, DEC); + Serial.print(": ->"); + Serial.write((const uint8_t*) s.name, s.name_length); + Serial.print("<-"); + Serial.println(); + } + /**/ + Serial.println("done"); +} +#endif // PERSISTENT_CONFIGURATION_SUPPORT + void sm_loop() { static unsigned long last = 0, now; @@ -480,3 +494,4 @@ void on_soil_moisture_ctrl(uint8_t *data, uint8_t len) { } // vim: set ft=arduino: + diff --git a/app.h b/app.h index e0ef6e9..6c48aa4 100644 --- a/app.h +++ b/app.h @@ -1,6 +1,7 @@ #ifndef APP_H #define APP_H +#include "config.h" #include #include @@ -117,10 +118,6 @@ void notify_battery_level(uint8_t value); void on_soil_moisture_ctrl(uint8_t *data, uint8_t len); -#ifndef SM_DEBUG -#define SM_DEBUG 1 -#endif - #if SM_DEBUG == 1 void write_req(struct sm_req const& req); void write_res(struct sm_res const& res); @@ -129,3 +126,4 @@ void write_res(struct sm_res const& res); #endif // vim: set ft=arduino: + diff --git a/config.h b/config.h new file mode 100644 index 0000000..e18a655 --- /dev/null +++ b/config.h @@ -0,0 +1,86 @@ +#ifndef CONFIG_H +#define CONFIG_H + +/* + +TL; DR; If you are using Arduino IDE, you have to configure this file. +See the first section. + +This file controls how the sketch work so this is where your should +start to adjust settings. If you are using Arduino-Makefile you can +set these values by editing the EXTRA_FLAGS Makefile variable. + +If you are using the Arduino IDE and want to set a configuration +value, change the lines that look like this: + +// #define FOO ... + +to + +#define FOO 1 + +*/ + +/* + +Configuration Section + +If you are using the Arduino IDE, change stuff in this section. All +values are commented out, but some have default values + +*/ + +// You have to set this to match your board. The build will fail if +// you don't set it. +// +// Values: +// 1: prototype-a, the hand assembled PCB +// 2: prototype-b, the china produced PCB +// +// Default: not set, you have to set it +// #define SM_BOARD_VERSION ... + +// If enabled the code won't start until you open the serial port +// monitor or otherwise connect to the serial port. +// +// This is useful to have defined when using the device with the +// Arduino IDE, but when you want to use it with a plant you don't +// want this to be set. +// +// Values: +// 0: Do not wait +// 1: Wait +// Default: 1 +// #define WAIT_FOR_SERIAL_BEFORE_STARING ... + +// If enabled the sensor's name will be persisted in EEPROM +// +// Values: +// 0: disabled +// 1: enabled +// Default: 0 +// #define PERSISTENT_CONFIGURATION_SUPPORT ... + +/* + +Default Values Section + +Do not touch stuff in this section, it only sets default values. + +*/ + +#ifndef WAIT_FOR_SERIAL_BEFORE_STARING +#define WAIT_FOR_SERIAL_BEFORE_STARING 1 +#endif + +#ifndef PERSISTENT_CONFIGURATION_SUPPORT +// Default to off, the code is not fully functional yet +#define PERSISTENT_CONFIGURATION_SUPPORT 0 +#endif + +#ifndef SM_DEBUG +#define SM_DEBUG 1 +#endif + +#endif + diff --git a/trygvisio_soil_moisture.ino b/trygvisio_soil_moisture.ino index 560aef7..d357d77 100644 --- a/trygvisio_soil_moisture.ino +++ b/trygvisio_soil_moisture.ino @@ -3,6 +3,8 @@ #include #include +// See config.h on how to configure the sketch +#include "config.h" #include "services.h" #include "app.h" @@ -45,6 +47,7 @@ void setup() { Serial.begin((unsigned int)115200); +#if WAIT_FOR_SERIAL_BEFORE_STARING == 1 // Wait until the serial port is available (useful only for the Leonardo) // As the Leonardo board is not reseted every time you open the Serial Monitor #if defined (__AVR_ATmega32U4__) @@ -56,6 +59,8 @@ void setup() { delay(1000); #endif +#endif // WAIT_FOR_SERIAL_BEFORE_STARING + setup_rf(); } -- cgit v1.2.3