aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-07-25 06:21:11 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2015-07-25 19:37:44 +0200
commita64156388fc92da1d151a1094e25d9d89240bb38 (patch)
treef65eb7c8b4d7dccb400b67ebc7b82e861ffec13c
parent77e261f12089bd7db854d21fc81a42eaa60e0b67 (diff)
downloadtrygvisio_soil_moisture-a64156388fc92da1d151a1094e25d9d89240bb38.tar.gz
trygvisio_soil_moisture-a64156388fc92da1d151a1094e25d9d89240bb38.tar.bz2
trygvisio_soil_moisture-a64156388fc92da1d151a1094e25d9d89240bb38.tar.xz
trygvisio_soil_moisture-a64156388fc92da1d151a1094e25d9d89240bb38.zip
o Cleaning up #if usage and config.h.
-rw-r--r--Debug.h2
-rw-r--r--app.cpp9
-rw-r--r--app.h4
-rw-r--r--config-check.h110
-rw-r--r--config.h117
-rw-r--r--trygvisio_soil_moisture.ino51
6 files changed, 148 insertions, 145 deletions
diff --git a/Debug.h b/Debug.h
index a5b3134..4485afd 100644
--- a/Debug.h
+++ b/Debug.h
@@ -1,7 +1,7 @@
#ifndef DEBUG_H
#define DEBUG_H
-#include "config.h"
+#include "config-check.h"
enum DebugSink {
DEBUG_SINK_SERIAL,
diff --git a/app.cpp b/app.cpp
index e347b46..aef96d7 100644
--- a/app.cpp
+++ b/app.cpp
@@ -1,5 +1,6 @@
#include "app.h"
#include "Debug.h"
+#include "config-check.h"
#include <EEPROM.h>
@@ -8,7 +9,7 @@
#elif SM_BOARD_VERSION == 2
#define SENSOR_COUNT 4
#else
-#error "SM_BOARD_VERSION must be set. See config.h"
+#error "SM_BOARD_VERSION must be set to a valid value. See config-check.h"
#endif
#define LED_PIN 13
@@ -138,7 +139,7 @@ void sm_loop() {
}
}
-#ifdef SM_DEBUG == 1
+#if SM_DEBUG
static void write_name(uint8_t const* name, uint8_t len) {
for (int i = 0; i < len; i++) {
@@ -258,7 +259,7 @@ void on_soil_moisture_ctrl(uint8_t *data, uint8_t len) {
struct sm_res res;
uint8_t sensor;
-#if SM_DEBUG == 1
+#if SM_DEBUG
write_req(*req);
#endif
@@ -361,7 +362,7 @@ void on_soil_moisture_ctrl(uint8_t *data, uint8_t len) {
notify_soil_moisture(res, body_len);
}
-#if PERSISTENT_CONFIGURATION_SUPPORT == 1
+#ifdef PERSISTENT_CONFIGURATION_SUPPORT
static int store_string(int index, uint8_t len, char *chars) {
EEPROM.write(index++, len);
diff --git a/app.h b/app.h
index 25f834f..0f84296 100644
--- a/app.h
+++ b/app.h
@@ -1,7 +1,7 @@
#ifndef APP_H
#define APP_H
-#include "config.h"
+#include "config-check.h"
#include <stdint.h>
#include <Arduino.h>
@@ -126,7 +126,7 @@ void notify_battery_level(uint8_t value);
void on_soil_moisture_ctrl(uint8_t *data, uint8_t len);
-#if SM_DEBUG == 1
+#ifdef SM_DEBUG
void write_req(struct sm_req const &req);
void write_res(struct sm_res const &res);
#endif
diff --git a/config-check.h b/config-check.h
new file mode 100644
index 0000000..f259706
--- /dev/null
+++ b/config-check.h
@@ -0,0 +1,110 @@
+#ifndef CONFIG_CHECK_H
+#define CONFIG_CHECK_H
+
+/*
+ * You have to edit config.h to your likings. The file has to include this:
+ *
+ * #define CONFIF_VERSION <value>
+ *
+ * where value is the value defined below in this file.
+ *
+ * Read the "Configuration Section" below for the defines you can set.
+ */
+
+#include "config.h"
+
+// Arduino IDE or the pre-processed code doesn't complain when an include file is not found.
+#if CONFIG_VERSION != 20150725
+#error *** Read the notes in config-check.h ***
+#endif
+
+/*
+ *
+ * Configuration Section.
+ *
+ * This lists all available configuration items. The items that have default values doesn't have to be included in config.h. The items that are of boolean types evaluate to false if defined to '0' or true for any other value. Some items are default to true if not defined to false.
+ *
+ */
+
+/*******************************************************************************
+ * Board version: SM_BOARD_VERSION
+ *
+ * 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
+ *
+ * No default, has to be specified in config.h
+ */
+
+ /*******************************************************************************
+ * Wait for serial before starting: WAIT_FOR_SERIAL_BEFORE_STARING
+ *
+ * Type: boolean
+ *
+ * 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.
+ */
+
+#ifndef WAIT_FOR_SERIAL_BEFORE_STARING
+#define WAIT_FOR_SERIAL_BEFORE_STARING 1
+#endif
+
+#if WAIT_FOR_SERIAL_BEFORE_STARING == 0
+#undef WAIT_FOR_SERIAL_BEFORE_STARING
+#endif
+
+/*******************************************************************************
+ * Persistent configuration: PERSISTENT_CONFIGURATION_SUPPORT
+ *
+ * Type: boolean
+ *
+ * If enabled the settings for each sensor will be persisted in EEPROM
+ */
+
+#ifndef PERSISTENT_CONFIGURATION_SUPPORT
+#define PERSISTENT_CONFIGURATION_SUPPORT 1
+#endif
+
+#if PERSISTENT_CONFIGURATION_SUPPORT == 0
+#undef PERSISTENT_CONFIGURATION_SUPPORT
+#endif
+
+/*******************************************************************************
+ * Use low power mode: USE_LOW_POWER_MODE
+ *
+ * Type: boolean
+ *
+ * If enabled the device will try to use as little power as possible.
+ *
+ * Not fully functional yet so it is disabled by default.
+ */
+
+// Disabled by default for now
+
+#if USE_LOW_POWER_MODE == 0
+#undef USE_LOW_POWER_MODE
+#endif
+
+/*******************************************************************************
+ * Debug sink: DEBUG_SINK
+ *
+ * Selects where all debug logging will go.
+ *
+ * Values:
+ * 1: no logging, all logging is discarded
+ * 2: Use Serial. Uses the platforms default Serial port, usually the USB or hardware serial ports on your board.
+ */
+
+#ifndef DEBUG_SINK
+// Default to Serial
+#define DEBUG_SINK 2
+#endif
+
+#endif
diff --git a/config.h b/config.h
deleted file mode 100644
index 7f9a235..0000000
--- a/config.h
+++ /dev/null
@@ -1,117 +0,0 @@
-#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
- *
- */
-
-///////////////////////////////////////////////////////////////////////////////
-// Board version
-//
-// 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 ...
-
-#define SM_BOARD_VERSION 1
-
-///////////////////////////////////////////////////////////////////////////////
-// Wait for serial before starting
-//
-// 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 ...
-
-///////////////////////////////////////////////////////////////////////////////
-// Persistent configuration
-//
-// If enabled the settings for each sensor will be persisted in EEPROM
-//
-// Values:
-// 0: disabled
-// 1: enabled
-// Default: 0
-// #define PERSISTENT_CONFIGURATION_SUPPORT ...
-
-///////////////////////////////////////////////////////////////////////////////
-// Use low power mode
-//
-// If enabled the device will try to use as little power as possible.
-//
-// Not fully functional yet so it is disabled by default.
-//
-// Values:
-// 0: disabled
-// 1: enabled
-// Default: 0
-// #define USE_LOW_POWER_MODE ...
-
-/*
- *
- * 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
-#define PERSISTENT_CONFIGURATION_SUPPORT 1
-#endif
-
-#ifndef SM_DEBUG
-#define SM_DEBUG 1
-#endif
-
-#ifndef LOW_POWER
-#define LOW_POWER 0
-#endif
-
-// Default to Serial port.
-#ifndef DEBUG_SINK
-#define DEBUG_SINK 2
-#endif
-
-#endif
diff --git a/trygvisio_soil_moisture.ino b/trygvisio_soil_moisture.ino
index 53936e3..c09f091 100644
--- a/trygvisio_soil_moisture.ino
+++ b/trygvisio_soil_moisture.ino
@@ -1,3 +1,6 @@
+// See config.h on how to configure the sketch
+#include "config-check.h"
+
#include <LowPower.h>
#include <EEPROM.h>
#include <SPI.h>
@@ -5,8 +8,6 @@
#include <aci_setup.h>
#include <aci_evts.h>
-// See config.h on how to configure the sketch
-#include "config.h"
#include "services.h"
#include "app.h"
#include "Debug.h"
@@ -27,7 +28,7 @@ static hal_aci_evt_t aci_data;
static boolean timing_change_done = false;
void __ble_assert(const char *file, uint16_t line)
-{
+{
debug.print("ERROR ");
debug.print(file);
debug.print(": ");
@@ -36,14 +37,12 @@ void __ble_assert(const char *file, uint16_t line)
while (1) {};
}
-#if defined(__AVR_ATmega32U4__)
-static void go_to_sleep() {
- LowPower.idle(SLEEP_1S, ADC_OFF, TIMER4_OFF, TIMER3_OFF, TIMER1_OFF, TIMER0_OFF, SPI_OFF, USART1_OFF, TWI_OFF, USB_OFF);
-}
+static void go_to_sleep();
+
+#ifdef USE_LOW_POWER
+static const bool use_low_power=1;
#else
-#warning No sleep support for current CPU architecture.
-static void go_to_sleep() {
-}
+static const bool use_low_power=0;
#endif
void setup() {
@@ -60,7 +59,7 @@ void setup() {
debug.begin((unsigned int)115200);
-#if WAIT_FOR_SERIAL_BEFORE_STARING == 1
+#if WAIT_FOR_SERIAL_BEFORE_STARING
// 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__)
@@ -350,6 +349,17 @@ static void aci_loop() {
}
}
+static void go_to_sleep() {
+#ifdef USE_LOW_POWER
+#if defined(__AVR_ATmega32U4__)
+ LowPower.idle(SLEEP_1S, ADC_OFF, TIMER4_OFF, TIMER3_OFF, TIMER1_OFF, TIMER0_OFF, SPI_OFF, USART1_OFF, TWI_OFF, USB_OFF);
+}
+#else
+# warning No sleep support for current CPU architecture.
+#endif
+#endif // USE_LOW_POWER
+}
+
static uint8_t value = 0;
void loop() {
static unsigned long last = 0, now;
@@ -392,14 +402,13 @@ void loop() {
sm_loop();
-#ifdef USE_LOW_POWER_MODE == 1
-#ifdef SM_DEBUG == 1
- debug.println(F("Sleeping..."));
- debug.flush();
+ if (use_low_power) {
+#ifdef SM_DEBUG
+ debug.println(F("Sleeping..."));
+ debug.flush();
#endif // SM_DEBUG
-
- go_to_sleep();
-#endif // USE_LOW_POWER_MODE
+ go_to_sleep();
+ }
}
static void show_pipes() {
@@ -472,13 +481,13 @@ void notify_soil_moisture(const struct sm_res &res, uint8_t body_len) {
return;
}
-#if SM_DEBUG
+#ifdef SM_DEBUG
debug.println("write_res");
write_res(res);
#endif
if (aci_state.data_credit_available == 0) {
-#if SM_DEBUG
+#ifdef SM_DEBUG
debug.println("Not enough credits to send notification.");
#endif
return;
@@ -488,7 +497,7 @@ void notify_soil_moisture(const struct sm_res &res, uint8_t body_len) {
if (sent) {
aci_state.data_credit_available--;
} else {
-#if SM_DEBUG
+#ifdef SM_DEBUG
debug.println("Sending failed");
#endif
}