diff --git a/examples/BinarySwitchSensor/BinarySwitchSensor.ino b/examples/BinarySwitchSensor/BinarySwitchSensor.ino index cb48456..c5e30c4 100644 --- a/examples/BinarySwitchSensor/BinarySwitchSensor.ino +++ b/examples/BinarySwitchSensor/BinarySwitchSensor.ino @@ -1,84 +1,82 @@ -/** - * 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 - * 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. - * - ******************************* - * - * DESCRIPTION - * - * Simple binary switch example - * Connect button or door/window reed switch between - * digitial I/O pin 3 (BUTTON_PIN below) and GND. - * http://www.mysensors.org/build/binary - */ - - -// Enable debug prints to serial monitor -#define MY_DEBUG - -// Enable and select radio type attached -#define MY_RADIO_NRF24 -//#define MY_RADIO_RFM69 - -#include -#include -#include - -#define CHILD_ID 3 -#define BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch - -Bounce debouncer = Bounce(); -int oldValue=-1; - -// Change to V_LIGHT if you use S_LIGHT in presentation below -MyMessage msg(CHILD_ID,V_TRIPPED); - -void setup() -{ - // Setup the button - pinMode(BUTTON_PIN,INPUT); - // Activate internal pull-up - digitalWrite(BUTTON_PIN,HIGH); - - // After setting up the button, setup debouncer - debouncer.attach(BUTTON_PIN); - debouncer.interval(5); - -} - -void presentation() { - // Register binary input sensor to gw (they will be created as child devices) - // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. - // If S_LIGHT is used, remember to update variable type you send in. See "msg" above. - present(CHILD_ID, S_DOOR); -} - - -// Check if digital input has changed and send in new value -void loop() -{ - debouncer.update(); - // Get the update value - int value = debouncer.read(); - - if (value != oldValue) { - // Send in the new value - send(msg.set(value==HIGH ? 1 : 0)); - oldValue = value; - } -} - +/** + * 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 + * 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. + * + ******************************* + * + * DESCRIPTION + * + * Simple binary switch example + * Connect button or door/window reed switch between + * digitial I/O pin 3 (BUTTON_PIN below) and GND. + * http://www.mysensors.org/build/binary + */ + + +// Enable debug prints to serial monitor +#define MY_DEBUG + +// Enable and select radio type attached +#define MY_RADIO_RF24 +//#define MY_RADIO_RFM69 + +#include +#include + +#define CHILD_ID 3 +#define BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch + +Bounce debouncer = Bounce(); +int oldValue=-1; + +// Change to V_LIGHT if you use S_LIGHT in presentation below +MyMessage msg(CHILD_ID,V_TRIPPED); + +void setup() +{ + // Setup the button + pinMode(BUTTON_PIN,INPUT); + // Activate internal pull-up + digitalWrite(BUTTON_PIN,HIGH); + + // After setting up the button, setup debouncer + debouncer.attach(BUTTON_PIN); + debouncer.interval(5); + +} + +void presentation() { + // Register binary input sensor to gw (they will be created as child devices) + // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. + // If S_LIGHT is used, remember to update variable type you send in. See "msg" above. + present(CHILD_ID, S_DOOR); +} + + +// Check if digital input has changed and send in new value +void loop() +{ + debouncer.update(); + // Get the update value + int value = debouncer.read(); + + if (value != oldValue) { + // Send in the new value + send(msg.set(value==HIGH ? 1 : 0)); + oldValue = value; + } +} diff --git a/examples/BinarySwitchSensorIndication/BinarySwitchSensorIndication.ino b/examples/BinarySwitchSensorIndication/BinarySwitchSensorIndication.ino index 6265d9d..f927319 100644 --- a/examples/BinarySwitchSensorIndication/BinarySwitchSensorIndication.ino +++ b/examples/BinarySwitchSensorIndication/BinarySwitchSensorIndication.ino @@ -6,7 +6,7 @@ * network topology allowing messages to be routed to nodes. * * Created by Henrik Ekblad - * Copyright (C) 2013-2016 Sensnology AB + * Copyright (C) 2013-2019 Sensnology AB * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors * * Documentation: http://www.mysensors.org @@ -32,9 +32,11 @@ #define MY_DEBUG // Enable and select radio type attached -#define MY_RADIO_NRF24 +#define MY_RADIO_RF24 //#define MY_RADIO_RFM69 +#define MY_INDICATION_HANDLER + #include #include #include @@ -147,4 +149,3 @@ void loop() oldValue = value; } } - diff --git a/examples/DallasTemperatureSensor/DallasTemperatureSensor.ino b/examples/DallasTemperatureSensor/DallasTemperatureSensor.ino index cdcc31c..73c6feb 100644 --- a/examples/DallasTemperatureSensor/DallasTemperatureSensor.ino +++ b/examples/DallasTemperatureSensor/DallasTemperatureSensor.ino @@ -1,109 +1,108 @@ -/** - * 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 - * 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. - * - ******************************* - * - * DESCRIPTION - * - * Example sketch showing how to send in DS1820B OneWire temperature readings back to the controller - * http://www.mysensors.org/build/temp - */ - - -// Enable debug prints to serial monitor -//#define MY_DEBUG - -// Enable and select radio type attached -#define MY_RADIO_NRF24 -//#define MY_RADIO_RFM69 - -#include -#include -#include -#include - -#define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No - -#define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected -#define MAX_ATTACHED_DS18B20 16 -unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds) -OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) -DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. -float lastTemperature[MAX_ATTACHED_DS18B20]; -int numSensors=0; -bool receivedConfig = false; -bool metric = true; -// Initialize temperature message -MyMessage msg(0,V_TEMP); - -void before() -{ - // Startup up the OneWire library - sensors.begin(); -} - -void setup() -{ - // requestTemperatures() will not block current thread - sensors.setWaitForConversion(false); -} - -void presentation() { - // Send the sketch version information to the gateway and Controller - sendSketchInfo("Temperature Sensor", "1.1"); - - // Fetch the number of attached temperature sensors - numSensors = sensors.getDeviceCount(); - - // Present all sensors to controller - for (int i=0; i(static_cast((getControllerConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.; - - // Only send data if temperature has changed and no error - #if COMPARE_TEMP == 1 - if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) { - #else - if (temperature != -127.00 && temperature != 85.00) { - #endif - - // Send in the new temperature - send(msg.setSensor(i).set(temperature,1)); - // Save new temperatures for next compare - lastTemperature[i]=temperature; - } - } - sleep(SLEEP_TIME); -} +/** + * 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 + * 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. + * + ******************************* + * + * DESCRIPTION + * + * Example sketch showing how to send in DS1820B OneWire temperature readings back to the controller + * http://www.mysensors.org/build/temp + */ + + +// Enable debug prints to serial monitor +//#define MY_DEBUG + +// Enable and select radio type attached +#define MY_RADIO_RF24 +//#define MY_RADIO_RFM69 + +#include +#include +#include + +#define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No + +#define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected +#define MAX_ATTACHED_DS18B20 16 +unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds) +OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) +DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. +float lastTemperature[MAX_ATTACHED_DS18B20]; +int numSensors=0; +bool receivedConfig = false; +bool metric = true; +// Initialize temperature message +MyMessage msg(0,V_TEMP); + +void before() +{ + // Startup up the OneWire library + sensors.begin(); +} + +void setup() +{ + // requestTemperatures() will not block current thread + sensors.setWaitForConversion(false); +} + +void presentation() { + // Send the sketch version information to the gateway and Controller + sendSketchInfo("Temperature Sensor", "1.1"); + + // Fetch the number of attached temperature sensors + numSensors = sensors.getDeviceCount(); + + // Present all sensors to controller + for (int i=0; i(static_cast((getControllerConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.; + + // Only send data if temperature has changed and no error + #if COMPARE_TEMP == 1 + if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) { + #else + if (temperature != -127.00 && temperature != 85.00) { + #endif + + // Send in the new temperature + send(msg.setSensor(i).set(temperature,1)); + // Save new temperatures for next compare + lastTemperature[i]=temperature; + } + } + sleep(SLEEP_TIME); +} diff --git a/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino b/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino index 19f65e0..0409182 100644 --- a/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino +++ b/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino @@ -35,7 +35,7 @@ #define MY_DEBUG // Enable and select radio type attached -#define MY_RADIO_NRF24 +#define MY_RADIO_RF24 //#define MY_RADIO_RFM69 //#define MY_RS485 @@ -46,7 +46,8 @@ // Set this to the pin you connected the DHT's data pin to #define DHT_DATA_PIN 3 -// Set this offset if the sensor has a permanent small offset to the real temperatures +// Set this offset if the sensor has a permanent small offset to the real temperatures. +// In Celsius degrees (as measured by the device) #define SENSOR_TEMP_OFFSET 0 // Sleep time between sensor updates (in milliseconds) @@ -110,12 +111,15 @@ void loop() } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) { // Only send temperature if it changed since the last measurement or if we didn't send an update for n times lastTemp = temperature; + + // apply the offset before converting to something different than Celsius degrees + temperature += SENSOR_TEMP_OFFSET; + if (!metric) { temperature = dht.toFahrenheit(temperature); } // Reset no updates counter nNoUpdatesTemp = 0; - temperature += SENSOR_TEMP_OFFSET; send(msgTemp.set(temperature, 1)); #ifdef MY_DEBUG diff --git a/examples/DimmableLightWithRotaryEncoderButton/DimmableLightWithRotaryEncoderButton.ino b/examples/DimmableLightWithRotaryEncoderButton/DimmableLightWithRotaryEncoderButton.ino index abddc14..39efdcd 100644 --- a/examples/DimmableLightWithRotaryEncoderButton/DimmableLightWithRotaryEncoderButton.ino +++ b/examples/DimmableLightWithRotaryEncoderButton/DimmableLightWithRotaryEncoderButton.ino @@ -1,240 +1,235 @@ -/** - * 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 - * 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. - * - ******************************* - * - * REVISION HISTORY - * Version 1.0 - Developed by Bruce Lacey and GizMoCuz (Domoticz) - * Version 1.1 - Modified by hek to incorporate a rotary encode to adjust - * light level locally at node - * - * DESCRIPTION - * This sketch provides an example how to implement a dimmable led light node with a rotary - * encoder connected for adjusting light level. - * The encoder has a click button which turns on/off the light (and remembers last dim-level) - * The sketch fades the light (non-blocking) to the desired level. - * - * Default MOSFET pin is 3 - * - * Arduino Encoder module - * --------------------------- - * 5V 5V (+) - * GND GND (-) - * 4 CLK (or putput 1) - * 5 DT (or output 1) - * 6 SW (Switch/Click) - */ - - -// Enable debug prints -#define MY_DEBUG - -// Enable and select radio type attached -#define MY_RADIO_NRF24 -//#define MY_RADIO_RFM69 - -#include -#include -#include -#include - -#define LED_PIN 3 // Arduino pin attached to MOSFET Gate pin -#define KNOB_ENC_PIN_1 4 // Rotary encoder input pin 1 -#define KNOB_ENC_PIN_2 5 // Rotary encoder input pin 2 -#define KNOB_BUTTON_PIN 6 // Rotary encoder button pin -#define FADE_DELAY 10 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim) -#define SEND_THROTTLE_DELAY 400 // Number of milliseconds before sending after user stops turning knob -#define SN "DimmableLED /w button" -#define SV "1.2" - -#define CHILD_ID_LIGHT 1 - -#define EEPROM_DIM_LEVEL_LAST 1 -#define EEPROM_DIM_LEVEL_SAVE 2 - -#define LIGHT_OFF 0 -#define LIGHT_ON 1 - -int dimValue; -int fadeTo; -int fadeDelta; -byte oldButtonVal; -bool changedByKnob=false; -bool sendDimValue=false; -unsigned long lastFadeStep; -unsigned long sendDimTimeout; -char convBuffer[10]; - -MyMessage dimmerMsg(CHILD_ID_LIGHT, V_DIMMER); -Encoder knob(KNOB_ENC_PIN_1, KNOB_ENC_PIN_2); -Bounce debouncer = Bounce(); - -void setup() -{ - // Set knob button pin as input (with debounce) - pinMode(KNOB_BUTTON_PIN, INPUT); - digitalWrite(KNOB_BUTTON_PIN, HIGH); - debouncer.attach(KNOB_BUTTON_PIN); - debouncer.interval(5); - oldButtonVal = debouncer.read(); - - // Set analog led pin to off - analogWrite( LED_PIN, 0); - - // Retreive our last dim levels from the eprom - fadeTo = dimValue = 0; - byte oldLevel = loadLevelState(EEPROM_DIM_LEVEL_LAST); - Serial.print("Sending in last known light level to controller: "); - Serial.println(oldLevel); - send(dimmerMsg.set(oldLevel), true); - - Serial.println("Ready to receive messages..."); -} - -void presentation() { - // Send the Sketch Version Information to the Gateway - present(CHILD_ID_LIGHT, S_DIMMER); - sendSketchInfo(SN, SV); -} - -void loop() -{ - // Check if someone turned the rotary encode - checkRotaryEncoder(); - - // Check if someone has pressed the knob button - checkButtonClick(); - - // Fade light to new dim value - fadeStep(); -} - -void receive(const MyMessage &message) -{ - if (message.type == V_LIGHT) { - // Incoming on/off command sent from controller ("1" or "0") - int lightState = message.getString()[0] == '1'; - int newLevel = 0; - if (lightState==LIGHT_ON) { - // Pick up last saved dimmer level from the eeprom - newLevel = loadLevelState(EEPROM_DIM_LEVEL_SAVE); - } - // Send dimmer level back to controller with ack enabled - send(dimmerMsg.set(newLevel), true); - // We do not change any levels here until ack comes back from gateway - return; - } else if (message.type == V_DIMMER) { - // Incoming dim-level command sent from controller (or ack message) - fadeTo = atoi(message.getString(convBuffer)); - // Save received dim value to eeprom (unless turned off). Will be - // retreived when a on command comes in - if (fadeTo != 0) - saveLevelState(EEPROM_DIM_LEVEL_SAVE, fadeTo); - } - saveLevelState(EEPROM_DIM_LEVEL_LAST, fadeTo); - - Serial.print("New light level received: "); - Serial.println(fadeTo); - - if (!changedByKnob) - knob.write(fadeTo); - - // Cancel send if user turns knob while message comes in - changedByKnob = false; - sendDimValue = false; - - // Stard fading to new light level - startFade(); -} - - - -void checkRotaryEncoder() { - long encoderValue = knob.read(); - - if (encoderValue > 100) { - encoderValue = 100; - knob.write(100); - } else if (encoderValue < 0) { - encoderValue = 0; - knob.write(0); - } - - if (encoderValue != fadeTo) { - fadeTo = encoderValue; - changedByKnob = true; - startFade(); - } -} - -void checkButtonClick() { - debouncer.update(); - byte buttonVal = debouncer.read(); - byte newLevel = 0; - if (buttonVal != oldButtonVal && buttonVal == LOW) { - if (dimValue==0) { - // Turn on light. Set the level to last saved dim value - int saved = loadLevelState(EEPROM_DIM_LEVEL_SAVE); - newLevel = saved > 0 ? saved : 100; - } - send(dimmerMsg.set(newLevel),true); - } - oldButtonVal = buttonVal; -} - -void startFade() { - fadeDelta = ( fadeTo - dimValue ) < 0 ? -1 : 1; - lastFadeStep = millis(); -} - -// This method provides a graceful none-blocking fade up/down effect -void fadeStep() { - unsigned long currentTime = millis(); - if ( dimValue != fadeTo && currentTime > lastFadeStep + FADE_DELAY) { - dimValue += fadeDelta; - analogWrite( LED_PIN, (int)(dimValue / 100. * 255) ); - lastFadeStep = currentTime; - - Serial.print("Fading level: "); - Serial.println(dimValue); - - if (fadeTo == dimValue && changedByKnob) { - sendDimValue = true; - sendDimTimeout = currentTime; - } - } - // Wait a few millisecs before sending in new value (if user still turns the knob) - if (sendDimValue && currentTime > sendDimTimeout + SEND_THROTTLE_DELAY) { - // We're done fading.. send in new dim-value to controller. - // Send in new dim value with ack (will be picked up in incomingMessage) - send(dimmerMsg.set(dimValue), true); // Send new dimmer value and request ack back - sendDimValue = false; - } -} - -// Make sure only to store/fetch values in the range 0-100 from eeprom -int loadLevelState(byte pos) { - return min(max(loadState(pos),0),100); -} -void saveLevelState(byte pos, byte data) { - saveState(pos,min(max(data,0),100)); -} - - - - +/** + * 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 + * 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. + * + ******************************* + * + * REVISION HISTORY + * Version 1.0 - Developed by Bruce Lacey and GizMoCuz (Domoticz) + * Version 1.1 - Modified by hek to incorporate a rotary encode to adjust + * light level locally at node + * + * DESCRIPTION + * This sketch provides an example how to implement a dimmable led light node with a rotary + * encoder connected for adjusting light level. + * The encoder has a click button which turns on/off the light (and remembers last dim-level) + * The sketch fades the light (non-blocking) to the desired level. + * + * Default MOSFET pin is 3 + * + * Arduino Encoder module + * --------------------------- + * 5V 5V (+) + * GND GND (-) + * 4 CLK (or putput 1) + * 5 DT (or output 1) + * 6 SW (Switch/Click) + */ + + +// Enable debug prints +#define MY_DEBUG + +// Enable and select radio type attached +#define MY_RADIO_RF24 +//#define MY_RADIO_RFM69 + +#include +#include +#include + +#define LED_PIN 3 // Arduino pin attached to MOSFET Gate pin +#define KNOB_ENC_PIN_1 4 // Rotary encoder input pin 1 +#define KNOB_ENC_PIN_2 5 // Rotary encoder input pin 2 +#define KNOB_BUTTON_PIN 6 // Rotary encoder button pin +#define FADE_DELAY 10 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim) +#define SEND_THROTTLE_DELAY 400 // Number of milliseconds before sending after user stops turning knob +#define SN "DimmableLED /w button" +#define SV "1.2" + +#define CHILD_ID_LIGHT 1 + +#define EEPROM_DIM_LEVEL_LAST 1 +#define EEPROM_DIM_LEVEL_SAVE 2 + +#define LIGHT_OFF 0 +#define LIGHT_ON 1 + +int dimValue; +int fadeTo; +int fadeDelta; +byte oldButtonVal; +bool changedByKnob=false; +bool sendDimValue=false; +unsigned long lastFadeStep; +unsigned long sendDimTimeout; +char convBuffer[10]; + +MyMessage dimmerMsg(CHILD_ID_LIGHT, V_DIMMER); +Encoder knob(KNOB_ENC_PIN_1, KNOB_ENC_PIN_2); +Bounce debouncer = Bounce(); + +void setup() +{ + // Set knob button pin as input (with debounce) + pinMode(KNOB_BUTTON_PIN, INPUT); + digitalWrite(KNOB_BUTTON_PIN, HIGH); + debouncer.attach(KNOB_BUTTON_PIN); + debouncer.interval(5); + oldButtonVal = debouncer.read(); + + // Set analog led pin to off + analogWrite( LED_PIN, 0); + + // Retreive our last dim levels from the eprom + fadeTo = dimValue = 0; + byte oldLevel = loadLevelState(EEPROM_DIM_LEVEL_LAST); + Serial.print("Sending in last known light level to controller: "); + Serial.println(oldLevel); + send(dimmerMsg.set(oldLevel), true); + + Serial.println("Ready to receive messages..."); +} + +void presentation() { + // Send the Sketch Version Information to the Gateway + present(CHILD_ID_LIGHT, S_DIMMER); + sendSketchInfo(SN, SV); +} + +void loop() +{ + // Check if someone turned the rotary encode + checkRotaryEncoder(); + + // Check if someone has pressed the knob button + checkButtonClick(); + + // Fade light to new dim value + fadeStep(); +} + +void receive(const MyMessage &message) +{ + if (message.type == V_LIGHT) { + // Incoming on/off command sent from controller ("1" or "0") + int lightState = message.getString()[0] == '1'; + int newLevel = 0; + if (lightState==LIGHT_ON) { + // Pick up last saved dimmer level from the eeprom + newLevel = loadLevelState(EEPROM_DIM_LEVEL_SAVE); + } + // Send dimmer level back to controller with ack enabled + send(dimmerMsg.set(newLevel), true); + // We do not change any levels here until ack comes back from gateway + return; + } else if (message.type == V_DIMMER) { + // Incoming dim-level command sent from controller (or ack message) + fadeTo = atoi(message.getString(convBuffer)); + // Save received dim value to eeprom (unless turned off). Will be + // retreived when a on command comes in + if (fadeTo != 0) + saveLevelState(EEPROM_DIM_LEVEL_SAVE, fadeTo); + } + saveLevelState(EEPROM_DIM_LEVEL_LAST, fadeTo); + + Serial.print("New light level received: "); + Serial.println(fadeTo); + + if (!changedByKnob) + knob.write(fadeTo); + + // Cancel send if user turns knob while message comes in + changedByKnob = false; + sendDimValue = false; + + // Stard fading to new light level + startFade(); +} + + + +void checkRotaryEncoder() { + long encoderValue = knob.read(); + + if (encoderValue > 100) { + encoderValue = 100; + knob.write(100); + } else if (encoderValue < 0) { + encoderValue = 0; + knob.write(0); + } + + if (encoderValue != fadeTo) { + fadeTo = encoderValue; + changedByKnob = true; + startFade(); + } +} + +void checkButtonClick() { + debouncer.update(); + byte buttonVal = debouncer.read(); + byte newLevel = 0; + if (buttonVal != oldButtonVal && buttonVal == LOW) { + if (dimValue==0) { + // Turn on light. Set the level to last saved dim value + int saved = loadLevelState(EEPROM_DIM_LEVEL_SAVE); + newLevel = saved > 0 ? saved : 100; + } + send(dimmerMsg.set(newLevel),true); + } + oldButtonVal = buttonVal; +} + +void startFade() { + fadeDelta = ( fadeTo - dimValue ) < 0 ? -1 : 1; + lastFadeStep = millis(); +} + +// This method provides a graceful none-blocking fade up/down effect +void fadeStep() { + unsigned long currentTime = millis(); + if ( dimValue != fadeTo && currentTime > lastFadeStep + FADE_DELAY) { + dimValue += fadeDelta; + analogWrite( LED_PIN, (int)(dimValue / 100. * 255) ); + lastFadeStep = currentTime; + + Serial.print("Fading level: "); + Serial.println(dimValue); + + if (fadeTo == dimValue && changedByKnob) { + sendDimValue = true; + sendDimTimeout = currentTime; + } + } + // Wait a few millisecs before sending in new value (if user still turns the knob) + if (sendDimValue && currentTime > sendDimTimeout + SEND_THROTTLE_DELAY) { + // We're done fading.. send in new dim-value to controller. + // Send in new dim value with ack (will be picked up in incomingMessage) + send(dimmerMsg.set(dimValue), true); // Send new dimmer value and request ack back + sendDimValue = false; + } +} + +// Make sure only to store/fetch values in the range 0-100 from eeprom +int loadLevelState(byte pos) { + return min(max(loadState(pos),0),100); +} +void saveLevelState(byte pos, byte data) { + saveState(pos,min(max(data,0),100)); +} diff --git a/examples/DistanceSensor/DistanceSensor.ino b/examples/DistanceSensor/DistanceSensor.ino index 0d3d73c..d780c37 100644 --- a/examples/DistanceSensor/DistanceSensor.ino +++ b/examples/DistanceSensor/DistanceSensor.ino @@ -1,79 +1,76 @@ -/** - * 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 - * 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. - * - ******************************* - * - * REVISION HISTORY - * Version 1.0 - Henrik EKblad - * - * DESCRIPTION - * This sketch provides an example how to implement a distance sensor using HC-SR04 - * http://www.mysensors.org/build/distance - */ - -// Enable debug prints -#define MY_DEBUG - -// Enable and select radio type attached -#define MY_RADIO_NRF24 -//#define MY_RADIO_RFM69 - -#include -#include -#include - -#define CHILD_ID 1 -#define TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor. -#define ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor. -#define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. -unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds) - -NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. -MyMessage msg(CHILD_ID, V_DISTANCE); -int lastDist; -bool metric = true; - -void setup() -{ - metric = getControllerConfig().isMetric; -} - -void presentation() { - // Send the sketch version information to the gateway and Controller - sendSketchInfo("Distance Sensor", "1.0"); - - // Register all sensors to gw (they will be created as child devices) - present(CHILD_ID, S_DISTANCE); -} - -void loop() -{ - int dist = metric?sonar.ping_cm():sonar.ping_in(); - Serial.print("Ping: "); - Serial.print(dist); // Convert ping time to distance in cm and print result (0 = outside set distance range) - Serial.println(metric?" cm":" in"); - - if (dist != lastDist) { - send(msg.set(dist)); - lastDist = dist; - } - - sleep(SLEEP_TIME); -} - - +/** + * 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 + * 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. + * + ******************************* + * + * REVISION HISTORY + * Version 1.0 - Henrik EKblad + * + * DESCRIPTION + * This sketch provides an example how to implement a distance sensor using HC-SR04 + * http://www.mysensors.org/build/distance + */ + +// Enable debug prints +#define MY_DEBUG + +// Enable and select radio type attached +#define MY_RADIO_RF24 +//#define MY_RADIO_RFM69 + +#include +#include + +#define CHILD_ID 1 +#define TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor. +#define ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor. +#define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. +unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds) + +NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. +MyMessage msg(CHILD_ID, V_DISTANCE); +int lastDist; +bool metric = true; + +void setup() +{ + metric = getControllerConfig().isMetric; +} + +void presentation() { + // Send the sketch version information to the gateway and Controller + sendSketchInfo("Distance Sensor", "1.0"); + + // Register all sensors to gw (they will be created as child devices) + present(CHILD_ID, S_DISTANCE); +} + +void loop() +{ + int dist = metric?sonar.ping_cm():sonar.ping_in(); + Serial.print("Ping: "); + Serial.print(dist); // Convert ping time to distance in cm and print result (0 = outside set distance range) + Serial.println(metric?" cm":" in"); + + if (dist != lastDist) { + send(msg.set(dist)); + lastDist = dist; + } + + sleep(SLEEP_TIME); +} diff --git a/examples/GPSSensor/GPSSensor.ino b/examples/GPSSensor/GPSSensor.ino index 35590b1..08b3519 100644 --- a/examples/GPSSensor/GPSSensor.ino +++ b/examples/GPSSensor/GPSSensor.ino @@ -1,146 +1,145 @@ -/** - * 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 - * 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. - * - ******************************* - * - * REVISION HISTORY - * Version 1.0 - Henrik Ekblad - * - * DESCRIPTION - * Example sketch showing how to interface with a GPS sensor. - * A GPS is also an excellent atomic time source. - * http://www.mysensors.org/build/gps - */ - -// Enable debug prints to serial monitor -#define MY_DEBUG - -// Enable and select radio type attached -#define MY_RADIO_NRF24 -//#define MY_RADIO_RFM69 - -// GPS position send interval (in millisectonds) -#define GPS_SEND_INTERVAL 10000 - -// The child id used for the gps sensor -#define CHILD_ID_GPS 1 - -// This is where the pin TX pin of your GPS sensor is connected to the arduino -static const int GPS_PIN = A0; - -// GPS Baud rate (note this is not the same as your serial montor baudrate). Most GPS modules uses 9600 bps. -static const uint32_t GPSBaud = 9600; - -// Offset hours adjustment from gps time (UTC) -const int offset = 1; - -#include -#include -#include -#include -#include - -// TinyGPS++ is used for parsing serial gps data -TinyGPSPlus gps; - -MyMessage msg(CHILD_ID_GPS, V_POSITION); - -// The serial connection to the GPS device -// A5 pin can be left unconnected -SoftwareSerial ss(GPS_PIN, A5); - -// Last time GPS position was sent to controller -unsigned long lastGPSSent = 0; - -// Some buffers -char latBuf[11]; -char lngBuf[11]; -char altBuf[6]; -char payload[30]; -char sz[64]; - - -void setup() { - // Set baudrate form gps communication - ss.begin(GPSBaud); -} - - -void presentation() { - // Send the sketch version information to the gateway and Controller - sendSketchInfo("GPS Sensor", "1.0"); - - // Register all sensors to gateway (they will be created as child devices) - present(CHILD_ID_GPS, S_GPS); -} - -void loop() -{ - unsigned long currentTime = millis(); - - // Evaluate if it is time to send a new position - bool timeToSend = currentTime - lastGPSSent > GPS_SEND_INTERVAL; - - // Read gps data - while (ss.available()) - gps.encode(ss.read()); - - if (timeToSend) { - // Sync gps time with Arduino - updateTime(); - - // Send current gps location - if (gps.location.isValid() && gps.altitude.isValid()) { - // Build position and altitude string to send - dtostrf(gps.location.lat(), 1, 6, latBuf); - dtostrf(gps.location.lng(), 1, 6, lngBuf); - dtostrf(gps.altitude.meters(), 1, 0, altBuf); - sprintf(payload, "%s;%s;%s", latBuf, lngBuf, altBuf); - - Serial.print(F("Position: ")); - Serial.println(payload); - - send(msg.set(payload)); - - Serial.print(F("GPS Time: ")); - sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d", month(), day(), year(), hour(), minute(), second()); - Serial.println(sz); - - - } else { - if (millis() > 5000 && gps.charsProcessed() < 10) - Serial.println(F("No GPS data received: check wiring")); - else - Serial.println(F("No GPS data received yet...")); - } - lastGPSSent = currentTime; - } -} - -void updateTime() -{ - TinyGPSDate d = gps.date; - TinyGPSTime t = gps.time; - if (d.isValid() && t.isValid()) { - // set the Time to the latest GPS reading if less then 0.2 seconds old - setTime(t.hour(), t.minute(), t.second(), d.day(), d.month(), d.year()); - adjustTime(offset * SECS_PER_HOUR); - return; - } - Serial.println(F("Unable to adjust time from GPS")); -} +/** + * 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 + * 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. + * + ******************************* + * + * REVISION HISTORY + * Version 1.0 - Henrik Ekblad + * + * DESCRIPTION + * Example sketch showing how to interface with a GPS sensor. + * A GPS is also an excellent atomic time source. + * http://www.mysensors.org/build/gps + */ + +// Enable debug prints to serial monitor +#define MY_DEBUG + +// Enable and select radio type attached +#define MY_RADIO_RF24 +//#define MY_RADIO_RFM69 + +// GPS position send interval (in millisectonds) +#define GPS_SEND_INTERVAL 10000 + +// The child id used for the gps sensor +#define CHILD_ID_GPS 1 + +// This is where the pin TX pin of your GPS sensor is connected to the arduino +static const int GPS_PIN = A0; + +// GPS Baud rate (note this is not the same as your serial montor baudrate). Most GPS modules uses 9600 bps. +static const uint32_t GPSBaud = 9600; + +// Offset hours adjustment from gps time (UTC) +const int offset = 1; + +#include +#include +#include +#include + +// TinyGPS++ is used for parsing serial gps data +TinyGPSPlus gps; + +MyMessage msg(CHILD_ID_GPS, V_POSITION); + +// The serial connection to the GPS device +// A5 pin can be left unconnected +SoftwareSerial ss(GPS_PIN, A5); + +// Last time GPS position was sent to controller +unsigned long lastGPSSent = 0; + +// Some buffers +char latBuf[11]; +char lngBuf[11]; +char altBuf[6]; +char payload[30]; +char sz[64]; + + +void setup() { + // Set baudrate form gps communication + ss.begin(GPSBaud); +} + + +void presentation() { + // Send the sketch version information to the gateway and Controller + sendSketchInfo("GPS Sensor", "1.0"); + + // Register all sensors to gateway (they will be created as child devices) + present(CHILD_ID_GPS, S_GPS); +} + +void loop() +{ + unsigned long currentTime = millis(); + + // Evaluate if it is time to send a new position + bool timeToSend = currentTime - lastGPSSent > GPS_SEND_INTERVAL; + + // Read gps data + while (ss.available()) + gps.encode(ss.read()); + + if (timeToSend) { + // Sync gps time with Arduino + updateTime(); + + // Send current gps location + if (gps.location.isValid() && gps.altitude.isValid()) { + // Build position and altitude string to send + dtostrf(gps.location.lat(), 1, 6, latBuf); + dtostrf(gps.location.lng(), 1, 6, lngBuf); + dtostrf(gps.altitude.meters(), 1, 0, altBuf); + sprintf(payload, "%s;%s;%s", latBuf, lngBuf, altBuf); + + Serial.print(F("Position: ")); + Serial.println(payload); + + send(msg.set(payload)); + + Serial.print(F("GPS Time: ")); + sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d", month(), day(), year(), hour(), minute(), second()); + Serial.println(sz); + + + } else { + if (millis() > 5000 && gps.charsProcessed() < 10) + Serial.println(F("No GPS data received: check wiring")); + else + Serial.println(F("No GPS data received yet...")); + } + lastGPSSent = currentTime; + } +} + +void updateTime() +{ + TinyGPSDate d = gps.date; + TinyGPSTime t = gps.time; + if (d.isValid() && t.isValid()) { + // set the Time to the latest GPS reading if less then 0.2 seconds old + setTime(t.hour(), t.minute(), t.second(), d.day(), d.month(), d.year()); + adjustTime(offset * SECS_PER_HOUR); + return; + } + Serial.println(F("Unable to adjust time from GPS")); +} diff --git a/examples/GatewayENC28J60/GatewayENC28J60.ino b/examples/GatewayENC28J60/GatewayENC28J60.ino index 926ba39..d81a860 100644 --- a/examples/GatewayENC28J60/GatewayENC28J60.ino +++ b/examples/GatewayENC28J60/GatewayENC28J60.ino @@ -1,102 +1,101 @@ -/** - * 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 - * 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. - * - ******************************* - * - * REVISION HISTORY - * Version 1.0 - Henrik EKblad - * Contribution by a-lurker and Anticimex, - * Contribution by Norbert Truchsess - * Contribution by Tomas Hozza - * - * - * DESCRIPTION - * The EthernetGateway sends data received from sensors to the ethernet link. - * The gateway also accepts input on ethernet interface, which is then sent out to the radio network. - * - * The GW code is designed for Arduino 328p / 16MHz. ATmega168 does not have enough memory to run this program. - * - * LED purposes: - * - To use the feature, uncomment WITH_LEDS_BLINKING in MyConfig.h - * - RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved - * - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly - * - ERR (red) - fast blink on error during transmission error or recieve crc error - * - * See http://www.mysensors.org/build/ethernet_gateway for wiring instructions. - * - */ - -// Enable debug prints to serial monitor -#define MY_DEBUG - -// Enable and select radio type attached -#define MY_RADIO_NRF24 -//#define MY_RADIO_RFM69 - -// When ENC28J60 is connected we have to move CE/CSN pins for NRF radio -#define MY_RF24_CE_PIN 5 -#define MY_RF24_CS_PIN 6 - -// Enable gateway ethernet module type -#define MY_GATEWAY_ENC28J60 - -// Gateway IP address -#define MY_IP_ADDRESS 192,168,178,66 - -// The port to keep open on node server mode / or port to contact in client mode -#define MY_PORT 5003 - -// Controller ip address. Enables client mode (default is "server" mode). -// Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere. -//#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 254 - -// The MAC address can be anything you want but should be unique on your network. -// Newer boards have a MAC address printed on the underside of the PCB, which you can (optionally) use. -// Note that most of the Ardunio examples use "DEAD BEEF FEED" for the MAC address. -#define MY_MAC_ADDRESS 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED - -// Flash leds on rx/tx/err -#define MY_LEDS_BLINKING_FEATURE -// Set blinking period -#define MY_DEFAULT_LED_BLINK_PERIOD 300 - -// Enable inclusion mode -#define MY_INCLUSION_MODE_FEATURE -// Enable Inclusion mode button on gateway -#define MY_INCLUSION_BUTTON_FEATURE -// Set inclusion mode duration (in seconds) -#define MY_INCLUSION_MODE_DURATION 60 -// Digital pin used for inclusion mode button -#define MY_INCLUSION_MODE_BUTTON_PIN 3 - -#define MY_DEFAULT_ERR_LED_PIN 7 // Error led pin -#define MY_DEFAULT_RX_LED_PIN 8 // Receive led pin -#define MY_DEFAULT_TX_LED_PIN 9 // the PCB, on board LED - -#include -#include -#include - - -void setup() -{ -} - -void loop() { -} - +/** + * 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 + * 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. + * + ******************************* + * + * REVISION HISTORY + * Version 1.0 - Henrik EKblad + * Contribution by a-lurker and Anticimex, + * Contribution by Norbert Truchsess + * Contribution by Tomas Hozza + * + * + * DESCRIPTION + * The EthernetGateway sends data received from sensors to the ethernet link. + * The gateway also accepts input on ethernet interface, which is then sent out to the radio network. + * + * The GW code is designed for Arduino 328p / 16MHz. ATmega168 does not have enough memory to run this program. + * + * LED purposes: + * - To use the feature, uncomment WITH_LEDS_BLINKING in MyConfig.h + * - RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved + * - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly + * - ERR (red) - fast blink on error during transmission error or recieve crc error + * + * See http://www.mysensors.org/build/ethernet_gateway for wiring instructions. + * + */ + +// Enable debug prints to serial monitor +#define MY_DEBUG + +// Enable and select radio type attached +#define MY_RADIO_RF24 +//#define MY_RADIO_RFM69 + +// When ENC28J60 is connected we have to move CE/CSN pins for NRF radio +#define MY_RF24_CE_PIN 5 +#define MY_RF24_CS_PIN 6 + +// Enable gateway ethernet module type +#define MY_GATEWAY_ENC28J60 + +// Gateway IP address +#define MY_IP_ADDRESS 192,168,178,66 + +// The port to keep open on node server mode / or port to contact in client mode +#define MY_PORT 5003 + +// Controller ip address. Enables client mode (default is "server" mode). +// Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere. +//#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 254 + +// The MAC address can be anything you want but should be unique on your network. +// Newer boards have a MAC address printed on the underside of the PCB, which you can (optionally) use. +// Note that most of the Ardunio examples use "DEAD BEEF FEED" for the MAC address. +#define MY_MAC_ADDRESS 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED + +// Flash leds on rx/tx/err +#define MY_LEDS_BLINKING_FEATURE +// Set blinking period +#define MY_DEFAULT_LED_BLINK_PERIOD 300 + +// Enable inclusion mode +#define MY_INCLUSION_MODE_FEATURE +// Enable Inclusion mode button on gateway +#define MY_INCLUSION_BUTTON_FEATURE +// Set inclusion mode duration (in seconds) +#define MY_INCLUSION_MODE_DURATION 60 +// Digital pin used for inclusion mode button +#define MY_INCLUSION_MODE_BUTTON_PIN 3 + +#define MY_DEFAULT_ERR_LED_PIN 7 // Error led pin +#define MY_DEFAULT_RX_LED_PIN 8 // Receive led pin +#define MY_DEFAULT_TX_LED_PIN 9 // the PCB, on board LED + +#include +#include +#include + + +void setup() +{ +} + +void loop() { +} diff --git a/examples/HeatpumpIRController/HeatpumpIRController.ino b/examples/HeatpumpIRController/HeatpumpIRController.ino index 5c026e8..a91386b 100644 --- a/examples/HeatpumpIRController/HeatpumpIRController.ino +++ b/examples/HeatpumpIRController/HeatpumpIRController.ino @@ -29,7 +29,7 @@ #define MY_DEBUG // Enable and select radio type attached -#define MY_RADIO_NRF24 +#define MY_RADIO_RF24 //#define MY_RADIO_RFM69 // HeatpumpIR libraries, diff --git a/examples/IrSensor/IrSensor.ino b/examples/IrSensor/IrSensor.ino index 3ec502d..9071231 100644 --- a/examples/IrSensor/IrSensor.ino +++ b/examples/IrSensor/IrSensor.ino @@ -1,456 +1,456 @@ -/** - * 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 - * 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. - * - ******************************* - * - * REVISION HISTORY - * Version 1.0 - Changed for MySensors usage by Bart Eversdijk - * Version 1.1 - Added option to record manual presets up to 240 - * Version 2.0 - Migrated to MySensrors version 2.0 - * - * DESCRIPTION - * - * IRrecord: record and play back IR signals as a minimal - * An IR detector/demodulator must be connected to the input RECV_PIN. - * An IR LED must be connected to the output PWM pin 3. - * - * - * The logic is: - * If a V_IR_RECORD is received the node enters in record mode and once a valid IR message has been received - * it is stored in EEPROM. The first byte of the V_IR_RECORD message will be used as preset ID - * - * If a V_IR_SEND the IR message beloning to the preset number of the first message byte is broadcasted - * - * - * Version 0.11 September, 2009 - * Copyright 2009 Ken Shirriff - * http://arcfn.com - */ - -// Enable debug prints -#define MY_DEBUG - -// Enable and select radio type attached -#define MY_RADIO_NRF24 - -#define MY_NODE_ID 5 - -#include -#include - -#include // https://github.com/z3t0/Arduino-IRremote/releases -// OR install IRRemote via "Sketch" -> "Include Library" -> "Manage Labraries..." -// Search for IRRemote b shirif and press the install button - -// Arduino pin to connect the IR receiver to -int RECV_PIN = 8; - -#define CHILD_ID 2 - -#define MY_RAWBUF 50 -const char * TYPE2STRING[] = { - "UNKONWN", - "RC5", - "RC6", - "NEC", - "Sony", - "Panasonic", - "JVC", - "SAMSUNG", - "Whynter", - "AIWA RC T501", - "LG", - "Sanyo", - "Mitsubishi", - "Dish", - "Sharp", - "Denon" -}; -#define Type2String(x) TYPE2STRING[x < 0 ? 0 : x] -#define AddrTxt F(" addres: 0x") -#define ValueTxt F(" value: 0x") -#define NATxt F(" - not implemented/found") - -// Raw or unknown codes requires an Arduino with a larger memory like a MEGA and some changes to store in EEPROM (now max 255 bytes) -// #define IR_SUPPORT_UNKNOWN_CODES -typedef union -{ - struct - { - decode_type_t type; // The type of code - unsigned long value; // The data bits if type is not raw - int len; // The length of the code in bits - unsigned int address; // Used by Panasonic & Sharp [16-bits] - } code; -#ifdef IR_SUPPORT_UNKNOWN_CODES - struct - { - decode_type_t type; // The type of code - unsigned int codes[MY_RAWBUF]; - byte count; // The number of interval samples - } raw; -#endif -} IRCode; - -#define MAX_STORED_IR_CODES 10 -IRCode StoredIRCodes[MAX_STORED_IR_CODES]; - -IRrecv irrecv(RECV_PIN); -IRsend irsend; -decode_results ircode; - -#define NO_PROG_MODE 0xFF -byte progModeId = NO_PROG_MODE; - -// Manual Preset IR values -- these are working demo values -// VERA call: luup.call_action("urn:schemas-arduino-cc:serviceId:ArduinoIr1", "SendIrCode", {Index=15}, ) -// One can add up to 240 preset codes (if your memory lasts) to see to correct data connect the Arduino with this plug in and -// look at the serial monitor while pressing the desired RC button -IRCode PresetIRCodes[] = { - { { RC5, 0x01, 12, 0 }}, // 11 - RC5 key "1" - { { RC5, 0x02, 12, 0 }}, // 12 - RC5 key "2" - { { RC5, 0x03, 12, 0 }}, // 13 - RC5 key "3" - { { NEC, 0xFF30CF, 32, 0 }}, // 14 - NEC key "1" - { { NEC, 0xFF18E7, 32, 0 }}, // 15 - NEC key "2" - { { NEC, 0xFF7A85, 32, 0 }}, // 16 - NEC key "3" - { { NEC, 0xFF10EF, 32, 0 }}, // 17 - NEC key "4" - { { NEC, 0xFF38C7, 32, 0 }}, // 18 - NEC key "5" - { { RC6, 0x800F2401, 36, 0 }}, // 19 - RC6 key "1" MicroSoft Mulitmedia RC - { { RC6, 0x800F2402, 36, 0 }} // 20 - RC6 key "2" MicroSoft Mulitmedia RC -}; -#define MAX_PRESET_IR_CODES (sizeof(PresetIRCodes)/sizeof(IRCode)) -#define MAX_IR_CODES (MAX_STORED_IR_CODES + MAX_PRESET_IR_CODES) - -MyMessage msgIrReceive(CHILD_ID, V_IR_RECEIVE); -MyMessage msgIrRecord(CHILD_ID, V_IR_RECORD); - -void setup() -{ - // Tell MYS Controller that we're NOT recording - send(msgIrRecord.set(0)); - - Serial.println(F("Recall EEPROM settings")); - recallEeprom(sizeof(StoredIRCodes), (byte *)&StoredIRCodes); - - // Start the ir receiver - irrecv.enableIRIn(); - - Serial.println(F("Init done...")); -} - -void presentation () -{ - // Send the sketch version information to the gateway and Controller - sendSketchInfo("IR Rec/Playback", "2.0"); - - // Register a sensors to gw. Use binary light for test purposes. - present(CHILD_ID, S_IR); -} - -void loop() -{ - if (irrecv.decode(&ircode)) { - dump(&ircode); - if (progModeId != NO_PROG_MODE) { - // If we are in PROG mode (Recording) store the new IR code and end PROG mode - if (storeRCCode(progModeId)) { - Serial.println(F("Stored ")); - - // If sucessfull RC decode and storage --> also update the EEPROM - storeEeprom(sizeof(StoredIRCodes), (byte *)&StoredIRCodes); - progModeId = NO_PROG_MODE; - - // Tell MYS Controller that we're done recording - send(msgIrRecord.set(0)); - } - } else { - // If we are in Playback mode just tell the MYS Controller we did receive an IR code - if (ircode.decode_type != UNKNOWN) { - if (ircode.value != REPEAT) { - // Look if we found a stored preset 0 => not found - byte num = lookUpPresetCode(&ircode); - if (num) { - // Send IR decode result to the MYS Controller - Serial.print(F("Found code for preset #")); - Serial.println(num); - send(msgIrReceive.set(num)); - } - } - } - } - // Wait a while before receive next IR-code (also block MySensors receiver so it will not interfere with a new message) - delay(500); - - // Start receiving again - irrecv.resume(); - } -} - -void receive(const MyMessage &message) { - //Serial.print(F("New message: ")); - //Serial.println(message.type); - - if (message.type == V_IR_RECORD) { // IR_RECORD V_VAR1 - // Get IR record requets for index : paramvalue - progModeId = message.getByte() % MAX_STORED_IR_CODES; - - // Tell MYS Controller that we're now in recording mode - send(msgIrRecord.set(1)); - - Serial.print(F("Record new IR for: ")); - Serial.println(progModeId); - } - - if (message.type == V_IR_SEND) { - // Send an IR code from offset: paramvalue - no check for legal value - Serial.print(F("Send IR preset: ")); - byte code = message.getByte() % MAX_IR_CODES; - if (code == 0) { - code = MAX_IR_CODES; - } - Serial.print(code); - sendRCCode(code); - } - - // Start receiving ir again... - irrecv.enableIRIn(); -} - - -byte lookUpPresetCode (decode_results *ircode) -{ - // Get rit of the RC5/6 toggle bit when looking up - if (ircode->decode_type == RC5) { - ircode->value = ircode->value & 0x7FF; - } - if (ircode->decode_type == RC6) { - ircode->value = ircode->value & 0xFFFF7FFF; - } - for (byte index = 0; index < MAX_STORED_IR_CODES; index++) - { - if ( StoredIRCodes[index].code.type == ircode->decode_type && - StoredIRCodes[index].code.value == ircode->value && - StoredIRCodes[index].code.len == ircode->bits) { - // The preset number starts with 1 so the last is stored as 0 -> fix this when looking up the correct index - return (index == 0) ? MAX_STORED_IR_CODES : index; - } - } - - for (byte index = 0; index < MAX_PRESET_IR_CODES; index++) - { - if ( PresetIRCodes[index].code.type == ircode->decode_type && - PresetIRCodes[index].code.value == ircode->value && - PresetIRCodes[index].code.len == ircode->bits) { - // The preset number starts with 1 so the last is stored as 0 -> fix this when looking up the correct index - return ((index == 0) ? MAX_PRESET_IR_CODES : index) + MAX_STORED_IR_CODES; - } - } - // not found so return 0 - return 0; -} - -// Stores the code for later playback -bool storeRCCode(byte index) { - - if (ircode.decode_type == UNKNOWN) { -#ifdef IR_SUPPORT_UNKNOWN_CODES - Serial.println(F("Received unknown code, saving as raw")); - // To store raw codes: - // Drop first value (gap) - // As of v1.3 of IRLib global values are already in microseconds rather than ticks - // They have also been adjusted for overreporting/underreporting of marks and spaces - byte rawCount = min(ircode.rawlen - 1, MY_RAWBUF); - for (int i = 1; i <= rawCount; i++) { - StoredIRCodes[index].raw.codes[i - 1] = ircode.rawbuf[i]; // Drop the first value - }; - return true; -#else - return false; - } -#endif - - if (ircode.value == REPEAT) { - // Don't record a NEC repeat value as that's useless. - Serial.println(F("repeat; ignoring.")); - return false; - } - // Get rit of the toggle bit when storing RC5/6 - if (ircode.decode_type == RC5) { - ircode.value = ircode.value & 0x07FF; - } - if (ircode.decode_type == RC6) { - ircode.value = ircode.value & 0xFFFF7FFF; - } - - StoredIRCodes[index].code.type = ircode.decode_type; - StoredIRCodes[index].code.value = ircode.value; - StoredIRCodes[index].code.address = ircode.address; // Used by Panasonic & Sharp [16-bits] - StoredIRCodes[index].code.len = ircode.bits; - Serial.print(F(" value: 0x")); - Serial.println(ircode.value, HEX); - return true; -} - -void sendRCCode(byte index) { - IRCode *pIr = ((index <= MAX_STORED_IR_CODES) ? &StoredIRCodes[index % MAX_STORED_IR_CODES] : &PresetIRCodes[index - MAX_STORED_IR_CODES - 1]); - -#ifdef IR_SUPPORT_UNKNOWN_CODES - if(pIr->code.type == UNKNOWN) { - // Assume 38 KHz - irsend.sendRaw(pIr->raw.codes, pIr->raw.count, 38); - Serial.println(F("Sent raw")); - return; - } -#endif - - Serial.print(F(" - sent ")); - Serial.print(Type2String(pIr->code.type)); - if (pIr->code.type == RC5) { - // For RC5 and RC6 there is a toggle bit for each succesor IR code sent alway toggle this bit, needs to repeat the command 3 times with 100 mS pause - pIr->code.value ^= 0x0800; - for (byte i=0; i < 3; i++) { - if (i > 0) { delay(100); } - irsend.sendRC5(pIr->code.value, pIr->code.len); - } - } - else if (pIr->code.type == RC6) { - // For RC5 and RC6 there is a toggle bit for each succesor IR code sent alway toggle this bit, needs to repeat the command 3 times with 100 mS pause - if (pIr->code.len == 20) { - pIr->code.value ^= 0x10000; - } - for (byte i=0; i < 3; i++) { - if (i > 0) { delay(100); } - irsend.sendRC6(pIr->code.value, pIr->code.len); - } - } - else if (pIr->code.type == NEC) { - irsend.sendNEC(pIr->code.value, pIr->code.len); - } - else if (pIr->code.type == SONY) { - irsend.sendSony(pIr->code.value, pIr->code.len); - } - else if (pIr->code.type == PANASONIC) { - irsend.sendPanasonic(pIr->code.address, pIr->code.value); - Serial.print(AddrTxt); - Serial.println(pIr->code.address, HEX); - } - else if (pIr->code.type == JVC) { - irsend.sendJVC(pIr->code.value, pIr->code.len, false); - } - else if (pIr->code.type == SAMSUNG) { - irsend.sendSAMSUNG(pIr->code.value, pIr->code.len); - } - else if (pIr->code.type == WHYNTER) { - irsend.sendWhynter(pIr->code.value, pIr->code.len); - } - else if (pIr->code.type == AIWA_RC_T501) { - irsend.sendAiwaRCT501(pIr->code.value); - } - else if (pIr->code.type == LG || pIr->code.type == SANYO || pIr->code.type == MITSUBISHI) { - Serial.println(NATxt); - return; - } - else if (pIr->code.type == DISH) { - // need to repeat the command 4 times with 100 mS pause - for (byte i=0; i < 4; i++) { - if (i > 0) { delay(100); } - irsend.sendDISH(pIr->code.value, pIr->code.len); - } - } - else if (pIr->code.type == SHARP) { - irsend.sendSharp(pIr->code.address, pIr->code.value); - Serial.print(AddrTxt); - Serial.println(pIr->code.address, HEX); - } - else if (pIr->code.type == DENON) { - irsend.sendDenon(pIr->code.value, pIr->code.len); - } - else { - // No valid IR type, found it does not make sense to broadcast - Serial.println(NATxt); - return; - } - Serial.print(" "); - Serial.println(pIr->code.value, HEX); -} - -// Dumps out the decode_results structure. -void dump(decode_results *results) { - int count = results->rawlen; - - Serial.print(F("Received : ")); - Serial.print(results->decode_type, DEC); - Serial.print(F(" ")); - Serial.print(Type2String(results->decode_type)); - - if (results->decode_type == PANASONIC) { - Serial.print(AddrTxt); - Serial.print(results->address,HEX); - Serial.print(ValueTxt); - } - Serial.print(F(" ")); - Serial.print(results->value, HEX); - Serial.print(F(" (")); - Serial.print(results->bits, DEC); - Serial.println(F(" bits)")); - - if (results->decode_type == UNKNOWN) { - Serial.print(F("Raw (")); - Serial.print(count, DEC); - Serial.print(F("): ")); - - for (int i = 0; i < count; i++) { - if ((i % 2) == 1) { - Serial.print(results->rawbuf[i]*USECPERTICK, DEC); - } - else { - Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC); - } - Serial.print(" "); - } - Serial.println(""); - } -} - -// Store IR record struct in EEPROM -void storeEeprom(byte len, byte *buf) -{ - saveState(0, len); - for (byte i = 1; i < min(len, 100); i++, buf++) - { - saveState(i, *buf); - } -} - -void recallEeprom(byte len, byte *buf) -{ - if (loadState(0) != len) - { - Serial.print(F("Corrupt EEPROM preset values and Clear EEPROM")); - for (byte i = 1; i < min(len, 100); i++, buf++) - { - *buf = 0; - storeEeprom(len, buf); - } - return; - } - for (byte i = 1; i < min(len, 100); i++, buf++) - { - *buf = loadState(i); - } -} +/** + * 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 + * 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. + * + ******************************* + * + * REVISION HISTORY + * Version 1.0 - Changed for MySensors usage by Bart Eversdijk + * Version 1.1 - Added option to record manual presets up to 240 + * Version 2.0 - Migrated to MySensrors version 2.0 + * + * DESCRIPTION + * + * IRrecord: record and play back IR signals as a minimal + * An IR detector/demodulator must be connected to the input RECV_PIN. + * An IR LED must be connected to the output PWM pin 3. + * + * + * The logic is: + * If a V_IR_RECORD is received the node enters in record mode and once a valid IR message has been received + * it is stored in EEPROM. The first byte of the V_IR_RECORD message will be used as preset ID + * + * If a V_IR_SEND the IR message beloning to the preset number of the first message byte is broadcasted + * + * + * Version 0.11 September, 2009 + * Copyright 2009 Ken Shirriff + * http://arcfn.com + */ + +// Enable debug prints +#define MY_DEBUG + +// Enable and select radio type attached +#define MY_RADIO_RF24 + +#define MY_NODE_ID 5 + +#include +#include + +#include // https://github.com/z3t0/Arduino-IRremote/releases +// OR install IRRemote via "Sketch" -> "Include Library" -> "Manage Labraries..." +// Search for IRRemote b shirif and press the install button + +// Arduino pin to connect the IR receiver to +int RECV_PIN = 8; + +#define CHILD_ID 2 + +#define MY_RAWBUF 50 +const char * TYPE2STRING[] = { + "UNKONWN", + "RC5", + "RC6", + "NEC", + "Sony", + "Panasonic", + "JVC", + "SAMSUNG", + "Whynter", + "AIWA RC T501", + "LG", + "Sanyo", + "Mitsubishi", + "Dish", + "Sharp", + "Denon" +}; +#define Type2String(x) TYPE2STRING[x < 0 ? 0 : x] +#define AddrTxt F(" addres: 0x") +#define ValueTxt F(" value: 0x") +#define NATxt F(" - not implemented/found") + +// Raw or unknown codes requires an Arduino with a larger memory like a MEGA and some changes to store in EEPROM (now max 255 bytes) +// #define IR_SUPPORT_UNKNOWN_CODES +typedef union +{ + struct + { + decode_type_t type; // The type of code + unsigned long value; // The data bits if type is not raw + int len; // The length of the code in bits + unsigned int address; // Used by Panasonic & Sharp [16-bits] + } code; +#ifdef IR_SUPPORT_UNKNOWN_CODES + struct + { + decode_type_t type; // The type of code + unsigned int codes[MY_RAWBUF]; + byte count; // The number of interval samples + } raw; +#endif +} IRCode; + +#define MAX_STORED_IR_CODES 10 +IRCode StoredIRCodes[MAX_STORED_IR_CODES]; + +IRrecv irrecv(RECV_PIN); +IRsend irsend; +decode_results ircode; + +#define NO_PROG_MODE 0xFF +byte progModeId = NO_PROG_MODE; + +// Manual Preset IR values -- these are working demo values +// VERA call: luup.call_action("urn:schemas-arduino-cc:serviceId:ArduinoIr1", "SendIrCode", {Index=15}, ) +// One can add up to 240 preset codes (if your memory lasts) to see to correct data connect the Arduino with this plug in and +// look at the serial monitor while pressing the desired RC button +IRCode PresetIRCodes[] = { + { { RC5, 0x01, 12, 0 }}, // 11 - RC5 key "1" + { { RC5, 0x02, 12, 0 }}, // 12 - RC5 key "2" + { { RC5, 0x03, 12, 0 }}, // 13 - RC5 key "3" + { { NEC, 0xFF30CF, 32, 0 }}, // 14 - NEC key "1" + { { NEC, 0xFF18E7, 32, 0 }}, // 15 - NEC key "2" + { { NEC, 0xFF7A85, 32, 0 }}, // 16 - NEC key "3" + { { NEC, 0xFF10EF, 32, 0 }}, // 17 - NEC key "4" + { { NEC, 0xFF38C7, 32, 0 }}, // 18 - NEC key "5" + { { RC6, 0x800F2401, 36, 0 }}, // 19 - RC6 key "1" MicroSoft Mulitmedia RC + { { RC6, 0x800F2402, 36, 0 }} // 20 - RC6 key "2" MicroSoft Mulitmedia RC +}; +#define MAX_PRESET_IR_CODES (sizeof(PresetIRCodes)/sizeof(IRCode)) +#define MAX_IR_CODES (MAX_STORED_IR_CODES + MAX_PRESET_IR_CODES) + +MyMessage msgIrReceive(CHILD_ID, V_IR_RECEIVE); +MyMessage msgIrRecord(CHILD_ID, V_IR_RECORD); + +void setup() +{ + // Tell MYS Controller that we're NOT recording + send(msgIrRecord.set(0)); + + Serial.println(F("Recall EEPROM settings")); + recallEeprom(sizeof(StoredIRCodes), (byte *)&StoredIRCodes); + + // Start the ir receiver + irrecv.enableIRIn(); + + Serial.println(F("Init done...")); +} + +void presentation () +{ + // Send the sketch version information to the gateway and Controller + sendSketchInfo("IR Rec/Playback", "2.0"); + + // Register a sensors to gw. Use binary light for test purposes. + present(CHILD_ID, S_IR); +} + +void loop() +{ + if (irrecv.decode(&ircode)) { + dump(&ircode); + if (progModeId != NO_PROG_MODE) { + // If we are in PROG mode (Recording) store the new IR code and end PROG mode + if (storeRCCode(progModeId)) { + Serial.println(F("Stored ")); + + // If sucessfull RC decode and storage --> also update the EEPROM + storeEeprom(sizeof(StoredIRCodes), (byte *)&StoredIRCodes); + progModeId = NO_PROG_MODE; + + // Tell MYS Controller that we're done recording + send(msgIrRecord.set(0)); + } + } else { + // If we are in Playback mode just tell the MYS Controller we did receive an IR code + if (ircode.decode_type != UNKNOWN) { + if (ircode.value != REPEAT) { + // Look if we found a stored preset 0 => not found + byte num = lookUpPresetCode(&ircode); + if (num) { + // Send IR decode result to the MYS Controller + Serial.print(F("Found code for preset #")); + Serial.println(num); + send(msgIrReceive.set(num)); + } + } + } + } + // Wait a while before receive next IR-code (also block MySensors receiver so it will not interfere with a new message) + delay(500); + + // Start receiving again + irrecv.resume(); + } +} + +void receive(const MyMessage &message) { + //Serial.print(F("New message: ")); + //Serial.println(message.type); + + if (message.type == V_IR_RECORD) { // IR_RECORD V_VAR1 + // Get IR record requets for index : paramvalue + progModeId = message.getByte() % MAX_STORED_IR_CODES; + + // Tell MYS Controller that we're now in recording mode + send(msgIrRecord.set(1)); + + Serial.print(F("Record new IR for: ")); + Serial.println(progModeId); + } + + if (message.type == V_IR_SEND) { + // Send an IR code from offset: paramvalue - no check for legal value + Serial.print(F("Send IR preset: ")); + byte code = message.getByte() % MAX_IR_CODES; + if (code == 0) { + code = MAX_IR_CODES; + } + Serial.print(code); + sendRCCode(code); + } + + // Start receiving ir again... + irrecv.enableIRIn(); +} + + +byte lookUpPresetCode (decode_results *ircode) +{ + // Get rit of the RC5/6 toggle bit when looking up + if (ircode->decode_type == RC5) { + ircode->value = ircode->value & 0x7FF; + } + if (ircode->decode_type == RC6) { + ircode->value = ircode->value & 0xFFFF7FFF; + } + for (byte index = 0; index < MAX_STORED_IR_CODES; index++) + { + if ( StoredIRCodes[index].code.type == ircode->decode_type && + StoredIRCodes[index].code.value == ircode->value && + StoredIRCodes[index].code.len == ircode->bits) { + // The preset number starts with 1 so the last is stored as 0 -> fix this when looking up the correct index + return (index == 0) ? MAX_STORED_IR_CODES : index; + } + } + + for (byte index = 0; index < MAX_PRESET_IR_CODES; index++) + { + if ( PresetIRCodes[index].code.type == ircode->decode_type && + PresetIRCodes[index].code.value == ircode->value && + PresetIRCodes[index].code.len == ircode->bits) { + // The preset number starts with 1 so the last is stored as 0 -> fix this when looking up the correct index + return ((index == 0) ? MAX_PRESET_IR_CODES : index) + MAX_STORED_IR_CODES; + } + } + // not found so return 0 + return 0; +} + +// Stores the code for later playback +bool storeRCCode(byte index) { + + if (ircode.decode_type == UNKNOWN) { +#ifdef IR_SUPPORT_UNKNOWN_CODES + Serial.println(F("Received unknown code, saving as raw")); + // To store raw codes: + // Drop first value (gap) + // As of v1.3 of IRLib global values are already in microseconds rather than ticks + // They have also been adjusted for overreporting/underreporting of marks and spaces + byte rawCount = min(ircode.rawlen - 1, MY_RAWBUF); + for (int i = 1; i <= rawCount; i++) { + StoredIRCodes[index].raw.codes[i - 1] = ircode.rawbuf[i]; // Drop the first value + }; + return true; +#else + return false; + } +#endif + + if (ircode.value == REPEAT) { + // Don't record a NEC repeat value as that's useless. + Serial.println(F("repeat; ignoring.")); + return false; + } + // Get rit of the toggle bit when storing RC5/6 + if (ircode.decode_type == RC5) { + ircode.value = ircode.value & 0x07FF; + } + if (ircode.decode_type == RC6) { + ircode.value = ircode.value & 0xFFFF7FFF; + } + + StoredIRCodes[index].code.type = ircode.decode_type; + StoredIRCodes[index].code.value = ircode.value; + StoredIRCodes[index].code.address = ircode.address; // Used by Panasonic & Sharp [16-bits] + StoredIRCodes[index].code.len = ircode.bits; + Serial.print(F(" value: 0x")); + Serial.println(ircode.value, HEX); + return true; +} + +void sendRCCode(byte index) { + IRCode *pIr = ((index <= MAX_STORED_IR_CODES) ? &StoredIRCodes[index % MAX_STORED_IR_CODES] : &PresetIRCodes[index - MAX_STORED_IR_CODES - 1]); + +#ifdef IR_SUPPORT_UNKNOWN_CODES + if(pIr->code.type == UNKNOWN) { + // Assume 38 KHz + irsend.sendRaw(pIr->raw.codes, pIr->raw.count, 38); + Serial.println(F("Sent raw")); + return; + } +#endif + + Serial.print(F(" - sent ")); + Serial.print(Type2String(pIr->code.type)); + if (pIr->code.type == RC5) { + // For RC5 and RC6 there is a toggle bit for each succesor IR code sent alway toggle this bit, needs to repeat the command 3 times with 100 mS pause + pIr->code.value ^= 0x0800; + for (byte i=0; i < 3; i++) { + if (i > 0) { delay(100); } + irsend.sendRC5(pIr->code.value, pIr->code.len); + } + } + else if (pIr->code.type == RC6) { + // For RC5 and RC6 there is a toggle bit for each succesor IR code sent alway toggle this bit, needs to repeat the command 3 times with 100 mS pause + if (pIr->code.len == 20) { + pIr->code.value ^= 0x10000; + } + for (byte i=0; i < 3; i++) { + if (i > 0) { delay(100); } + irsend.sendRC6(pIr->code.value, pIr->code.len); + } + } + else if (pIr->code.type == NEC) { + irsend.sendNEC(pIr->code.value, pIr->code.len); + } + else if (pIr->code.type == SONY) { + irsend.sendSony(pIr->code.value, pIr->code.len); + } + else if (pIr->code.type == PANASONIC) { + irsend.sendPanasonic(pIr->code.address, pIr->code.value); + Serial.print(AddrTxt); + Serial.println(pIr->code.address, HEX); + } + else if (pIr->code.type == JVC) { + irsend.sendJVC(pIr->code.value, pIr->code.len, false); + } + else if (pIr->code.type == SAMSUNG) { + irsend.sendSAMSUNG(pIr->code.value, pIr->code.len); + } + else if (pIr->code.type == WHYNTER) { + irsend.sendWhynter(pIr->code.value, pIr->code.len); + } + else if (pIr->code.type == AIWA_RC_T501) { + irsend.sendAiwaRCT501(pIr->code.value); + } + else if (pIr->code.type == LG || pIr->code.type == SANYO || pIr->code.type == MITSUBISHI) { + Serial.println(NATxt); + return; + } + else if (pIr->code.type == DISH) { + // need to repeat the command 4 times with 100 mS pause + for (byte i=0; i < 4; i++) { + if (i > 0) { delay(100); } + irsend.sendDISH(pIr->code.value, pIr->code.len); + } + } + else if (pIr->code.type == SHARP) { + irsend.sendSharp(pIr->code.address, pIr->code.value); + Serial.print(AddrTxt); + Serial.println(pIr->code.address, HEX); + } + else if (pIr->code.type == DENON) { + irsend.sendDenon(pIr->code.value, pIr->code.len); + } + else { + // No valid IR type, found it does not make sense to broadcast + Serial.println(NATxt); + return; + } + Serial.print(" "); + Serial.println(pIr->code.value, HEX); +} + +// Dumps out the decode_results structure. +void dump(decode_results *results) { + int count = results->rawlen; + + Serial.print(F("Received : ")); + Serial.print(results->decode_type, DEC); + Serial.print(F(" ")); + Serial.print(Type2String(results->decode_type)); + + if (results->decode_type == PANASONIC) { + Serial.print(AddrTxt); + Serial.print(results->address,HEX); + Serial.print(ValueTxt); + } + Serial.print(F(" ")); + Serial.print(results->value, HEX); + Serial.print(F(" (")); + Serial.print(results->bits, DEC); + Serial.println(F(" bits)")); + + if (results->decode_type == UNKNOWN) { + Serial.print(F("Raw (")); + Serial.print(count, DEC); + Serial.print(F("): ")); + + for (int i = 0; i < count; i++) { + if ((i % 2) == 1) { + Serial.print(results->rawbuf[i]*USECPERTICK, DEC); + } + else { + Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC); + } + Serial.print(" "); + } + Serial.println(""); + } +} + +// Store IR record struct in EEPROM +void storeEeprom(byte len, byte *buf) +{ + saveState(0, len); + for (byte i = 1; i < min(len, 100); i++, buf++) + { + saveState(i, *buf); + } +} + +void recallEeprom(byte len, byte *buf) +{ + if (loadState(0) != len) + { + Serial.print(F("Corrupt EEPROM preset values and Clear EEPROM")); + for (byte i = 1; i < min(len, 100); i++, buf++) + { + *buf = 0; + storeEeprom(len, buf); + } + return; + } + for (byte i = 1; i < min(len, 100); i++, buf++) + { + *buf = loadState(i); + } +} diff --git a/examples/IrrigationController/IrrigationController.ino b/examples/IrrigationController/IrrigationController.ino index 5c9f917..0d95c70 100644 --- a/examples/IrrigationController/IrrigationController.ino +++ b/examples/IrrigationController/IrrigationController.ino @@ -1,883 +1,883 @@ -/* -MySprinkler for MySensors - -Arduino Multi-Zone Sprinkler Control - -May 31, 2015 - -*** Version 2.0 - -*** Upgraded to http://MySensors.org version 1.4.1 -*** Expanded for up to 16 Valves -*** Setup for active low relay board or comment out #define ACTIVE_LOW to switch to active high -*** Switch to bitshift method vs byte arrays -*** Changed RUN_ALL_ZONES Vera device to 0 (was highest valve) -*** Added optional LCD display featuring remaining time, date last ran & current time -*** Features 'raindrop' and 'clock' icons which indicate sensor is updating valve data and clock respectively -*** Added single pushbutton menu to manually select which program to run (All Zones or a Single Zone) -*** Added option of naming your Zones programmatically or with Vera (V_VAR3 used to store names) - -Utilizing your Vera home automation controller and the MySensors.org gateway you can -control up to a sixteen zone irrigation system with only three digital pins. This sketch -will create NUMBER_OF_VALVES + 1 devices on your Vera controller - -This sketch features the following: - -* Allows you to cycle through All zones (RUN_ALL_ZONES) or individual zone (RUN_SINGLE_ZONE) control. -* Use the 0th controller to activate RUN_ALL_ZONES (each zone in numeric sequence 1 to n) - using Variable1 as the "ON" time in minutes in each of the vera devices created. -* Use the individual zone controller to activate a single zone. This feature uses - Variable2 as the "ON" time for each individual device/zone. -* Connect according to pinout below and uses Shift Registers as to allow the MySensors - standard radio configuration and still leave available digital pins -* Turning on any zone will stop the current process and begin that particular process. -* Turning off any zone will stop the current process and turn off all zones. -* To push your new time intervals for your zones, simply change the variable on your Vera and - your arduino will call to Vera once a minute and update accordingly. Variables will also be - requested when the device is first powered on. -* Pushbutton activation to RUN_ALL_ZONES, RUN_SINGLE_ZONE or halt the current program -* LED status indicator - -PARTS LIST: -Available from the MySensors store - http://www.mysensors.org/store/ -* Relays (8 channel) -* Female Pin Header Connector Strip -* Prototype Universal Printed Circuit Boards (PCB) -* NRF24L01 Radio -* Arduino (I used a Pro Mini) -* FTDI USB to TTL Serial Adapter -* Capacitors (10uf and .1uf) -* 3.3v voltage regulator -* Resistors (270, 1K & 10K) -* Female Dupont Cables -* 1602 LCD (with I2C Interface) -* LED -* Push button -* Shift Register (SN74HC595) -* 2 Pole 5mm Pitch PCB Mount Screw Terminal Block -* 3 Pole 5mm Pitch PCB Mount Screw Terminal Block -* 22-24 gauge wire or similar (I used Cat5/Cat6 cable) -* 18 gauge wire (for relay) -* Irrigation Power Supply (24-Volt/750 mA Transformer) - - -INSTRUCTIONS: - -* A step-by-step setup video is available here: http://youtu.be/l4GPRTsuHkI -* After assembling your arduino, radio, decoupling capacitors, shift register(s), status LED, pushbutton LCD (I2C connected to - A4 and A5) and relays, and load the sketch. -* Following the instructions at https://MySensors.org include the device to your MySensors Gateway. -* Verify that each new device has a Variable1, Variable2 and Variable3. Populate data accordingly with whole minutes for - the RUN_ALL_ZONES routine (Variable1) and the RUN_SINGLE_ZONE routines (Variable 2). The values entered for times may be zero and - you may use the defaulet zone names by leaving Variable3 blank. -* Once you have entered values for each zone and each variable, save the settings by pressing the red save button on your Vera. -* Restart your arduino; verify the settings are loaded into your arduino with the serial monitor; the array will be printed - on the serial monitor. -* Your arduino should slow-flash, indicating that it is in ready mode. -* There are multiple debug serial prints that can be monitored to assure that it is operating properly. -* ***THIS SHOULD NO LONGER BE NEEDED*** The standard MySensors library now works. https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads for the I2C library, or use yours - -Contributed by Jim (BulldogLowell@gmail.com) with much contribution from Pete (pete.will@mysensors.org) and is released to the public domain -*/ -// - -// Enable debug prints -//#define MY_DEBUG - -// Enable and select radio type attached -#define MY_RADIO_NRF24 -//#define MY_RADIO_RFM69 - -//#define MY_NODE_ID 1 // Set this to fix your Radio ID or use Auto - -#include -#include -#include -#include -#include -#include - - -#define NUMBER_OF_VALVES 8 // Change this to set your valve count up to 16. -#define VALVE_RESET_TIME 7500UL // Change this (in milliseconds) for the time you need your valves to hydraulically reset and change state -#define VALVE_TIMES_RELOAD 300000UL // Change this (in milliseconds) for how often to update all valves data from the controller (Loops at value/number valves) - // ie: 300000 for 8 valves produces requests 37.5seconds with all valves updated every 5mins - -#define SKETCH_NAME "MySprinkler" -#define SKETCH_VERSION "2.2" -// -#define CHILD_ID_SPRINKLER 0 -// -#define ACTIVE_LOW // comment out this line if your relays are active high -// -#define DEBUG_ON // comment out to supress serial monitor output -// -#ifdef ACTIVE_LOW -#define BITSHIFT_VALVE_NUMBER ~(1U << (valveNumber-1)) -#define ALL_VALVES_OFF 0xFFFF -#else -#define BITSHIFT_VALVE_NUMBER (1U << (valveNumber-1)) -#define ALL_VALVES_OFF 0U -#endif -// -#ifdef DEBUG_ON -#define DEBUG_PRINT(x) Serial.print(x) -#define DEBUG_PRINTLN(x) Serial.println(x) -#else -#define DEBUG_PRINT(x) -#define DEBUG_PRINTLN(x) -#define SERIAL_START(x) -#endif -// -typedef enum { - STAND_BY_ALL_OFF, RUN_SINGLE_ZONE, RUN_ALL_ZONES, CYCLE_COMPLETE, ZONE_SELECT_MENU -} -SprinklerStates; -// -SprinklerStates state = STAND_BY_ALL_OFF; -SprinklerStates lastState; -byte menuState = 0; -unsigned long menuTimer; -byte countDownTime = 10; -// -int allZoneTime [NUMBER_OF_VALVES + 1]; -int valveSoloTime [NUMBER_OF_VALVES + 1]; -int valveNumber; -int lastValve; -unsigned long startMillis; -const int ledPin = 5; -const int waterButtonPin = 3; -bool buttonPushed = false; -bool showTime = true; -bool clockUpdating = false; -bool recentUpdate = true; -int allVars[] = {V_VAR1, V_VAR2, V_VAR3}; -const char *dayOfWeek[] = { - "Null", "Sunday ", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday " -}; -// Name your Zones here or use Vera to edit them by adding a name in Variable3... -String valveNickName[17] = { - "All Zones", "Zone 1", "Zone 2", "Zone 3", "Zone 4", "Zone 5", "Zone 6", "Zone 7", "Zone 8", "Zone 9", "Zone 10", "Zone 11", "Zone 12", "Zone 13", "Zone 14", "Zone 15", "Zone 16" -}; -// -time_t lastTimeRun = 0; -//Setup Shift Register... -const int latchPin = 8; -const int clockPin = 4; -const int dataPin = 7; -const int outputEnablePin = 6; -// -byte clock[8] = {0x0, 0xe, 0x15, 0x17, 0x11, 0xe, 0x0}; // fetching time indicator -byte raindrop[8] = {0x4, 0x4, 0xA, 0xA, 0x11, 0xE, 0x0,}; // fetching Valve Data indicator -// Set the pins on the I2C chip used for LCD connections: -// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol -LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address to 0x27 -// -MyMessage msg1valve(CHILD_ID_SPRINKLER, V_LIGHT); -MyMessage var1valve(CHILD_ID_SPRINKLER, V_VAR1); -MyMessage var2valve(CHILD_ID_SPRINKLER, V_VAR2); - -bool receivedInitialValue = false; -bool inSetup = true; -// -void setup() -{ - DEBUG_PRINTLN(F("Initialising...")); - pinMode(latchPin, OUTPUT); - pinMode(clockPin, OUTPUT); - pinMode(dataPin, OUTPUT); - pinMode(ledPin, OUTPUT); - pinMode(outputEnablePin, OUTPUT); - digitalWrite (outputEnablePin, LOW); - pinMode(waterButtonPin, INPUT_PULLUP); - //pinMode(waterButtonPin, INPUT); - attachInterrupt(digitalPinToInterrupt(waterButtonPin), PushButton, RISING); //May need to change for your Arduino model - digitalWrite (ledPin, HIGH); - DEBUG_PRINTLN(F("Turning All Valves Off...")); - updateRelays(ALL_VALVES_OFF); - //delay(5000); - lcd.begin(16, 2); //(16 characters and 2 line display) - lcd.clear(); - lcd.backlight(); - lcd.createChar(0, clock); - lcd.createChar(1, raindrop); - // - //check for saved date in EEPROM - DEBUG_PRINTLN(F("Checking EEPROM for stored date:")); - delay(500); - if (loadState(0) == 0xFF) // EEPROM flag - { - DEBUG_PRINTLN(F("Retreiving last run time from EEPROM...")); - for (int i = 0; i < 4 ; i++) - { - lastTimeRun = lastTimeRun << 8; - lastTimeRun = lastTimeRun | loadState(i + 1); // assemble 4 bytes into an ussigned long epoch timestamp - } - } - - DEBUG_PRINTLN(F("Sensor Presentation Complete")); - // - digitalWrite (ledPin, LOW); - DEBUG_PRINTLN(F("Ready...")); - // - lcd.setCursor(0, 0); - lcd.print(F(" Syncing Time ")); - lcd.setCursor(15, 0); - lcd.write(byte(0)); - lcd.setCursor(0, 1); - int clockCounter = 0; - while (timeStatus() == timeNotSet && clockCounter < 21) - { - requestTime(); - DEBUG_PRINTLN(F("Requesting time from Gateway:")); - wait(1000); - lcd.print("."); - clockCounter++; - if (clockCounter > 16) - { - DEBUG_PRINTLN(F("Failed initial clock synchronization!")); - lcd.clear(); - lcd.print(F(" Failed Clock ")); - lcd.setCursor(0, 1); - lcd.print(F(" Syncronization ")); - wait(2000); - break; - } - } - // - //Update valve data when first powered on - for (byte i = 1; i <= NUMBER_OF_VALVES; i++) - { - lcd.clear(); - goGetValveTimes(); - } - lcd.clear(); - inSetup = false; -} - - -void presentation() -{ - sendSketchInfo(SKETCH_NAME, SKETCH_VERSION); - for (byte i = 0; i <= NUMBER_OF_VALVES; i++) - { - present(i, S_LIGHT); - } -} - -// -void loop() -{ - updateClock(); - updateDisplay(); - goGetValveTimes(); - // - if (buttonPushed) - { - menuTimer = millis(); - DEBUG_PRINTLN(F("Button Pressed")); - if (state == STAND_BY_ALL_OFF) - { - state = ZONE_SELECT_MENU; - menuState = 0; - } - else if (state == ZONE_SELECT_MENU) - { - menuState++; - if (menuState > NUMBER_OF_VALVES) - { - menuState = 0; - } - } - else - { - state = STAND_BY_ALL_OFF; - } - buttonPushed = false; - } - if (state == STAND_BY_ALL_OFF) - { - slowToggleLED (); - if (state != lastState) - { - updateRelays(ALL_VALVES_OFF); - DEBUG_PRINTLN(F("State Changed... all Zones off")); - for (byte i = 0; i <= NUMBER_OF_VALVES; i++) - { - wait(50); - send(msg1valve.setSensor(i).set(false), false); - } - lcd.clear(); - lcd.setCursor(0,0); - lcd.print(F("** Irrigation **")); - lcd.setCursor(0,1); - lcd.print(F("** Halted **")); - wait(2000); - lastValve = -1; - } - } - // - else if (state == RUN_ALL_ZONES) - { - if (lastValve != valveNumber) - { - for (byte i = 0; i <= NUMBER_OF_VALVES; i++) - { - if (i == 0 || i == valveNumber) - { - send(msg1valve.setSensor(i).set(true), false); - } - else - { - send(msg1valve.setSensor(i).set(false), false); - } - wait(50); - } - } - lastValve = valveNumber; - fastToggleLed(); - if (state != lastState) - { - valveNumber = 1; - updateRelays(ALL_VALVES_OFF); - DEBUG_PRINTLN(F("State Changed, Running All Zones...")); - } - unsigned long nowMillis = millis(); - if (nowMillis - startMillis < VALVE_RESET_TIME) - { - updateRelays(ALL_VALVES_OFF); - } - else if (nowMillis - startMillis < (allZoneTime[valveNumber] * 60000UL)) - { - updateRelays(BITSHIFT_VALVE_NUMBER); - } - else - { - DEBUG_PRINTLN(F("Changing Valves...")); - updateRelays(ALL_VALVES_OFF); - startMillis = millis(); - valveNumber++; - if (valveNumber > NUMBER_OF_VALVES) - { - state = CYCLE_COMPLETE; - startMillis = millis(); - lastValve = -1; - lastTimeRun = now(); - saveDateToEEPROM(lastTimeRun); - for (byte i = 0; i <= NUMBER_OF_VALVES; i++) - { - send(msg1valve.setSensor(i).set(false), false); - wait(50); - } - DEBUG_PRINT(F("State = ")); - DEBUG_PRINTLN(state); - } - } - } - // - else if (state == RUN_SINGLE_ZONE) - { - fastToggleLed(); - if (state != lastState) - { - for (byte i = 0; i <= NUMBER_OF_VALVES; i++) - { - if (i == 0 || i == valveNumber) - { - send(msg1valve.setSensor(i).set(true), false); - } - else - { - send(msg1valve.setSensor(i).set(false), false); - } - wait(50); - } - DEBUG_PRINTLN(F("State Changed, Single Zone Running...")); - DEBUG_PRINT(F("Zone: ")); - DEBUG_PRINTLN(valveNumber); - } - unsigned long nowMillis = millis(); - if (nowMillis - startMillis < VALVE_RESET_TIME) - { - updateRelays(ALL_VALVES_OFF); - } - else if (nowMillis - startMillis < (valveSoloTime [valveNumber] * 60000UL)) - { - updateRelays(BITSHIFT_VALVE_NUMBER); - } - else - { - updateRelays(ALL_VALVES_OFF); - for (byte i = 0; i <= NUMBER_OF_VALVES; i++) - { - send(msg1valve.setSensor(i).set(false), false); - } - state = CYCLE_COMPLETE; - startMillis = millis(); - DEBUG_PRINT(F("State = ")); - DEBUG_PRINTLN(state); - } - lastTimeRun = now(); - } - else if (state == CYCLE_COMPLETE) - { - if (millis() - startMillis < 30000UL) - { - fastToggleLed(); - } - else - { - state = STAND_BY_ALL_OFF; - } - } - if (state == ZONE_SELECT_MENU) - { - displayMenu(); - } - else - { - lastState = state; - } -} -// -void displayMenu(void) -{ - static byte lastMenuState = -1; - static int lastSecond; - if (menuState != lastMenuState) - { - lcd.clear(); - lcd.setCursor(0, 0); - lcd.print(valveNickName[menuState]); - lcd.setCursor(0, 1); - lcd.print(F("Starting")); - DEBUG_PRINT(valveNickName[menuState]); - Serial.print(F(" Starting Shortly")); - } - int thisSecond = (millis() - menuTimer) / 1000UL; - if (thisSecond != lastSecond && thisSecond < 8) - { - lcd.print(F(".")); - Serial.print("."); - } - lastSecond = thisSecond; - if (millis() - menuTimer > 10000UL) - { - startMillis = millis(); - if (menuState == 0) - { - valveNumber = 1; - state = RUN_ALL_ZONES; - } - else - { - valveNumber = menuState; - state = RUN_SINGLE_ZONE; - } - } - else - { - - } - lastMenuState = menuState; -} -// -void updateRelays(int value) -{ - digitalWrite(latchPin, LOW); - shiftOut(dataPin, clockPin, MSBFIRST, highByte(value)); - shiftOut(dataPin, clockPin, MSBFIRST, lowByte(value)); - digitalWrite(latchPin, HIGH); -} -// -void PushButton() //interrupt with debounce -{ - static unsigned long last_interrupt_time = 0; - unsigned long interrupt_time = millis(); - if (interrupt_time - last_interrupt_time > 200) - { - buttonPushed = true; - } - last_interrupt_time = interrupt_time; -} -// -void fastToggleLed() -{ - static unsigned long fastLedTimer; - if (millis() - fastLedTimer >= 100UL) - { - digitalWrite(ledPin, !digitalRead(ledPin)); - fastLedTimer = millis (); - } -} -// -void slowToggleLED () -{ - static unsigned long slowLedTimer; - if (millis() - slowLedTimer >= 1250UL) - { - digitalWrite(ledPin, !digitalRead(ledPin)); - slowLedTimer = millis (); - } -} -// -void receive(const MyMessage &message) -{ - bool zoneTimeUpdate = false; - if (message.isAck()) - { - DEBUG_PRINTLN(F("This is an ack from gateway")); - } - for (byte i = 0; i <= NUMBER_OF_VALVES; i++) - { - if (message.sensor == i) - { - if (message.type == V_LIGHT) - { - int switchState = atoi(message.data); - if (switchState == 0) - { - state = STAND_BY_ALL_OFF; - DEBUG_PRINTLN(F("Recieved Instruction to Cancel...")); - } - else - { - if (i == 0) - { - state = RUN_ALL_ZONES; - valveNumber = 1; - DEBUG_PRINTLN(F("Recieved Instruction to Run All Zones...")); - } - else - { - state = RUN_SINGLE_ZONE; - valveNumber = i; - DEBUG_PRINT(F("Recieved Instruction to Activate Zone: ")); - DEBUG_PRINTLN(i); - } - } - startMillis = millis(); - } - else if (message.type == V_VAR1) - { - int variable1 = atoi(message.data);// RUN_ALL_ZONES time - DEBUG_PRINT(F("Recieved variable1 valve:")); - DEBUG_PRINT(i); - DEBUG_PRINT(F(" = ")); - DEBUG_PRINTLN(variable1); - if (variable1 != allZoneTime[i]) - { - allZoneTime[i] = variable1; - - zoneTimeUpdate = true; - } - receivedInitialValue = true; - } - else if (message.type == V_VAR2) - { - int variable2 = atoi(message.data);// RUN_SINGLE_ZONE time - DEBUG_PRINT(F("Recieved variable2 valve:")); - DEBUG_PRINT(i); - DEBUG_PRINT(F(" = ")); - DEBUG_PRINTLN(variable2); - if (variable2 != valveSoloTime[i]) - { - valveSoloTime[i] = variable2; - zoneTimeUpdate = true; - } - receivedInitialValue = true; - } - else if (message.type == V_VAR3) - { - String newMessage = String(message.data); - if (newMessage.length() == 0) - { - DEBUG_PRINT(F("No Name Recieved for zone ")); - DEBUG_PRINTLN(i); - break; - } - if (newMessage.length() > 16) - { - newMessage.substring(0, 16); - } - valveNickName[i] = ""; - valveNickName[i] += newMessage; - DEBUG_PRINT(F("Recieved variable3 valve: ")); - DEBUG_PRINT(i); - DEBUG_PRINT(F(" = ")); - DEBUG_PRINTLN(valveNickName[i]); - } - receivedInitialValue = true; - } - } - if (zoneTimeUpdate) - { - // - DEBUG_PRINTLN(F("New Zone Times Recieved...")); - for (byte i = 0; i <= NUMBER_OF_VALVES; i++) - { - if (i != 0) - { - DEBUG_PRINT(F("Zone ")); - DEBUG_PRINT(i); - DEBUG_PRINT(F(" individual time: ")); - DEBUG_PRINT(valveSoloTime[i]); - DEBUG_PRINT(F(" group time: ")); - DEBUG_PRINT(allZoneTime[i]); - DEBUG_PRINT(F(" name: ")); - DEBUG_PRINTLN(valveNickName[i]); - recentUpdate = true; - } - } - } - else - { - recentUpdate = false; - } -} -// -void updateDisplay() -{ - static unsigned long lastUpdateTime; - static bool displayToggle = false; - //static byte toggleCounter = 0; - static SprinklerStates lastDisplayState; - if (state != lastDisplayState || millis() - lastUpdateTime >= 3000UL) - { - displayToggle = !displayToggle; - switch (state) { - case STAND_BY_ALL_OFF: - // - fastClear(); - lcd.setCursor(0, 0); - if (displayToggle) - { - lcd.print(F(" System Ready ")); - if (clockUpdating) - { - lcd.setCursor(15, 0); - lcd.write(byte(0)); - } - lcd.setCursor(0, 1); - lcd.print(hourFormat12() < 10 ? F(" ") : F("")); - lcd.print(hourFormat12()); - lcd.print(minute() < 10 ? F(":0") : F(":")); - lcd.print(minute()); - lcd.print(isAM() ? F("am") : F("pm")); - lcd.print(month() < 10 ? F(" 0") : F(" ")); - lcd.print(month()); - lcd.print(day() < 10 ? F("/0") : F("/")); - lcd.print(day()); - lcd.print(F("/")); - lcd.print(year() % 100); - } - else - { - lcd.print(F(" Last Watered ")); - if (clockUpdating) - { - lcd.setCursor(15, 0); - lcd.write(byte(0)); - } - lcd.setCursor(0, 1); - lcd.print(dayOfWeek[weekday(lastTimeRun)]); - lcd.setCursor(11, 1); - lcd.print(month(lastTimeRun) < 10 ? F(" ") : F("")); - lcd.print(month(lastTimeRun)); - lcd.print(day(lastTimeRun) < 10 ? F("/0") : F("/")); - lcd.print(day(lastTimeRun)); - } - break; - case RUN_SINGLE_ZONE: - // - fastClear(); - lcd.setCursor(0, 0); - if (displayToggle) - { - lcd.print(F("Single Zone Mode")); - lcd.setCursor(0, 1); - lcd.print(F(" Zone:")); - if (valveNumber < 10) lcd.print(F("0")); - lcd.print(valveNumber); - lcd.print(F(" Active")); - } - else - { - lcd.print(F(" Time Remaining ")); - lcd.setCursor(0, 1); - if (valveSoloTime[valveNumber] == 0) - { - lcd.print(F(" No Valve Time ")); - } - else - { - unsigned long timeRemaining = (valveSoloTime[valveNumber] * 60) - ((millis() - startMillis) / 1000); - lcd.print(timeRemaining / 60 < 10 ? " 0" : " "); - lcd.print(timeRemaining / 60); - lcd.print("min"); - lcd.print(timeRemaining % 60 < 10 ? " 0" : " "); - lcd.print(timeRemaining % 60); - lcd.print("sec "); - } - } - break; - case RUN_ALL_ZONES: - // - fastClear(); - lcd.setCursor(0, 0); - if (displayToggle) - { - lcd.print(F(" All-Zone Mode ")); - lcd.setCursor(0, 1); - lcd.print(F(" Zone:")); - if (valveNumber < 10) lcd.print(F("0")); - lcd.print(valveNumber); - lcd.print(F(" Active ")); - } - else - { - lcd.print(F(" Time Remaining ")); - lcd.setCursor(0, 1); - int timeRemaining = (allZoneTime[valveNumber] * 60) - ((millis() - startMillis) / 1000); - lcd.print((timeRemaining / 60) < 10 ? " 0" : " "); - lcd.print(timeRemaining / 60); - lcd.print("min"); - lcd.print(timeRemaining % 60 < 10 ? " 0" : " "); - lcd.print(timeRemaining % 60); - lcd.print("sec "); - } - break; - case CYCLE_COMPLETE: - // - if (displayToggle) - { - lcd.setCursor(0, 0); - lcd.print(F(" Watering Cycle ")); - lcd.setCursor(0, 1); - lcd.print(F(" Complete ")); - } - else - { - int totalTimeRan = 0; - for (int i = 1; i < NUMBER_OF_VALVES + 1; i++) - { - totalTimeRan += allZoneTime[i]; - } - lcd.setCursor(0, 0); - lcd.print(F(" Total Time Run ")); - lcd.setCursor(0, 1); - lcd.print(totalTimeRan < 10 ? " 0" : " "); - lcd.print(totalTimeRan); - lcd.print(" Minutes "); - } - default: - // what of ZONE_SELECT_MENU? - break; - } - lastUpdateTime = millis(); - } - lastDisplayState = state; -} -void receiveTime(time_t newTime) -{ - DEBUG_PRINTLN(F("Time value received and updated...")); - int lastSecond = second(); - int lastMinute = minute(); - int lastHour = hour(); - setTime(newTime); - if (((second() != lastSecond) || (minute() != lastMinute) || (hour() != lastHour)) || showTime) - { - DEBUG_PRINTLN(F("Clock updated....")); - DEBUG_PRINT(F("Sensor's time currently set to:")); - DEBUG_PRINT(hourFormat12() < 10 ? F(" 0") : F(" ")); - DEBUG_PRINT(hourFormat12()); - DEBUG_PRINT(minute() < 10 ? F(":0") : F(":")); - DEBUG_PRINT(minute()); - DEBUG_PRINTLN(isAM() ? F("am") : F("pm")); - DEBUG_PRINT(month()); - DEBUG_PRINT(F("/")); - DEBUG_PRINT(day()); - DEBUG_PRINT(F("/")); - DEBUG_PRINTLN(year()); - DEBUG_PRINTLN(dayOfWeek[weekday()]); - showTime = false; - } - else - { - DEBUG_PRINTLN(F("Sensor's time did NOT need adjustment greater than 1 second.")); - } - clockUpdating = false; -} -void fastClear() -{ - lcd.setCursor(0, 0); - lcd.print(F(" ")); - lcd.setCursor(0, 1); - lcd.print(F(" ")); -} -// -void updateClock() -{ - static unsigned long lastVeraGetTime; - if (millis() - lastVeraGetTime >= 3600000UL) // updates clock time and gets zone times from vera once every hour - { - DEBUG_PRINTLN(F("Requesting time and valve data from Gateway...")); - lcd.setCursor(15, 0); - lcd.write(byte(0)); - clockUpdating = true; - requestTime(); - lastVeraGetTime = millis(); - } -} -// -void saveDateToEEPROM(unsigned long theDate) -{ - DEBUG_PRINTLN(F("Saving Last Run date")); - if (loadState(0) != 0xFF) - { - saveState(0, 0xFF); // EEPROM flag for last date saved stored in EEPROM (location zero) - } - // - for (int i = 1; i < 5; i++) - { - saveState(5 - i, byte(theDate >> 8 * (i - 1))); // store epoch datestamp in 4 bytes of EEPROM starting in location one - } -} -// -void goGetValveTimes() -{ - static unsigned long valveUpdateTime; - static byte valveIndex = 1; - if (inSetup || millis() - valveUpdateTime >= VALVE_TIMES_RELOAD / NUMBER_OF_VALVES) // update each valve once every 5 mins (distributes the traffic) - { - if (inSetup) { - lcd.print(F(" Updating ")); - lcd.setCursor(0, 1); - lcd.print(F(" Valve Data: ")); - lcd.print(valveIndex); - } - bool flashIcon = false; - DEBUG_PRINT(F("Calling for Valve ")); - DEBUG_PRINT(valveIndex); - DEBUG_PRINTLN(F(" Data...")); - for (int a = 0; a < (sizeof(allVars)/sizeof(int)); a++) { - receivedInitialValue = false; - byte timeout = 10; - while (!receivedInitialValue && timeout > 0) - { - lcd.setCursor(15, 0); - flashIcon = !flashIcon; - flashIcon ? lcd.write(byte(1)) : lcd.print(F(" ")); - request(valveIndex, allVars[a]); - wait(50); - timeout--; - } - } - valveUpdateTime = millis(); - valveIndex++; - if (valveIndex > NUMBER_OF_VALVES) - { - valveIndex = 1; - } - } -} +/* +MySprinkler for MySensors + +Arduino Multi-Zone Sprinkler Control + +May 31, 2015 + +*** Version 2.0 + +*** Upgraded to http://MySensors.org version 1.4.1 +*** Expanded for up to 16 Valves +*** Setup for active low relay board or comment out #define ACTIVE_LOW to switch to active high +*** Switch to bitshift method vs byte arrays +*** Changed RUN_ALL_ZONES Vera device to 0 (was highest valve) +*** Added optional LCD display featuring remaining time, date last ran & current time +*** Features 'raindrop' and 'clock' icons which indicate sensor is updating valve data and clock respectively +*** Added single pushbutton menu to manually select which program to run (All Zones or a Single Zone) +*** Added option of naming your Zones programmatically or with Vera (V_VAR3 used to store names) + +Utilizing your Vera home automation controller and the MySensors.org gateway you can +control up to a sixteen zone irrigation system with only three digital pins. This sketch +will create NUMBER_OF_VALVES + 1 devices on your Vera controller + +This sketch features the following: + +* Allows you to cycle through All zones (RUN_ALL_ZONES) or individual zone (RUN_SINGLE_ZONE) control. +* Use the 0th controller to activate RUN_ALL_ZONES (each zone in numeric sequence 1 to n) + using Variable1 as the "ON" time in minutes in each of the vera devices created. +* Use the individual zone controller to activate a single zone. This feature uses + Variable2 as the "ON" time for each individual device/zone. +* Connect according to pinout below and uses Shift Registers as to allow the MySensors + standard radio configuration and still leave available digital pins +* Turning on any zone will stop the current process and begin that particular process. +* Turning off any zone will stop the current process and turn off all zones. +* To push your new time intervals for your zones, simply change the variable on your Vera and + your arduino will call to Vera once a minute and update accordingly. Variables will also be + requested when the device is first powered on. +* Pushbutton activation to RUN_ALL_ZONES, RUN_SINGLE_ZONE or halt the current program +* LED status indicator + +PARTS LIST: +Available from the MySensors store - http://www.mysensors.org/store/ +* Relays (8 channel) +* Female Pin Header Connector Strip +* Prototype Universal Printed Circuit Boards (PCB) +* NRF24L01 Radio +* Arduino (I used a Pro Mini) +* FTDI USB to TTL Serial Adapter +* Capacitors (10uf and .1uf) +* 3.3v voltage regulator +* Resistors (270, 1K & 10K) +* Female Dupont Cables +* 1602 LCD (with I2C Interface) +* LED +* Push button +* Shift Register (SN74HC595) +* 2 Pole 5mm Pitch PCB Mount Screw Terminal Block +* 3 Pole 5mm Pitch PCB Mount Screw Terminal Block +* 22-24 gauge wire or similar (I used Cat5/Cat6 cable) +* 18 gauge wire (for relay) +* Irrigation Power Supply (24-Volt/750 mA Transformer) + + +INSTRUCTIONS: + +* A step-by-step setup video is available here: http://youtu.be/l4GPRTsuHkI +* After assembling your arduino, radio, decoupling capacitors, shift register(s), status LED, pushbutton LCD (I2C connected to + A4 and A5) and relays, and load the sketch. +* Following the instructions at https://MySensors.org include the device to your MySensors Gateway. +* Verify that each new device has a Variable1, Variable2 and Variable3. Populate data accordingly with whole minutes for + the RUN_ALL_ZONES routine (Variable1) and the RUN_SINGLE_ZONE routines (Variable 2). The values entered for times may be zero and + you may use the defaulet zone names by leaving Variable3 blank. +* Once you have entered values for each zone and each variable, save the settings by pressing the red save button on your Vera. +* Restart your arduino; verify the settings are loaded into your arduino with the serial monitor; the array will be printed + on the serial monitor. +* Your arduino should slow-flash, indicating that it is in ready mode. +* There are multiple debug serial prints that can be monitored to assure that it is operating properly. +* ***THIS SHOULD NO LONGER BE NEEDED*** The standard MySensors library now works. https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads for the I2C library, or use yours + +Contributed by Jim (BulldogLowell@gmail.com) with much contribution from Pete (pete.will@mysensors.org) and is released to the public domain +*/ +// + +// Enable debug prints +//#define MY_DEBUG + +// Enable and select radio type attached +#define MY_RADIO_RF24 +//#define MY_RADIO_RFM69 + +//#define MY_NODE_ID 1 // Set this to fix your Radio ID or use Auto + +#include +#include +#include +#include +#include +#include + + +#define NUMBER_OF_VALVES 8 // Change this to set your valve count up to 16. +#define VALVE_RESET_TIME 7500UL // Change this (in milliseconds) for the time you need your valves to hydraulically reset and change state +#define VALVE_TIMES_RELOAD 300000UL // Change this (in milliseconds) for how often to update all valves data from the controller (Loops at value/number valves) + // ie: 300000 for 8 valves produces requests 37.5seconds with all valves updated every 5mins + +#define SKETCH_NAME "MySprinkler" +#define SKETCH_VERSION "2.2" +// +#define CHILD_ID_SPRINKLER 0 +// +#define ACTIVE_LOW // comment out this line if your relays are active high +// +#define DEBUG_ON // comment out to supress serial monitor output +// +#ifdef ACTIVE_LOW +#define BITSHIFT_VALVE_NUMBER ~(1U << (valveNumber-1)) +#define ALL_VALVES_OFF 0xFFFF +#else +#define BITSHIFT_VALVE_NUMBER (1U << (valveNumber-1)) +#define ALL_VALVES_OFF 0U +#endif +// +#ifdef DEBUG_ON +#define DEBUG_PRINT(x) Serial.print(x) +#define DEBUG_PRINTLN(x) Serial.println(x) +#else +#define DEBUG_PRINT(x) +#define DEBUG_PRINTLN(x) +#define SERIAL_START(x) +#endif +// +typedef enum { + STAND_BY_ALL_OFF, RUN_SINGLE_ZONE, RUN_ALL_ZONES, CYCLE_COMPLETE, ZONE_SELECT_MENU +} +SprinklerStates; +// +SprinklerStates state = STAND_BY_ALL_OFF; +SprinklerStates lastState; +byte menuState = 0; +unsigned long menuTimer; +byte countDownTime = 10; +// +int allZoneTime [NUMBER_OF_VALVES + 1]; +int valveSoloTime [NUMBER_OF_VALVES + 1]; +int valveNumber; +int lastValve; +unsigned long startMillis; +const int ledPin = 5; +const int waterButtonPin = 3; +bool buttonPushed = false; +bool showTime = true; +bool clockUpdating = false; +bool recentUpdate = true; +int allVars[] = {V_VAR1, V_VAR2, V_VAR3}; +const char *dayOfWeek[] = { + "Null", "Sunday ", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday " +}; +// Name your Zones here or use Vera to edit them by adding a name in Variable3... +String valveNickName[17] = { + "All Zones", "Zone 1", "Zone 2", "Zone 3", "Zone 4", "Zone 5", "Zone 6", "Zone 7", "Zone 8", "Zone 9", "Zone 10", "Zone 11", "Zone 12", "Zone 13", "Zone 14", "Zone 15", "Zone 16" +}; +// +time_t lastTimeRun = 0; +//Setup Shift Register... +const int latchPin = 8; +const int clockPin = 4; +const int dataPin = 7; +const int outputEnablePin = 6; +// +byte clock[8] = {0x0, 0xe, 0x15, 0x17, 0x11, 0xe, 0x0}; // fetching time indicator +byte raindrop[8] = {0x4, 0x4, 0xA, 0xA, 0x11, 0xE, 0x0,}; // fetching Valve Data indicator +// Set the pins on the I2C chip used for LCD connections: +// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol +LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address to 0x27 +// +MyMessage msg1valve(CHILD_ID_SPRINKLER, V_LIGHT); +MyMessage var1valve(CHILD_ID_SPRINKLER, V_VAR1); +MyMessage var2valve(CHILD_ID_SPRINKLER, V_VAR2); + +bool receivedInitialValue = false; +bool inSetup = true; +// +void setup() +{ + DEBUG_PRINTLN(F("Initialising...")); + pinMode(latchPin, OUTPUT); + pinMode(clockPin, OUTPUT); + pinMode(dataPin, OUTPUT); + pinMode(ledPin, OUTPUT); + pinMode(outputEnablePin, OUTPUT); + digitalWrite (outputEnablePin, LOW); + pinMode(waterButtonPin, INPUT_PULLUP); + //pinMode(waterButtonPin, INPUT); + attachInterrupt(digitalPinToInterrupt(waterButtonPin), PushButton, RISING); //May need to change for your Arduino model + digitalWrite (ledPin, HIGH); + DEBUG_PRINTLN(F("Turning All Valves Off...")); + updateRelays(ALL_VALVES_OFF); + //delay(5000); + lcd.begin(16, 2); //(16 characters and 2 line display) + lcd.clear(); + lcd.backlight(); + lcd.createChar(0, clock); + lcd.createChar(1, raindrop); + // + //check for saved date in EEPROM + DEBUG_PRINTLN(F("Checking EEPROM for stored date:")); + delay(500); + if (loadState(0) == 0xFF) // EEPROM flag + { + DEBUG_PRINTLN(F("Retreiving last run time from EEPROM...")); + for (int i = 0; i < 4 ; i++) + { + lastTimeRun = lastTimeRun << 8; + lastTimeRun = lastTimeRun | loadState(i + 1); // assemble 4 bytes into an ussigned long epoch timestamp + } + } + + DEBUG_PRINTLN(F("Sensor Presentation Complete")); + // + digitalWrite (ledPin, LOW); + DEBUG_PRINTLN(F("Ready...")); + // + lcd.setCursor(0, 0); + lcd.print(F(" Syncing Time ")); + lcd.setCursor(15, 0); + lcd.write(byte(0)); + lcd.setCursor(0, 1); + int clockCounter = 0; + while (timeStatus() == timeNotSet && clockCounter < 21) + { + requestTime(); + DEBUG_PRINTLN(F("Requesting time from Gateway:")); + wait(1000); + lcd.print("."); + clockCounter++; + if (clockCounter > 16) + { + DEBUG_PRINTLN(F("Failed initial clock synchronization!")); + lcd.clear(); + lcd.print(F(" Failed Clock ")); + lcd.setCursor(0, 1); + lcd.print(F(" Syncronization ")); + wait(2000); + break; + } + } + // + //Update valve data when first powered on + for (byte i = 1; i <= NUMBER_OF_VALVES; i++) + { + lcd.clear(); + goGetValveTimes(); + } + lcd.clear(); + inSetup = false; +} + + +void presentation() +{ + sendSketchInfo(SKETCH_NAME, SKETCH_VERSION); + for (byte i = 0; i <= NUMBER_OF_VALVES; i++) + { + present(i, S_LIGHT); + } +} + +// +void loop() +{ + updateClock(); + updateDisplay(); + goGetValveTimes(); + // + if (buttonPushed) + { + menuTimer = millis(); + DEBUG_PRINTLN(F("Button Pressed")); + if (state == STAND_BY_ALL_OFF) + { + state = ZONE_SELECT_MENU; + menuState = 0; + } + else if (state == ZONE_SELECT_MENU) + { + menuState++; + if (menuState > NUMBER_OF_VALVES) + { + menuState = 0; + } + } + else + { + state = STAND_BY_ALL_OFF; + } + buttonPushed = false; + } + if (state == STAND_BY_ALL_OFF) + { + slowToggleLED (); + if (state != lastState) + { + updateRelays(ALL_VALVES_OFF); + DEBUG_PRINTLN(F("State Changed... all Zones off")); + for (byte i = 0; i <= NUMBER_OF_VALVES; i++) + { + wait(50); + send(msg1valve.setSensor(i).set(false), false); + } + lcd.clear(); + lcd.setCursor(0,0); + lcd.print(F("** Irrigation **")); + lcd.setCursor(0,1); + lcd.print(F("** Halted **")); + wait(2000); + lastValve = -1; + } + } + // + else if (state == RUN_ALL_ZONES) + { + if (lastValve != valveNumber) + { + for (byte i = 0; i <= NUMBER_OF_VALVES; i++) + { + if (i == 0 || i == valveNumber) + { + send(msg1valve.setSensor(i).set(true), false); + } + else + { + send(msg1valve.setSensor(i).set(false), false); + } + wait(50); + } + } + lastValve = valveNumber; + fastToggleLed(); + if (state != lastState) + { + valveNumber = 1; + updateRelays(ALL_VALVES_OFF); + DEBUG_PRINTLN(F("State Changed, Running All Zones...")); + } + unsigned long nowMillis = millis(); + if (nowMillis - startMillis < VALVE_RESET_TIME) + { + updateRelays(ALL_VALVES_OFF); + } + else if (nowMillis - startMillis < (allZoneTime[valveNumber] * 60000UL)) + { + updateRelays(BITSHIFT_VALVE_NUMBER); + } + else + { + DEBUG_PRINTLN(F("Changing Valves...")); + updateRelays(ALL_VALVES_OFF); + startMillis = millis(); + valveNumber++; + if (valveNumber > NUMBER_OF_VALVES) + { + state = CYCLE_COMPLETE; + startMillis = millis(); + lastValve = -1; + lastTimeRun = now(); + saveDateToEEPROM(lastTimeRun); + for (byte i = 0; i <= NUMBER_OF_VALVES; i++) + { + send(msg1valve.setSensor(i).set(false), false); + wait(50); + } + DEBUG_PRINT(F("State = ")); + DEBUG_PRINTLN(state); + } + } + } + // + else if (state == RUN_SINGLE_ZONE) + { + fastToggleLed(); + if (state != lastState) + { + for (byte i = 0; i <= NUMBER_OF_VALVES; i++) + { + if (i == 0 || i == valveNumber) + { + send(msg1valve.setSensor(i).set(true), false); + } + else + { + send(msg1valve.setSensor(i).set(false), false); + } + wait(50); + } + DEBUG_PRINTLN(F("State Changed, Single Zone Running...")); + DEBUG_PRINT(F("Zone: ")); + DEBUG_PRINTLN(valveNumber); + } + unsigned long nowMillis = millis(); + if (nowMillis - startMillis < VALVE_RESET_TIME) + { + updateRelays(ALL_VALVES_OFF); + } + else if (nowMillis - startMillis < (valveSoloTime [valveNumber] * 60000UL)) + { + updateRelays(BITSHIFT_VALVE_NUMBER); + } + else + { + updateRelays(ALL_VALVES_OFF); + for (byte i = 0; i <= NUMBER_OF_VALVES; i++) + { + send(msg1valve.setSensor(i).set(false), false); + } + state = CYCLE_COMPLETE; + startMillis = millis(); + DEBUG_PRINT(F("State = ")); + DEBUG_PRINTLN(state); + } + lastTimeRun = now(); + } + else if (state == CYCLE_COMPLETE) + { + if (millis() - startMillis < 30000UL) + { + fastToggleLed(); + } + else + { + state = STAND_BY_ALL_OFF; + } + } + if (state == ZONE_SELECT_MENU) + { + displayMenu(); + } + else + { + lastState = state; + } +} +// +void displayMenu(void) +{ + static byte lastMenuState = -1; + static int lastSecond; + if (menuState != lastMenuState) + { + lcd.clear(); + lcd.setCursor(0, 0); + lcd.print(valveNickName[menuState]); + lcd.setCursor(0, 1); + lcd.print(F("Starting")); + DEBUG_PRINT(valveNickName[menuState]); + Serial.print(F(" Starting Shortly")); + } + int thisSecond = (millis() - menuTimer) / 1000UL; + if (thisSecond != lastSecond && thisSecond < 8) + { + lcd.print(F(".")); + Serial.print("."); + } + lastSecond = thisSecond; + if (millis() - menuTimer > 10000UL) + { + startMillis = millis(); + if (menuState == 0) + { + valveNumber = 1; + state = RUN_ALL_ZONES; + } + else + { + valveNumber = menuState; + state = RUN_SINGLE_ZONE; + } + } + else + { + + } + lastMenuState = menuState; +} +// +void updateRelays(int value) +{ + digitalWrite(latchPin, LOW); + shiftOut(dataPin, clockPin, MSBFIRST, highByte(value)); + shiftOut(dataPin, clockPin, MSBFIRST, lowByte(value)); + digitalWrite(latchPin, HIGH); +} +// +void PushButton() //interrupt with debounce +{ + static unsigned long last_interrupt_time = 0; + unsigned long interrupt_time = millis(); + if (interrupt_time - last_interrupt_time > 200) + { + buttonPushed = true; + } + last_interrupt_time = interrupt_time; +} +// +void fastToggleLed() +{ + static unsigned long fastLedTimer; + if (millis() - fastLedTimer >= 100UL) + { + digitalWrite(ledPin, !digitalRead(ledPin)); + fastLedTimer = millis (); + } +} +// +void slowToggleLED () +{ + static unsigned long slowLedTimer; + if (millis() - slowLedTimer >= 1250UL) + { + digitalWrite(ledPin, !digitalRead(ledPin)); + slowLedTimer = millis (); + } +} +// +void receive(const MyMessage &message) +{ + bool zoneTimeUpdate = false; + if (message.isAck()) + { + DEBUG_PRINTLN(F("This is an ack from gateway")); + } + for (byte i = 0; i <= NUMBER_OF_VALVES; i++) + { + if (message.sensor == i) + { + if (message.type == V_LIGHT) + { + int switchState = atoi(message.data); + if (switchState == 0) + { + state = STAND_BY_ALL_OFF; + DEBUG_PRINTLN(F("Recieved Instruction to Cancel...")); + } + else + { + if (i == 0) + { + state = RUN_ALL_ZONES; + valveNumber = 1; + DEBUG_PRINTLN(F("Recieved Instruction to Run All Zones...")); + } + else + { + state = RUN_SINGLE_ZONE; + valveNumber = i; + DEBUG_PRINT(F("Recieved Instruction to Activate Zone: ")); + DEBUG_PRINTLN(i); + } + } + startMillis = millis(); + } + else if (message.type == V_VAR1) + { + int variable1 = atoi(message.data);// RUN_ALL_ZONES time + DEBUG_PRINT(F("Recieved variable1 valve:")); + DEBUG_PRINT(i); + DEBUG_PRINT(F(" = ")); + DEBUG_PRINTLN(variable1); + if (variable1 != allZoneTime[i]) + { + allZoneTime[i] = variable1; + + zoneTimeUpdate = true; + } + receivedInitialValue = true; + } + else if (message.type == V_VAR2) + { + int variable2 = atoi(message.data);// RUN_SINGLE_ZONE time + DEBUG_PRINT(F("Recieved variable2 valve:")); + DEBUG_PRINT(i); + DEBUG_PRINT(F(" = ")); + DEBUG_PRINTLN(variable2); + if (variable2 != valveSoloTime[i]) + { + valveSoloTime[i] = variable2; + zoneTimeUpdate = true; + } + receivedInitialValue = true; + } + else if (message.type == V_VAR3) + { + String newMessage = String(message.data); + if (newMessage.length() == 0) + { + DEBUG_PRINT(F("No Name Recieved for zone ")); + DEBUG_PRINTLN(i); + break; + } + if (newMessage.length() > 16) + { + newMessage.substring(0, 16); + } + valveNickName[i] = ""; + valveNickName[i] += newMessage; + DEBUG_PRINT(F("Recieved variable3 valve: ")); + DEBUG_PRINT(i); + DEBUG_PRINT(F(" = ")); + DEBUG_PRINTLN(valveNickName[i]); + } + receivedInitialValue = true; + } + } + if (zoneTimeUpdate) + { + // + DEBUG_PRINTLN(F("New Zone Times Recieved...")); + for (byte i = 0; i <= NUMBER_OF_VALVES; i++) + { + if (i != 0) + { + DEBUG_PRINT(F("Zone ")); + DEBUG_PRINT(i); + DEBUG_PRINT(F(" individual time: ")); + DEBUG_PRINT(valveSoloTime[i]); + DEBUG_PRINT(F(" group time: ")); + DEBUG_PRINT(allZoneTime[i]); + DEBUG_PRINT(F(" name: ")); + DEBUG_PRINTLN(valveNickName[i]); + recentUpdate = true; + } + } + } + else + { + recentUpdate = false; + } +} +// +void updateDisplay() +{ + static unsigned long lastUpdateTime; + static bool displayToggle = false; + //static byte toggleCounter = 0; + static SprinklerStates lastDisplayState; + if (state != lastDisplayState || millis() - lastUpdateTime >= 3000UL) + { + displayToggle = !displayToggle; + switch (state) { + case STAND_BY_ALL_OFF: + // + fastClear(); + lcd.setCursor(0, 0); + if (displayToggle) + { + lcd.print(F(" System Ready ")); + if (clockUpdating) + { + lcd.setCursor(15, 0); + lcd.write(byte(0)); + } + lcd.setCursor(0, 1); + lcd.print(hourFormat12() < 10 ? F(" ") : F("")); + lcd.print(hourFormat12()); + lcd.print(minute() < 10 ? F(":0") : F(":")); + lcd.print(minute()); + lcd.print(isAM() ? F("am") : F("pm")); + lcd.print(month() < 10 ? F(" 0") : F(" ")); + lcd.print(month()); + lcd.print(day() < 10 ? F("/0") : F("/")); + lcd.print(day()); + lcd.print(F("/")); + lcd.print(year() % 100); + } + else + { + lcd.print(F(" Last Watered ")); + if (clockUpdating) + { + lcd.setCursor(15, 0); + lcd.write(byte(0)); + } + lcd.setCursor(0, 1); + lcd.print(dayOfWeek[weekday(lastTimeRun)]); + lcd.setCursor(11, 1); + lcd.print(month(lastTimeRun) < 10 ? F(" ") : F("")); + lcd.print(month(lastTimeRun)); + lcd.print(day(lastTimeRun) < 10 ? F("/0") : F("/")); + lcd.print(day(lastTimeRun)); + } + break; + case RUN_SINGLE_ZONE: + // + fastClear(); + lcd.setCursor(0, 0); + if (displayToggle) + { + lcd.print(F("Single Zone Mode")); + lcd.setCursor(0, 1); + lcd.print(F(" Zone:")); + if (valveNumber < 10) lcd.print(F("0")); + lcd.print(valveNumber); + lcd.print(F(" Active")); + } + else + { + lcd.print(F(" Time Remaining ")); + lcd.setCursor(0, 1); + if (valveSoloTime[valveNumber] == 0) + { + lcd.print(F(" No Valve Time ")); + } + else + { + unsigned long timeRemaining = (valveSoloTime[valveNumber] * 60) - ((millis() - startMillis) / 1000); + lcd.print(timeRemaining / 60 < 10 ? " 0" : " "); + lcd.print(timeRemaining / 60); + lcd.print("min"); + lcd.print(timeRemaining % 60 < 10 ? " 0" : " "); + lcd.print(timeRemaining % 60); + lcd.print("sec "); + } + } + break; + case RUN_ALL_ZONES: + // + fastClear(); + lcd.setCursor(0, 0); + if (displayToggle) + { + lcd.print(F(" All-Zone Mode ")); + lcd.setCursor(0, 1); + lcd.print(F(" Zone:")); + if (valveNumber < 10) lcd.print(F("0")); + lcd.print(valveNumber); + lcd.print(F(" Active ")); + } + else + { + lcd.print(F(" Time Remaining ")); + lcd.setCursor(0, 1); + int timeRemaining = (allZoneTime[valveNumber] * 60) - ((millis() - startMillis) / 1000); + lcd.print((timeRemaining / 60) < 10 ? " 0" : " "); + lcd.print(timeRemaining / 60); + lcd.print("min"); + lcd.print(timeRemaining % 60 < 10 ? " 0" : " "); + lcd.print(timeRemaining % 60); + lcd.print("sec "); + } + break; + case CYCLE_COMPLETE: + // + if (displayToggle) + { + lcd.setCursor(0, 0); + lcd.print(F(" Watering Cycle ")); + lcd.setCursor(0, 1); + lcd.print(F(" Complete ")); + } + else + { + int totalTimeRan = 0; + for (int i = 1; i < NUMBER_OF_VALVES + 1; i++) + { + totalTimeRan += allZoneTime[i]; + } + lcd.setCursor(0, 0); + lcd.print(F(" Total Time Run ")); + lcd.setCursor(0, 1); + lcd.print(totalTimeRan < 10 ? " 0" : " "); + lcd.print(totalTimeRan); + lcd.print(" Minutes "); + } + default: + // what of ZONE_SELECT_MENU? + break; + } + lastUpdateTime = millis(); + } + lastDisplayState = state; +} +void receiveTime(time_t newTime) +{ + DEBUG_PRINTLN(F("Time value received and updated...")); + int lastSecond = second(); + int lastMinute = minute(); + int lastHour = hour(); + setTime(newTime); + if (((second() != lastSecond) || (minute() != lastMinute) || (hour() != lastHour)) || showTime) + { + DEBUG_PRINTLN(F("Clock updated....")); + DEBUG_PRINT(F("Sensor's time currently set to:")); + DEBUG_PRINT(hourFormat12() < 10 ? F(" 0") : F(" ")); + DEBUG_PRINT(hourFormat12()); + DEBUG_PRINT(minute() < 10 ? F(":0") : F(":")); + DEBUG_PRINT(minute()); + DEBUG_PRINTLN(isAM() ? F("am") : F("pm")); + DEBUG_PRINT(month()); + DEBUG_PRINT(F("/")); + DEBUG_PRINT(day()); + DEBUG_PRINT(F("/")); + DEBUG_PRINTLN(year()); + DEBUG_PRINTLN(dayOfWeek[weekday()]); + showTime = false; + } + else + { + DEBUG_PRINTLN(F("Sensor's time did NOT need adjustment greater than 1 second.")); + } + clockUpdating = false; +} +void fastClear() +{ + lcd.setCursor(0, 0); + lcd.print(F(" ")); + lcd.setCursor(0, 1); + lcd.print(F(" ")); +} +// +void updateClock() +{ + static unsigned long lastVeraGetTime; + if (millis() - lastVeraGetTime >= 3600000UL) // updates clock time and gets zone times from vera once every hour + { + DEBUG_PRINTLN(F("Requesting time and valve data from Gateway...")); + lcd.setCursor(15, 0); + lcd.write(byte(0)); + clockUpdating = true; + requestTime(); + lastVeraGetTime = millis(); + } +} +// +void saveDateToEEPROM(unsigned long theDate) +{ + DEBUG_PRINTLN(F("Saving Last Run date")); + if (loadState(0) != 0xFF) + { + saveState(0, 0xFF); // EEPROM flag for last date saved stored in EEPROM (location zero) + } + // + for (int i = 1; i < 5; i++) + { + saveState(5 - i, byte(theDate >> 8 * (i - 1))); // store epoch datestamp in 4 bytes of EEPROM starting in location one + } +} +// +void goGetValveTimes() +{ + static unsigned long valveUpdateTime; + static byte valveIndex = 1; + if (inSetup || millis() - valveUpdateTime >= VALVE_TIMES_RELOAD / NUMBER_OF_VALVES) // update each valve once every 5 mins (distributes the traffic) + { + if (inSetup) { + lcd.print(F(" Updating ")); + lcd.setCursor(0, 1); + lcd.print(F(" Valve Data: ")); + lcd.print(valveIndex); + } + bool flashIcon = false; + DEBUG_PRINT(F("Calling for Valve ")); + DEBUG_PRINT(valveIndex); + DEBUG_PRINTLN(F(" Data...")); + for (int a = 0; a < (sizeof(allVars)/sizeof(int)); a++) { + receivedInitialValue = false; + byte timeout = 10; + while (!receivedInitialValue && timeout > 0) + { + lcd.setCursor(15, 0); + flashIcon = !flashIcon; + flashIcon ? lcd.write(byte(1)) : lcd.print(F(" ")); + request(valveIndex, allVars[a]); + wait(50); + timeout--; + } + } + valveUpdateTime = millis(); + valveIndex++; + if (valveIndex > NUMBER_OF_VALVES) + { + valveIndex = 1; + } + } +} diff --git a/examples/LightLuxSensor/LightLuxSensor.ino b/examples/LightLuxSensor/LightLuxSensor.ino index eb4485f..4e8bef0 100644 --- a/examples/LightLuxSensor/LightLuxSensor.ino +++ b/examples/LightLuxSensor/LightLuxSensor.ino @@ -1,88 +1,87 @@ -/** - * 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 - * 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. - * - ******************************* - * - * REVISION HISTORY - * Version 1.0 - idefix - * - * DESCRIPTION - * Arduino BH1750FVI Light sensor - * communicate using I2C Protocol - * this library enable 2 slave device addresses - * Main address 0x23 - * secondary address 0x5C - * connect the sensor as follows : - * - * VCC >>> 5V - * Gnd >>> Gnd - * ADDR >>> NC or GND - * SCL >>> A5 - * SDA >>> A4 - * http://www.mysensors.org/build/light - */ - - -// Enable debug prints to serial monitor -#define MY_DEBUG - -// Enable and select radio type attached -#define MY_RADIO_NRF24 -//#define MY_RADIO_RFM69 - -#include -#include -#include -#include - -#define CHILD_ID_LIGHT 0 -unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds) - -BH1750 lightSensor; - -// V_LIGHT_LEVEL should only be used for uncalibrated light level 0-100%. -// If your controller supports the new V_LEVEL variable, use this instead for -// transmitting LUX light level. -MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL); -// MyMessage msg(CHILD_ID_LIGHT, V_LEVEL); -uint16_t lastlux; - -void setup() -{ - lightSensor.begin(); -} - -void presentation() { - // Send the sketch version information to the gateway and Controller - sendSketchInfo("Light Lux Sensor", "1.0"); - - // Register all sensors to gateway (they will be created as child devices) - present(CHILD_ID_LIGHT, S_LIGHT_LEVEL); -} - -void loop() -{ - uint16_t lux = lightSensor.readLightLevel();// Get Lux value - Serial.println(lux); - if (lux != lastlux) { - send(msg.set(lux)); - lastlux = lux; - } - - sleep(SLEEP_TIME); -} +/** + * 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 + * 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. + * + ******************************* + * + * REVISION HISTORY + * Version 1.0 - idefix + * + * DESCRIPTION + * Arduino BH1750FVI Light sensor + * communicate using I2C Protocol + * this library enable 2 slave device addresses + * Main address 0x23 + * secondary address 0x5C + * connect the sensor as follows : + * + * VCC >>> 5V + * Gnd >>> Gnd + * ADDR >>> NC or GND + * SCL >>> A5 + * SDA >>> A4 + * https://www.mysensors.org/build/light-bh1750 + */ + + +// Enable debug prints to serial monitor +#define MY_DEBUG + +// Enable and select radio type attached +#define MY_RADIO_RF24 +//#define MY_RADIO_RFM69 + +#include +#include +#include + +#define CHILD_ID_LIGHT 0 +unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds) + +BH1750 lightSensor; + +// V_LIGHT_LEVEL should only be used for uncalibrated light level 0-100%. +// If your controller supports the new V_LEVEL variable, use this instead for +// transmitting LUX light level. +MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL); +// MyMessage msg(CHILD_ID_LIGHT, V_LEVEL); +uint16_t lastlux; + +void setup() +{ + lightSensor.begin(); +} + +void presentation() { + // Send the sketch version information to the gateway and Controller + sendSketchInfo("Light Lux Sensor", "1.0"); + + // Register all sensors to gateway (they will be created as child devices) + present(CHILD_ID_LIGHT, S_LIGHT_LEVEL); +} + +void loop() +{ + uint16_t lux = lightSensor.readLightLevel();// Get Lux value + Serial.println(lux); + if (lux != lastlux) { + send(msg.set(lux)); + lastlux = lux; + } + + sleep(SLEEP_TIME); +} diff --git a/examples/LightningSensor/LightningSensor.ino b/examples/LightningSensor/LightningSensor.ino index aded9f0..6640461 100644 --- a/examples/LightningSensor/LightningSensor.ino +++ b/examples/LightningSensor/LightningSensor.ino @@ -1,125 +1,124 @@ -// This sketch is to integrate the Playing With Fusion AXS3935 Lightning Sensor Breakout Board -// with the MySensors environment and is based on a sketch provided by Playing With Fusion -// http://playingwithfusion.com/productview.php?pdid=22&catid=1001 -// - -// Enable debug prints to serial monitor -#define MY_DEBUG - -// Enable and select radio type attached -#define MY_RADIO_NRF24 -//#define MY_RADIO_RFM69 - -#include -#include -// include Playing With Fusion AXS3935 libraries -#include - -// setup CS pins used for the connection with the lightning sensor -// other connections are controlled by the SPI library) -#define CS_PIN 8 -#define SI_PIN 7 -#define IRQ_PIN 3 -volatile int8_t AS3935_ISR_Trig = 0; - -// #defines -#define AS3935_INDOORS 1 -#define AS3935_OUTDOORS 0 -#define AS3935_DIST_DIS 0 -#define AS3935_DIST_EN 1 -#define AS3935_CAPACITANCE 96 // <-- SET THIS VALUE TO THE NUMBER LISTED ON YOUR BOARD -// prototypes -void AS3935_ISR(); - -PWF_AS3935 lightning0(CS_PIN, IRQ_PIN, SI_PIN); - -#define CHILD_ID_DISTANCE 1 -#define CHILD_ID_INTENSITY 2 -MyMessage msgDist(CHILD_ID_DISTANCE, V_DISTANCE); -MyMessage msgInt(CHILD_ID_INTENSITY, V_VAR1); - -void setup() -{ - Serial.println("Playing With Fusion: AS3935 Lightning Sensor, SEN-39001"); - Serial.println("beginning boot procedure...."); - - // setup for the the SPI library: - SPI.begin(); // begin SPI - SPI.setClockDivider(SPI_CLOCK_DIV4); // SPI speed to SPI_CLOCK_DIV16/1MHz (max 2MHz, NEVER 500kHz!) - SPI.setDataMode(SPI_MODE1); // MAX31855 is a Mode 1 device - // --> clock starts low, read on rising edge - SPI.setBitOrder(MSBFIRST); // data sent to chip MSb first - - lightning0.AS3935_DefInit(); // set registers to default - // now update sensor cal for your application and power up chip - lightning0.AS3935_ManualCal(AS3935_CAPACITANCE, AS3935_OUTDOORS, AS3935_DIST_EN); - // AS3935_ManualCal Parameters: - // --> capacitance, in pF (marked on package) - // --> indoors/outdoors (AS3935_INDOORS:0 / AS3935_OUTDOORS:1) - // --> disturbers (AS3935_DIST_EN:1 / AS3935_DIST_DIS:2) - // function also powers up the chip - - // enable interrupt (hook IRQ pin to Arduino Uno/Mega interrupt input: 1 -> pin 3 ) - attachInterrupt(digitalPinToInterrupt(IRQ_PIN), AS3935_ISR, RISING); - // dump the registry data to the serial port for troubleshooting purposes - lightning0.AS3935_PrintAllRegs(); - - // delay execution to allow chip to stabilize. - delay(1000); - -} - -void presentation() { - // Send the sketch version information to the gateway and Controller - sendSketchInfo("Lightning Sensor", "1.1"); - - // Register all sensors to gw (they will be created as child devices) - present(CHILD_ID_DISTANCE, S_DISTANCE); - present(CHILD_ID_INTENSITY, S_CUSTOM); -} - -void loop() -{ - - // This program only handles an AS3935 lightning sensor. It does nothing until - // an interrupt is detected on the IRQ pin. - while(0 == AS3935_ISR_Trig){} - - // reset interrupt flag - AS3935_ISR_Trig = 0; - - // now get interrupt source - uint8_t int_src = lightning0.AS3935_GetInterruptSrc(); - if(0 == int_src) - { - Serial.println("Unknown interrupt source"); - } - else if(1 == int_src) - { - uint8_t lightning_dist_km = lightning0.AS3935_GetLightningDistKm(); - uint32_t lightning_intensity = lightning0.AS3935_GetStrikeEnergyRaw(); - - Serial.print("Lightning detected! Distance to strike: "); - Serial.print(lightning_dist_km); - Serial.println(" kilometers"); - Serial.print("Lightning detected! Lightning Intensity: "); - Serial.println(lightning_intensity); - send(msgDist.set(lightning_dist_km)); - send(msgInt.set(lightning_intensity)); - } - else if(2 == int_src) - { - Serial.println("Disturber detected"); - } - else if(3 == int_src) - { - Serial.println("Noise level too high"); - } -} - -// this is irq handler for AS3935 interrupts, has to return void and take no arguments -// always make code in interrupt handlers fast and short -void AS3935_ISR() -{ - AS3935_ISR_Trig = 1; -} +// This sketch is to integrate the Playing With Fusion AXS3935 Lightning Sensor Breakout Board +// with the MySensors environment and is based on a sketch provided by Playing With Fusion +// http://playingwithfusion.com/productview.php?pdid=22&catid=1001 +// + +// Enable debug prints to serial monitor +#define MY_DEBUG + +// Enable and select radio type attached +#define MY_RADIO_RF24 +//#define MY_RADIO_RFM69 + +#include +// include Playing With Fusion AXS3935 libraries +#include + +// setup CS pins used for the connection with the lightning sensor +// other connections are controlled by the SPI library) +#define CS_PIN 8 +#define SI_PIN 7 +#define IRQ_PIN 3 +volatile int8_t AS3935_ISR_Trig = 0; + +// #defines +#define AS3935_INDOORS 1 +#define AS3935_OUTDOORS 0 +#define AS3935_DIST_DIS 0 +#define AS3935_DIST_EN 1 +#define AS3935_CAPACITANCE 96 // <-- SET THIS VALUE TO THE NUMBER LISTED ON YOUR BOARD +// prototypes +void AS3935_ISR(); + +PWF_AS3935 lightning0(CS_PIN, IRQ_PIN, SI_PIN); + +#define CHILD_ID_DISTANCE 1 +#define CHILD_ID_INTENSITY 2 +MyMessage msgDist(CHILD_ID_DISTANCE, V_DISTANCE); +MyMessage msgInt(CHILD_ID_INTENSITY, V_VAR1); + +void setup() +{ + Serial.println("Playing With Fusion: AS3935 Lightning Sensor, SEN-39001"); + Serial.println("beginning boot procedure...."); + + // setup for the the SPI library: + SPI.begin(); // begin SPI + SPI.setClockDivider(SPI_CLOCK_DIV4); // SPI speed to SPI_CLOCK_DIV16/1MHz (max 2MHz, NEVER 500kHz!) + SPI.setDataMode(SPI_MODE1); // MAX31855 is a Mode 1 device + // --> clock starts low, read on rising edge + SPI.setBitOrder(MSBFIRST); // data sent to chip MSb first + + lightning0.AS3935_DefInit(); // set registers to default + // now update sensor cal for your application and power up chip + lightning0.AS3935_ManualCal(AS3935_CAPACITANCE, AS3935_OUTDOORS, AS3935_DIST_EN); + // AS3935_ManualCal Parameters: + // --> capacitance, in pF (marked on package) + // --> indoors/outdoors (AS3935_INDOORS:0 / AS3935_OUTDOORS:1) + // --> disturbers (AS3935_DIST_EN:1 / AS3935_DIST_DIS:2) + // function also powers up the chip + + // enable interrupt (hook IRQ pin to Arduino Uno/Mega interrupt input: 1 -> pin 3 ) + attachInterrupt(digitalPinToInterrupt(IRQ_PIN), AS3935_ISR, RISING); + // dump the registry data to the serial port for troubleshooting purposes + lightning0.AS3935_PrintAllRegs(); + + // delay execution to allow chip to stabilize. + delay(1000); + +} + +void presentation() { + // Send the sketch version information to the gateway and Controller + sendSketchInfo("Lightning Sensor", "1.1"); + + // Register all sensors to gw (they will be created as child devices) + present(CHILD_ID_DISTANCE, S_DISTANCE); + present(CHILD_ID_INTENSITY, S_CUSTOM); +} + +void loop() +{ + + // This program only handles an AS3935 lightning sensor. It does nothing until + // an interrupt is detected on the IRQ pin. + while(0 == AS3935_ISR_Trig){} + + // reset interrupt flag + AS3935_ISR_Trig = 0; + + // now get interrupt source + uint8_t int_src = lightning0.AS3935_GetInterruptSrc(); + if(0 == int_src) + { + Serial.println("Unknown interrupt source"); + } + else if(1 == int_src) + { + uint8_t lightning_dist_km = lightning0.AS3935_GetLightningDistKm(); + uint32_t lightning_intensity = lightning0.AS3935_GetStrikeEnergyRaw(); + + Serial.print("Lightning detected! Distance to strike: "); + Serial.print(lightning_dist_km); + Serial.println(" kilometers"); + Serial.print("Lightning detected! Lightning Intensity: "); + Serial.println(lightning_intensity); + send(msgDist.set(lightning_dist_km)); + send(msgInt.set(lightning_intensity)); + } + else if(2 == int_src) + { + Serial.println("Disturber detected"); + } + else if(3 == int_src) + { + Serial.println("Noise level too high"); + } +} + +// this is irq handler for AS3935 interrupts, has to return void and take no arguments +// always make code in interrupt handlers fast and short +void AS3935_ISR() +{ + AS3935_ISR_Trig = 1; +} diff --git a/examples/MAX6875TemperatureSensor/MAX6875TemperatureSensor.ino b/examples/MAX6875TemperatureSensor/MAX6875TemperatureSensor.ino old mode 100644 new mode 100755 index a2f91fb..80308d2 --- a/examples/MAX6875TemperatureSensor/MAX6875TemperatureSensor.ino +++ b/examples/MAX6875TemperatureSensor/MAX6875TemperatureSensor.ino @@ -1,87 +1,86 @@ -/** - * 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 - * 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. - * - ******************************* - * - * REVISION HISTORY - * Version 1.0 - MrLynx - * - * DESCRIPTION - * MAX6875 High temperature sensor with range: -200 degC - +1300 degC - * http://forum.mysensors.org/topic/641/max6675 - * - * Connect MAX6675 - Arduino - * CS0 - 4 - * SO - 3 - * SCLK - 5 - * VCC - 5V - * GND - GND - * - */ - - -// Enable debug prints to serial monitor -#define MY_DEBUG - -// Enable and select radio type attached -#define MY_RADIO_NRF24 -//#define MY_RADIO_RFM69 - -#include -#include -#include - -uint8_t CS0 = 4; // CS pin on MAX6675 -uint8_t SO = 3; // SO pin of MAX6675 -uint8_t SCLK = 5; // SCK pin of MAX6675 -uint8_t units = 1; // Units to readout temp (0 = ËšF, 1 = ËšC) -float temperature = 0.0; // Temperature output variable -float lastTemperature; -unsigned long SLEEP_TIME = 30000; -bool metric = true; - -MyMessage msg(0, V_TEMP); - -// Initialize the MAX6675 Library for our chip - -MAX6675 temp0(CS0, SO, SCLK, units); - -void presentation() { - // Send the sketch version information to the gateway and Controller - sendSketchInfo("Max6675 Temp Sensor", "1.0"); - - // Present all sensors to controller - present(0, S_TEMP); -} - -void loop() -{ - temperature = temp0.read_temp(); // Read the temp - - if (temperature < 0) { // If there is an error with the TC, temperature will be < 0 - Serial.println("Thermocouple Error!!"); // There is a thermocouple error - } else { - Serial.print("Current Temperature: "); - Serial.println( temperature ); // Print the temperature to Serial - if (temperature != lastTemperature) - send(msg.setSensor(0).set(temperature, 1)); - lastTemperature = temperature; - } - - sleep(SLEEP_TIME); -} +/** + * 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 + * 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. + * + ******************************* + * + * REVISION HISTORY + * Version 1.0 - MrLynx + * + * DESCRIPTION + * MAX6875 High temperature sensor with range: -200 degC - +1300 degC + * http://forum.mysensors.org/topic/641/max6675 + * + * Connect MAX6675 - Arduino + * CS0 - 4 + * SO - 3 + * SCLK - 5 + * VCC - 5V + * GND - GND + * + */ + + +// Enable debug prints to serial monitor +#define MY_DEBUG + +// Enable and select radio type attached +#define MY_RADIO_RF24 +//#define MY_RADIO_RFM69 + +#include +#include + +uint8_t CS0 = 4; // CS pin on MAX6675 +uint8_t SO = 3; // SO pin of MAX6675 +uint8_t SCLK = 5; // SCK pin of MAX6675 +uint8_t units = 1; // Units to readout temp (0 = ËšF, 1 = ËšC) +float temperature = 0.0; // Temperature output variable +float lastTemperature; +unsigned long SLEEP_TIME = 30000; +bool metric = true; + +MyMessage msg(0, V_TEMP); + +// Initialize the MAX6675 Library for our chip + +MAX6675 temp0(CS0, SO, SCLK, units); + +void presentation() { + // Send the sketch version information to the gateway and Controller + sendSketchInfo("Max6675 Temp Sensor", "1.0"); + + // Present all sensors to controller + present(0, S_TEMP); +} + +void loop() +{ + temperature = temp0.read_temp(); // Read the temp + + if (temperature < 0) { // If there is an error with the TC, temperature will be < 0 + Serial.println("Thermocouple Error!!"); // There is a thermocouple error + } else { + Serial.print("Current Temperature: "); + Serial.println( temperature ); // Print the temperature to Serial + if (temperature != lastTemperature) + send(msg.setSensor(0).set(temperature, 1)); + lastTemperature = temperature; + } + + sleep(SLEEP_TIME); +} diff --git a/examples/MH-Z19 CO2 sensor b/examples/MH-Z19 CO2 sensor index 1dae87e..8456db3 100644 --- a/examples/MH-Z19 CO2 sensor +++ b/examples/MH-Z19 CO2 sensor @@ -33,7 +33,7 @@ #define MY_DEBUG // Enable and select radio type attached -#define MY_RADIO_NRF24 // A 2.4Ghz transmitter and receiver, often used with MySensors. +#define MY_RADIO_RF24 // A 2.4Ghz transmitter and receiver, often used with MySensors. // #define MY_RF24_PA_LEVEL RF24_PA_MIN // This sets a low-power mode for the radio. Useful if you use the verison with the bigger antenna, but don't want to power that from a separate power source. It can also fix problems with fake Chinese versions of the radio. // #define MY_RADIO_RFM69 // 433Mhz transmitter and reveiver. diff --git a/examples/MotionSensorBatteryReport/MotionSensorBatteryReport.ino b/examples/MotionSensorBatteryReport/MotionSensorBatteryReport.ino new file mode 100644 index 0000000..35ca94d --- /dev/null +++ b/examples/MotionSensorBatteryReport/MotionSensorBatteryReport.ino @@ -0,0 +1,106 @@ +/* + * 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 + * Copyright (C) 2013-2019 Sensnology AB + * Full contributor list: https://github.com/mysensors/MySensors/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. + * + ******************************* + * + * REVISION HISTORY + * Version 1.0 - Henrik Ekblad + * Version 1.1 - Ivo Pullens, added battery voltage reporting roughly every 24h + * + * DESCRIPTION + * Motion Sensor example using HC-SR501. Requires at least MySensors 2.3.2 ! + * http://www.mysensors.org/build/motion + * Every 24 hours the battery level will be reported. + * Battery power will be measured as described here. + * https://www.mysensors.org/build/humidity_si7021 + */ + +// Enable debug prints +// #define MY_DEBUG + +// Enable and select radio type attached +#define MY_RADIO_RF24 +//#define MY_RADIO_NRF5_ESB +//#define MY_RADIO_RFM69 +//#define MY_RADIO_RFM95 + +#include +#include // Install from https://github.com/Yveaux/Arduino_Vcc + +const float VccMin = 1.8; // Minimum expected Vcc level, in Volts: Brownout at 1.8V -> 0% +const float VccMax = 2.0*1.6; // Maximum expected Vcc level, in Volts: 2xAA fresh Alkaline -> 100% +const float VccCorrection = 1.0; // Measured Vcc by multimeter divided by reported Vcc +static Vcc vcc(VccCorrection); + +#define SLEEP_TIME_MS (24ul*60ul*60ul*1000ul) // Sleep time between battery reports (24h, in milliseconds) +#define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your motion sensor. (Only 2 and 3 generates interrupt!) +#define CHILD_ID 1 // Id of the sensor child + +// Initialize motion message +MyMessage msg(CHILD_ID, V_TRIPPED); + +void setup() +{ + pinMode(DIGITAL_INPUT_SENSOR, INPUT); // sets the motion sensor digital pin as input +} + +void presentation() +{ + // Send the sketch version information to the gateway and Controller + sendSketchInfo(F("Motion Sensor"), F("1.1")); + + // Register all sensors to gw (they will be created as child devices) + present(CHILD_ID, S_MOTION, F("Motion detected")); +} + +void loop() +{ + static uint32_t sleepTimeMs = SLEEP_TIME_MS; + + // Sleep until interrupt comes in on motion sensor, or if sleepTimeMs elapsed. + sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), RISING, sleepTimeMs); + + // Read digital motion value + const bool tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; + + Serial.print(F("Motion tripped ")); + Serial.println(tripped); + send(msg.set(tripped?"1":"0")); // Send tripped value to gw + + // Request how much of the sleep time is remaining + sleepTimeMs = getSleepRemaining(); + + Serial.print(F("Remaining sleep time [ms] ")); + Serial.println(sleepTimeMs); + + if (0ul == sleepTimeMs) + { + // Report battery level and restart the cycle + const uint8_t batteryPcnt = static_cast(0.5 + vcc.Read_Perc(VccMin, VccMax)); + sendBatteryLevel(batteryPcnt); + + Serial.print(F("Vbat ")); + Serial.print(vcc.Read_Volts()); + Serial.print(F("\tPerc ")); + Serial.println(batteryPcnt); + + sleepTimeMs = SLEEP_TIME_MS; + } +} + + diff --git a/examples/OregonSensorDecode/OregonSensorDecode.ino b/examples/OregonSensorDecode/OregonSensorDecode.ino index 4795526..396fc2e 100644 --- a/examples/OregonSensorDecode/OregonSensorDecode.ino +++ b/examples/OregonSensorDecode/OregonSensorDecode.ino @@ -49,7 +49,7 @@ #define MY_NODE_ID 10 // Enable and select radio type attached -#define MY_RADIO_NRF24 +#define MY_RADIO_RF24 //#define MY_RADIO_RFM69 #include diff --git a/examples/PMS-7003 dust sensor b/examples/PMS-7003 dust sensor index 85a08d0..ceffbb2 100644 --- a/examples/PMS-7003 dust sensor +++ b/examples/PMS-7003 dust sensor @@ -53,7 +53,7 @@ #define MY_DEBUG // Enable and select radio type attached -#define MY_RADIO_NRF24 // A 2.4Ghz transmitter and receiver, often used with MySensors. +#define MY_RADIO_RF24 // A 2.4Ghz transmitter and receiver, often used with MySensors. #define MY_RF24_PA_LEVEL RF24_PA_MIN // This sets a low-power mode for the radio. Useful if you use the verison with the bigger antenna, but don't want to power that from a separate power source. It can also fix problems with fake Chinese versions of the radio. // #define MY_RADIO_RFM69 // 433Mhz transmitter and reveiver. diff --git a/examples/ParkingSensor/ParkingSensor.ino b/examples/ParkingSensor/ParkingSensor.ino old mode 100644 new mode 100755 index 69ccc82..1de034a --- a/examples/ParkingSensor/ParkingSensor.ino +++ b/examples/ParkingSensor/ParkingSensor.ino @@ -1,181 +1,180 @@ -/** - * 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 - * 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. - * - ******************************* - * - * REVISION HISTORY - * Version 1.0 - Created by Henrik Ekblad - * - * DESCRIPTION - * Parking sensor using a neopixel led ring and distance sensor (HC-SR04). - * Configure the digital pins used for distance sensor and neopixels below. - * NOTE! Remeber to feed leds and distance sensor serparatly from your Arduino. - * It will probably not survive feeding more than a couple of LEDs. You - * can also adjust intesity below to reduce the power requirements. - * - * Sends parking status to the controller as a DOOR sensor if SEND_STATUS_TO_CONTROLLER - * is defined below. You can also use this _standalone_ without any radio by - * removing the SEND_STATUS_TO_CONTROLLER define. - */ - -#define SEND_STATUS_TO_CONTROLLER // Put a comment on this line for standalone mode - -#include -#include - -#ifdef SEND_STATUS_TO_CONTROLLER -// Enable debug prints to serial monitor -#define MY_DEBUG - -// Enable and select radio type attached -#define MY_RADIO_NRF24 -//#define MY_RADIO_RFM69 - -#include -#include -#endif - -#define NEO_PIN 4 // NeoPixels input pin - -#define TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor. -#define ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor. - -#define NUMPIXELS 24 // Number of nexpixels in ring/strip -#define MAX_INTESITY 20 // Intesity of leds (in percentage). Remeber more intesity requires more power. - -// The maximum rated measuring range for the HC-SR04 is about 400-500cm. -#define MAX_DISTANCE 100 // Max distance we want to start indicating green (in cm) -#define PANIC_DISTANCE 5 // Mix distance we red warning indication should be active (in cm) -#define PARKED_DISTANCE 20 // Distance when "parked signal" should be sent to controller (in cm) - -#define PARK_OFF_TIMEOUT 20000 // Number of milliseconds until turning off light when parked. - -// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest -// example for more information on possible values. - -Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, NEO_PIN, NEO_GRB + NEO_KHZ800); - -NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. - -#ifdef SEND_STATUS_TO_CONTROLLER -#define CHILD_ID 1 -MyMessage msg(CHILD_ID,V_TRIPPED); -#endif -unsigned long sendInterval = 5000; // Send park status at maximum every 5 second. -unsigned long lastSend; - -int oldParkedStatus=-1; - -unsigned long blinkInterval = 100; // blink interval (milliseconds) -unsigned long lastBlinkPeriod; -bool blinkColor = true; - -// To make a fading motion on the led ring/tape we only move one pixel/distDebounce time -unsigned long distDebounce = 30; -unsigned long lastDebouncePeriod; -int numLightPixels=0; -int skipZero=0; - -void setup() { - Serial.begin(115200); - Serial.println("Starting distance sensor"); - pixels.begin(); // This initializes the NeoPixel library. - Serial.println("Neopixels initialized"); -} - -#ifdef SEND_STATUS_TO_CONTROLLER -void presentation() { - sendSketchInfo("Parking Sensor", "1.0"); - present(CHILD_ID, S_DOOR, "Parking Status"); -} -#endif - -void loop() { - unsigned long now = millis(); - unsigned int fullDist = (sonar.ping_median() / US_ROUNDTRIP_CM); -// Serial.println(fullDist); - int displayDist = min(fullDist, MAX_DISTANCE); - if (displayDist == 0 && skipZero<10) { - // Try to filter zero readings - skipZero++; - return; - } - // Check if it is time to alter the leds - if (now-lastDebouncePeriod > distDebounce) { - lastDebouncePeriod = now; - - // Update parked status - int parked = displayDist != 0 && displayDist sendInterval) { - if (parked) - Serial.println("Car Parked"); - else - Serial.println("Car Gone"); -#ifdef SEND_STATUS_TO_CONTROLLER - send(msg.set(parked)); -#endif - oldParkedStatus = parked; - lastSend = now; - } - - if (parked && now-lastSend > PARK_OFF_TIMEOUT) { - // We've been parked for a while now. Turn off all pixels - for(int i=0;inumLightPixels) { - // Fast raise - numLightPixels += max((newLightPixels - numLightPixels) / 2, 1); - } else if (newLightPixels=NUMPIXELS) { - // Do some intense red blinking - if (now-lastBlinkPeriod > blinkInterval) { - blinkColor = !blinkColor; - lastBlinkPeriod = now; - } - for(int i=0;i + * 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. + * + ******************************* + * + * REVISION HISTORY + * Version 1.0 - Created by Henrik Ekblad + * + * DESCRIPTION + * Parking sensor using a neopixel led ring and distance sensor (HC-SR04). + * Configure the digital pins used for distance sensor and neopixels below. + * NOTE! Remeber to feed leds and distance sensor serparatly from your Arduino. + * It will probably not survive feeding more than a couple of LEDs. You + * can also adjust intesity below to reduce the power requirements. + * + * Sends parking status to the controller as a DOOR sensor if SEND_STATUS_TO_CONTROLLER + * is defined below. You can also use this _standalone_ without any radio by + * removing the SEND_STATUS_TO_CONTROLLER define. + */ + +#define SEND_STATUS_TO_CONTROLLER // Put a comment on this line for standalone mode + +#include +#include + +#ifdef SEND_STATUS_TO_CONTROLLER +// Enable debug prints to serial monitor +#define MY_DEBUG + +// Enable and select radio type attached +#define MY_RADIO_RF24 +//#define MY_RADIO_RFM69 + +#include +#endif + +#define NEO_PIN 4 // NeoPixels input pin + +#define TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor. +#define ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor. + +#define NUMPIXELS 24 // Number of nexpixels in ring/strip +#define MAX_INTESITY 20 // Intesity of leds (in percentage). Remeber more intesity requires more power. + +// The maximum rated measuring range for the HC-SR04 is about 400-500cm. +#define MAX_DISTANCE 100 // Max distance we want to start indicating green (in cm) +#define PANIC_DISTANCE 5 // Mix distance we red warning indication should be active (in cm) +#define PARKED_DISTANCE 20 // Distance when "parked signal" should be sent to controller (in cm) + +#define PARK_OFF_TIMEOUT 20000 // Number of milliseconds until turning off light when parked. + +// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest +// example for more information on possible values. + +Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, NEO_PIN, NEO_GRB + NEO_KHZ800); + +NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. + +#ifdef SEND_STATUS_TO_CONTROLLER +#define CHILD_ID 1 +MyMessage msg(CHILD_ID,V_TRIPPED); +#endif +unsigned long sendInterval = 5000; // Send park status at maximum every 5 second. +unsigned long lastSend; + +int oldParkedStatus=-1; + +unsigned long blinkInterval = 100; // blink interval (milliseconds) +unsigned long lastBlinkPeriod; +bool blinkColor = true; + +// To make a fading motion on the led ring/tape we only move one pixel/distDebounce time +unsigned long distDebounce = 30; +unsigned long lastDebouncePeriod; +int numLightPixels=0; +int skipZero=0; + +void setup() { + Serial.begin(115200); + Serial.println("Starting distance sensor"); + pixels.begin(); // This initializes the NeoPixel library. + Serial.println("Neopixels initialized"); +} + +#ifdef SEND_STATUS_TO_CONTROLLER +void presentation() { + sendSketchInfo("Parking Sensor", "1.0"); + present(CHILD_ID, S_DOOR, "Parking Status"); +} +#endif + +void loop() { + unsigned long now = millis(); + unsigned int fullDist = (sonar.ping_median() / US_ROUNDTRIP_CM); +// Serial.println(fullDist); + int displayDist = min(fullDist, MAX_DISTANCE); + if (displayDist == 0 && skipZero<10) { + // Try to filter zero readings + skipZero++; + return; + } + // Check if it is time to alter the leds + if (now-lastDebouncePeriod > distDebounce) { + lastDebouncePeriod = now; + + // Update parked status + int parked = displayDist != 0 && displayDist sendInterval) { + if (parked) + Serial.println("Car Parked"); + else + Serial.println("Car Gone"); +#ifdef SEND_STATUS_TO_CONTROLLER + send(msg.set(parked)); +#endif + oldParkedStatus = parked; + lastSend = now; + } + + if (parked && now-lastSend > PARK_OFF_TIMEOUT) { + // We've been parked for a while now. Turn off all pixels + for(int i=0;inumLightPixels) { + // Fast raise + numLightPixels += max((newLightPixels - numLightPixels) / 2, 1); + } else if (newLightPixels=NUMPIXELS) { + // Do some intense red blinking + if (now-lastBlinkPeriod > blinkInterval) { + blinkColor = !blinkColor; + lastBlinkPeriod = now; + } + for(int i=0;i + * 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. + * + ******************************* + * + * DESCRIPTION + * + * This node can measure the moisture of 6 different plants. It uses the cheap 'capacitive analog + * moisture sensor' that you can get for about 3 dollars an Aliexpress or eBay. For example: + * https://www.aliexpress.com/item/Analog-Capacitive-Soil-Moisture-Sensor-V1-2-Corrosion-Resistant-Z09-Drop-ship/32858273308.html + * + * Each plant' moisture value can also be responded to individually, either by turning on an LED (wire that to the plan, and you can see which one is thirsty) or, if you want, per-plant automated irrigation by connecting a little solenoid.. + * + * Todo: Allow the controller to set the threshold values for each plant individually. Unfortunately, Domoticz doesn't support this yet :-( + * + */ + +//#define MY_SIGNING_SIMPLE_PASSWD "changeme" // Useful if your MySensors network is encrypted. +#define MY_SPLASH_SCREEN_DISABLED // saves a little memory. +//#define MY_DISABLE_RAM_ROUTING_TABLE_FEATURE // saves a little memory. + +//#define MY_NODE_ID 60 // Optional. Sets fixed id with controller. +//#define MY_PARENT_NODE_ID 0 // Optional. Sets fixed id for controller. +//#define MY_PARENT_NODE_IS_STATIC // Optional. Sets fixed id for controller. + +#define MY_TRANSPORT_WAIT_READY_MS 10000 // try connecting to the gateway for 10 seconds. Otherwise just continue. + +// Enable debug prints to serial monitor +// #define MY_DEBUG + +// Enable and select radio type attached +#define MY_RADIO_RF24 +//#define MY_RADIO_NRF5_ESB +//#define MY_RADIO_RFM69 +//#define MY_RADIO_RFM95 + +#define MY_RF24_PA_LEVEL RF24_PA_LOW // Low power radio setting works better with cheap Chinese radios. + +#include + +#define NUMBEROFSENSORS 6 // How many sensors are connected? +#define DRYNESSTHRESHOLD 45 // minimum moisture level that is still ok. A lower value will trigger LED/irrigation. + +#define SLEEPTIME 60 // sleep time between the sending of data (in SECONDS). Maximum is 254 seconds. Change "byte" to "int" further down in the code if you want more time between sending updates. +unsigned long lastTimeChecked = 0; // used by the measurement timer. + +MyMessage msg(0, V_LEVEL); // used to send data to the gateway. + +void before() +{ + for (byte i = 3; i < NUMBEROFSENSORS + 3; i++){ // Set the LED (or irrigation valves) to their initial position. Because Mysensors uses pin 2, we use pin 3 till 8 as output. + pinMode(i, OUTPUT); + digitalWrite(i, LOW); + } +} + +void presentation() +{ + // send the sketch version information to the gateway and Controller + sendSketchInfo(F("Plant Sensorium"), F("1.0")); + // present the sensors + for (byte i=0; i 1000) { + lastTimeChecked = millis(); + + Serial.println(F("__________")); + + for (int i=0; i= DRYNESSTHRESHOLD + 10){ // turn of the water/led if the plant is wet enough. + digitalWrite(shiftedDigitalPin, LOW); + } + + if(measurementCounter < NUMBEROFSENSORS){ // During the first 6 seconds the script will send updated data. + if(measurementCounter == i){ // it sends sensor 0 at second 0. Sensor 1 at second 1, etc. This keeps the radio happy. + send(msg.setSensor(i).set(moistureLevel)); + } + } + if(measurementCounter > SLEEPTIME){ // If enough time has passed, the counter is reset, and new data is sent. + measurementCounter = 0; + }else{ + measurementCounter++; + } + } + + } +} diff --git a/examples/PressureSensor/PressureSensor.ino b/examples/PressureSensor/PressureSensor.ino index e810d20..f58e2ac 100644 --- a/examples/PressureSensor/PressureSensor.ino +++ b/examples/PressureSensor/PressureSensor.ino @@ -20,286 +20,402 @@ * * REVISION HISTORY * Version 1.0 - Henrik Ekblad + * Version 1.1 - AnonymousZebra * * DESCRIPTION - * Pressure sensor example using BMP085 module - * http://www.mysensors.org/build/pressure + * This is an example of using the Bosch BME280 module, which can measure temperature, humidity and airpressure, + * and do so really accurately, while using very little power. A 3.3v and a 5v version is available, make sure to check which one you have. + * It communicates over the I2C protocol. + * + * This script uses the BME280 library by Embedded Adventures. Download it, and place it in your Arduino library folder. + * https://github.com/embeddedadventures/BME280 + * + * Connect your sensor's powerlines, and connect your sensor to the SDA and SCL pins of your board. + * On Arduino Nano SDA is pin A4, and SCL is pin A5. + * On the Ardunio Mega and Due the SDA in pin 20, and the SCL is pin 21. + * On the Arduino Leonardo and Pro Micro 2 the SDA in pin 2, and the SCL is pin 3. + * + * This script has been written in such a way that it can at the same time function as a repeater-node. + * It can also easily be used on battery power. Booth features can be turned on in the code below. + * + * Finally, you can decide if you want the forecast feature to be turned on. This is a cool feature, + * but there is a catch: it also means that you are locked into taking a measurement exactly once a + * minute, to build up prediction data for the algorithm. + * + * The reason so many variables have BME280 at the beginning, is so that it is easier to combine multiple sensors on one Arduino. + * + * + * The BME280 datasheet: https://cdn.sparkfun.com/assets/learn_tutorials/4/1/9/BST-BME280_DS001-10.pdf * */ -// Enable debug prints to serial monitor +// if you uncomment this, you can get test and debug updates about everything the sensor is doing by using the serial monitor tool. #define MY_DEBUG // Enable and select radio type attached -#define MY_RADIO_NRF24 -//#define MY_RADIO_RFM69 - -#include -#include -#include -#include - -#define BARO_CHILD 0 -#define TEMP_CHILD 1 - -const float ALTITUDE = 688; // <-- adapt this value to your own location's altitude. - -// Sleep time between reads (in seconds). Do not change this value as the forecast algorithm needs a sample every minute. -const unsigned long SLEEP_TIME = 60000; - +#define MY_RADIO_RF24 // A 2.4Ghz transmitter and receiver, often used with MySensors. +// #define MY_RF24_PA_LEVEL RF24_PA_MIN // This sets a low-power mode for the radio. Useful if you use the version with the bigger antenna, but don't want to power that from a separate power source. It can also fix problems with fake Chinese versions of the radio. +// #define MY_RADIO_RFM69 // 433Mhz transmitter and reveiver. + +// Do you want this sensor to also be a repeater? +// #define MY_REPEATER_FEATURE // Just remove the two slashes at the beginning of this line to also enable this sensor to act as a repeater for other sensors. If this node is on battery power, you probably shouldn't enable this. + +// Are you using this sensor on battery power? +// #define BATTERY_POWERED // Just remove the two slashes at the beginning of this line if your node is battery powered. It will then go into deep sleep as much as possible. While it's sleeping it can't work as a repeater! + +// Would you like the sensor to generate a weather forecast based on the barometric pressure? +#define GENERATE_FORECAST + + +// LIBRARIES +#include // A communication backbone, the Serial Peripheral Interface. +#include // The MySensors library. Hurray! +#include // Enables the Wire communication protocol. +//#include // alternative library you could try (DIY; no code for this is in here yet). +//#include // alternative library you could try (DIY; no code for this is in here yet). +#include // Bosch BME280 Embedded Adventures MOD-1022 weather multi-sensor Arduino code, written originally by Embedded Adventures. https://github.com/embeddedadventures/BME280 + + +// VARIABLES YOU CAN CHANGE +const float ALTITUDE = 14; // Change this value to your location's altitude (in m). Use your smartphone GPS to get an accurate value, or use an online map. +unsigned long BME280measurementInterval = 60000; // Sleep time between reads for the BME sensor (in ms). Keep this value at 60000 if you have enabled the forecast feature, as the forecast algorithm needs a sample every minute. +#define COMPARE_TEMP 1 // Send temperature only if it changed? 1 = Yes 0 = No. Can save battery. +float tempThreshold = 0.1; // How big a temperature difference has to minimally be before an update is sent. Makes the sensor less precise, but also less jittery, and can save battery. +#define COMPARE_HUM 1 // Send humidity only if changed? 1 = Yes 0 = No. Can save battery. +float humThreshold = 0.1; // How big a humidity difference has to minimally be before an update is sent. Makes the sensor less precise, but also less jittery, and can save battery. +#define COMPARE_BARO 1 // Send barometric pressure only if changed? 1 = Yes 0 = No. Can save battery. +float presThreshold = 0.1; // How big a barometric difference has to minimally be before an update is sent. Makes the sensor less precise, but also less jittery, and can save battery. + + +// VARIABLES YOU PROBABLY SHOULDN'T CHANGE +#define TEMP_CHILD_ID 0 // for MySensors. Within this node each sensortype should have its own ID number. +#define HUM_CHILD_ID 1 // for MySensors. Within this node each sensortype should have its own ID number. +#define BARO_CHILD_ID 2 // for MySensors. Within this node each sensortype should have its own ID number. +float lastTemperature = -1; // Stores the previous measurement, so it can be compared with a new measurement. +float lastHumidity = -1; // Stores the previous measurement, so it can be compared with a new measurement. +float lastPressure = -1; // Stores the previous measurement, so it can be compared with a new measurement. +unsigned long BME280measurementSleepTime = 0; // variable to store the calculated Sleep time if the node is battery powered. +bool metric = true; // Variable that stores if the sensor will output the temperature in Fahrenheit of Celsius. The gateway sends this preference to the node, so you dont need to change it here. +bool receivedConfig = false; // The MySensors gateway will tell the node if it should output in metric or not. + +#define CONVERSION_FACTOR (1.0/10.0) // used by forecast algorithm to convert from Pa to kPa, by dividing hPa by 10. + +#ifdef GENERATE_FORECAST // Below you will find a lot of variables used by the forecast algorithm. const char *weather[] = { "stable", "sunny", "cloudy", "unstable", "thunderstorm", "unknown" }; enum FORECAST { - STABLE = 0, // "Stable Weather Pattern" - SUNNY = 1, // "Slowly rising Good Weather", "Clear/Sunny " - CLOUDY = 2, // "Slowly falling L-Pressure ", "Cloudy/Rain " - UNSTABLE = 3, // "Quickly rising H-Press", "Not Stable" - THUNDERSTORM = 4, // "Quickly falling L-Press", "Thunderstorm" - UNKNOWN = 5 // "Unknown (More Time needed) + STABLE = 0, // "Stable Weather Pattern" + SUNNY = 1, // "Slowly rising Good Weather", "Clear/Sunny " + CLOUDY = 2, // "Slowly falling L-Pressure ", "Cloudy/Rain " + UNSTABLE = 3, // "Quickly rising H-Press", "Not Stable" + THUNDERSTORM = 4, // "Quickly falling L-Press", "Thunderstorm" + UNKNOWN = 5 // "Unknown (More Time needed) }; - -Adafruit_BMP085 bmp = Adafruit_BMP085(); // Digital Pressure Sensor - -float lastPressure = -1; -float lastTemp = -1; -int lastForecast = -1; - +int lastForecast = -1; // Stores the previous forecast, so it can be compared with a new forecast. const int LAST_SAMPLES_COUNT = 5; float lastPressureSamples[LAST_SAMPLES_COUNT]; +int minuteCount = 0; // Helps the forecast algorithm keep time. +bool firstRound = true; // Helps the forecast algorithm recognise if the sensor has just been powered up. +float pressureAvg; // Average value is used in forecast algorithm. +float pressureAvg2; // Average after 2 hours is used as reference value for the next iteration. +float dP_dt; // Pressure delta over time +#endif + +// MYSENSORS COMMUNICATION VARIABLES +MyMessage temperatureMsg(TEMP_CHILD_ID, V_TEMP); +MyMessage humidityMsg(HUM_CHILD_ID, V_HUM); +MyMessage pressureMsg(BARO_CHILD_ID, V_PRESSURE); +#ifdef GENERATE_FORECAST +MyMessage forecastMsg(BARO_CHILD_ID, V_FORECAST); +#endif + + +void setup() { + Wire.begin(); // Wire.begin(sda, scl) // starts the wire communication protocol, used to chat with the BME280 sensor. + Serial.begin(115200); // for serial debugging over USB. + Serial.println("Hello world, I am a sensor node."); + +#ifdef BATTERY_POWERED // If the node is battery powered, we'll let Sleep take over the scheduling. + BME280measurementSleepTime = BME280measurementInterval; + BME280measurementInterval = 0; // When the Arduino is asleep, millis doesn't increment anymore (time stops as it were). To fix this, we'll set the measurement interval time to 1, so that when the arduino wakes up it will immediately try to measure again. +#endif -// this CONVERSION_FACTOR is used to convert from Pa to kPa in forecast algorithm -// get kPa/h be dividing hPa by 10 -#define CONVERSION_FACTOR (1.0/10.0) - -int minuteCount = 0; -bool firstRound = true; -// average value is used in forecast algorithm. -float pressureAvg; -// average after 2 hours is used as reference value for the next iteration. -float pressureAvg2; - -float dP_dt; -bool metric; -MyMessage tempMsg(TEMP_CHILD, V_TEMP); -MyMessage pressureMsg(BARO_CHILD, V_PRESSURE); -MyMessage forecastMsg(BARO_CHILD, V_FORECAST); - - -void setup() -{ - if (!bmp.begin()) - { - Serial.println("Could not find a valid BMP085 sensor, check wiring!"); - while (1) {} - } - metric = getControllerConfig().isMetric; } + void presentation() { // Send the sketch version information to the gateway and Controller - sendSketchInfo("Pressure Sensor", "1.1"); + sendSketchInfo("BME280 Sensor", "1.1"); - // Register sensors to gw (they will be created as child devices) - present(BARO_CHILD, S_BARO); - present(TEMP_CHILD, S_TEMP); + // Tell the MySensors gateway what kind of sensors this node has, and what their ID's on the node are, as defined in the code above. + present(BARO_CHILD_ID, S_BARO); + present(TEMP_CHILD_ID, S_TEMP); + present(HUM_CHILD_ID, S_HUM); } -void loop() -{ - float pressure = bmp.readSealevelPressure(ALTITUDE) / 100.0; - float temperature = bmp.readTemperature(); - - if (!metric) - { - // Convert to fahrenheit - temperature = temperature * 9.0 / 5.0 + 32.0; - } - - int forecast = sample(pressure); - - Serial.print("Temperature = "); - Serial.print(temperature); - Serial.println(metric ? " *C" : " *F"); - Serial.print("Pressure = "); - Serial.print(pressure); - Serial.println(" hPa"); - Serial.print("Forecast = "); - Serial.println(weather[forecast]); - - - if (temperature != lastTemp) - { - send(tempMsg.set(temperature, 1)); - lastTemp = temperature; - } - - if (pressure != lastPressure) - { - send(pressureMsg.set(pressure, 0)); - lastPressure = pressure; - } - - if (forecast != lastForecast) - { - send(forecastMsg.set(weather[forecast])); - lastForecast = forecast; - } - - sleep(SLEEP_TIME); -} + +void loop() { + + // You should not change these variables: + static unsigned long previousBME280Millis = 0; // Used to remember the time that the BME280 sensor was asked for a measurement. + unsigned long currentMillis = millis(); // The time since the sensor started, counted in milliseconds. This script tries to avoid using the Sleep function, so that it could at the same time be a MySensors repeater. + static boolean BME280shouldAsk = true; // This is true when the time is right for a new measurement to be made. + static boolean BME280justAsked = false; // This indicates whether we have just asked the sensor module for a measurement, so the receiving part of the code (part 2) should be primed. This two-part construction helps to bridge the time where the BME280 module is busy, without blocking the entire node from doing anything else (like being a repeater, or working with other connected sensor modules). + + + // PART 1. If enough time has passed, a new measurement should be taken: + if (BME280shouldAsk == true && currentMillis - previousBME280Millis >= BME280measurementInterval) { + previousBME280Millis = currentMillis; // store the current time as the previous measurement start time. + BME280shouldAsk = false; + Serial.println(""); + Serial.println("BME280 - Requesting new data from sensor module."); + BME280.readCompensationParams(); // Need to read the NVM compensation parameters. + +#ifdef BATTERY_POWERED + // After taking the measurement the chip goes back to sleep. This code is only enabled if you enabled BATTERY POWERED at the top of this script. + // Oversampling settings (os1x, os2x, os4x, os8x or os16x). + BME280.writeFilterCoefficient(fc_16); // IIR Filter coefficient, higher numbers avoid sudden changes to be accounted for (such as slamming a door) + BME280.writeOversamplingPressure(os16x); // pressure x16 + BME280.writeOversamplingTemperature(os8x); // temperature x8 + BME280.writeOversamplingHumidity(os8x); // humidity x8 + BME280.writeMode(smForced); // Forced sample. After taking the measurement the chip goes back to sleep. +#else + // Normal mode for regular automatic samples + BME280.writeStandbyTime(tsb_0p5ms); // tsb = 0.5ms + BME280.writeFilterCoefficient(fc_16); // IIR Filter coefficient 16 + BME280.writeOversamplingPressure(os16x); // pressure x16 + BME280.writeOversamplingTemperature(os8x); // temperature x8 + BME280.writeOversamplingHumidity(os8x); // humidity x8 + BME280.writeMode(smNormal); +#endif + + // As we exit part 1, in theory BME280.isMeasuring() should now be true. + BME280justAsked = true; + } + + + // Part 2. This will trigger if the sensor has just been asked for a measurement, and is also just done figuring out those measurements. + if(BME280justAsked == true && BME280.isMeasuring() == false) { // + BME280justAsked = false; // makes sure we don't do this part again in the next pass through the main loop. + Serial.println("BME280 - Sensor module has some new values ready:"); + + // Read out the data - must do this before calling the getxxxxx routines + BME280.readMeasurements(); + + float temperature = BME280.getTemperatureMostAccurate(); // Must get the temperature first. + float humidity = BME280.getHumidityMostAccurate(); // Get the humidity. + float pressure_local = BME280.getPressureMostAccurate(); // Get pressure at current location + float pressure = pressure_local/pow((1.0 - ( ALTITUDE / 44330.0 )), 5.255); // Adjust to sea level pressure using user altitude +#ifdef GENERATE_FORECAST + int forecast = sample(pressure); // Run the forecast function with a new pressure update. +#endif + + if (!metric) { + // Convert temperature to fahrenheit + temperature = temperature * 9.0 / 5.0 + 32.0; + } + + // Useful for debugging + Serial.print("BME280 - Temperature = "); + Serial.print(temperature); + Serial.println(metric ? " °C" : " °F"); + Serial.print("BME280 - Humidity = "); + Serial.print(humidity); + Serial.println(" %"); + Serial.print("BME280 - Pressure = "); + Serial.print(pressure); + Serial.println(" hPa"); +#ifdef GENERATE_FORECAST + Serial.print("BME280 - Forecast = "); + Serial.println(weather[forecast]); +#endif + + // Now, let's send the measurements to the gateway. + + // Send temperature + if (COMPARE_TEMP == 1 && abs(temperature - lastTemperature) < tempThreshold) { // is the temperature difference bigger than the threshold? + Serial.print(temperature - lastTemperature); + Serial.print("- BME280 - Temperature difference too small, so not sending the new measurement to the gateway.\n"); + } else { + Serial.print("BME280 - Sending the new temperature to the gateway.\n"); + send(temperatureMsg.set(temperature, 1)); + lastTemperature = temperature; // Save new temperatures to be able to compare in the next round. + } + + // Send humidity + if (COMPARE_HUM == 1 && abs(humidity - lastHumidity) < humThreshold) { // is the humidity difference bigger than the threshold? + Serial.print(humidity - lastHumidity); + Serial.println("- BME280 - Humidity difference too small, so not sending the new measurement to the gateway."); + } else { + Serial.println("BME280 - Sending the new humidity to the gateway."); + send(humidityMsg.set(humidity, 1)); + lastHumidity = humidity; // Save new humidity to be able to compare in the next round. + } + + // Send pressure + if (COMPARE_BARO == 1 && abs(pressure - lastPressure) < presThreshold) { // is the pressure difference bigger than the threshold? + Serial.print(pressure - lastPressure); + Serial.println("- BME280 - Pressure difference too small, so not sending the new measurement to the gateway."); + } else { + Serial.println("BME280 - Sending the new pressure to the gateway."); + send(pressureMsg.set(pressure, 1)); + lastPressure = pressure; // Save new pressure to be able to compare in the next round. + } + +#ifdef GENERATE_FORECAST + // Send forecast + if (forecast != lastForecast) { + Serial.println("BME280 - Sending the latest forecast to the gateway."); + send(forecastMsg.set(weather[forecast])); + lastForecast = forecast; + } +#endif + + Serial.println("BME280 - Measurement complete. Going to wait until next measurement."); + BME280shouldAsk = true; // Ready for the new round. + } + +#ifdef BATTERY_POWERED + // This code will only be included in the sketch if the BATTERY POWERED feature is enabled. + if(BME280shouldAsk == true && BME280justAsked == false) { // Both parts are done, so we can let the sensor sleep again. + unsigned long quicktimecheck = millis(); // To check how much time has passed since the beginning of being awake, and then calculate from that how long to sleep until the next intended measuring time, we need to know how many milliseconds have passed. + unsigned long sleeptime = BME280measurementSleepTime - (quicktimecheck - previousBME280Millis); // How much time has passed already during the calculating? Subtract that from the intended interval time. + Serial.println("BME280 - zzzzZZZZzzzzZZZZzzzz"); + sleep(sleeptime); + Serial.println("BME280 - Waking up."); + } +#endif + +} // end of main loop. + + + +#ifdef GENERATE_FORECAST +// These functions are only included if the forecast function is enables. The are used to generate a weater prediction by checking if the barometric pressure is rising or falling over time. float getLastPressureSamplesAverage() { - float lastPressureSamplesAverage = 0; - for (int i = 0; i < LAST_SAMPLES_COUNT; i++) - { - lastPressureSamplesAverage += lastPressureSamples[i]; - } - lastPressureSamplesAverage /= LAST_SAMPLES_COUNT; - - return lastPressureSamplesAverage; -} + float lastPressureSamplesAverage = 0; + for (int i = 0; i < LAST_SAMPLES_COUNT; i++) { + lastPressureSamplesAverage += lastPressureSamples[i]; + } + lastPressureSamplesAverage /= LAST_SAMPLES_COUNT; + return lastPressureSamplesAverage; +} -// Algorithm found here +// Forecast algorithm found here // http://www.freescale.com/files/sensors/doc/app_note/AN3914.pdf // Pressure in hPa --> forecast done by calculating kPa/h -int sample(float pressure) -{ - // Calculate the average of the last n minutes. - int index = minuteCount % LAST_SAMPLES_COUNT; - lastPressureSamples[index] = pressure; - - minuteCount++; - if (minuteCount > 185) - { - minuteCount = 6; - } - - if (minuteCount == 5) - { - pressureAvg = getLastPressureSamplesAverage(); - } - else if (minuteCount == 35) - { - float lastPressureAvg = getLastPressureSamplesAverage(); - float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; - if (firstRound) // first time initial 3 hour - { - dP_dt = change * 2; // note this is for t = 0.5hour - } - else - { - dP_dt = change / 1.5; // divide by 1.5 as this is the difference in time from 0 value. - } - } - else if (minuteCount == 65) - { - float lastPressureAvg = getLastPressureSamplesAverage(); - float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; - if (firstRound) //first time initial 3 hour - { - dP_dt = change; //note this is for t = 1 hour - } - else - { - dP_dt = change / 2; //divide by 2 as this is the difference in time from 0 value - } - } - else if (minuteCount == 95) - { - float lastPressureAvg = getLastPressureSamplesAverage(); - float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; - if (firstRound) // first time initial 3 hour - { - dP_dt = change / 1.5; // note this is for t = 1.5 hour - } - else - { - dP_dt = change / 2.5; // divide by 2.5 as this is the difference in time from 0 value - } - } - else if (minuteCount == 125) - { - float lastPressureAvg = getLastPressureSamplesAverage(); - pressureAvg2 = lastPressureAvg; // store for later use. - float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; - if (firstRound) // first time initial 3 hour - { - dP_dt = change / 2; // note this is for t = 2 hour - } - else - { - dP_dt = change / 3; // divide by 3 as this is the difference in time from 0 value - } - } - else if (minuteCount == 155) - { - float lastPressureAvg = getLastPressureSamplesAverage(); - float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; - if (firstRound) // first time initial 3 hour - { - dP_dt = change / 2.5; // note this is for t = 2.5 hour - } - else - { - dP_dt = change / 3.5; // divide by 3.5 as this is the difference in time from 0 value - } - } - else if (minuteCount == 185) - { - float lastPressureAvg = getLastPressureSamplesAverage(); - float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; - if (firstRound) // first time initial 3 hour - { - dP_dt = change / 3; // note this is for t = 3 hour - } - else - { - dP_dt = change / 4; // divide by 4 as this is the difference in time from 0 value - } - pressureAvg = pressureAvg2; // Equating the pressure at 0 to the pressure at 2 hour after 3 hours have past. - firstRound = false; // flag to let you know that this is on the past 3 hour mark. Initialized to 0 outside main loop. - } - - int forecast = UNKNOWN; - if (minuteCount < 35 && firstRound) //if time is less than 35 min on the first 3 hour interval. - { - forecast = UNKNOWN; - } - else if (dP_dt < (-0.25)) - { - forecast = THUNDERSTORM; - } - else if (dP_dt > 0.25) - { - forecast = UNSTABLE; - } - else if ((dP_dt > (-0.25)) && (dP_dt < (-0.05))) - { - forecast = CLOUDY; - } - else if ((dP_dt > 0.05) && (dP_dt < 0.25)) - { - forecast = SUNNY; - } - else if ((dP_dt >(-0.05)) && (dP_dt < 0.05)) - { - forecast = STABLE; - } - else - { - forecast = UNKNOWN; - } - - // uncomment when debugging - //Serial.print(F("Forecast at minute ")); - //Serial.print(minuteCount); - //Serial.print(F(" dP/dt = ")); - //Serial.print(dP_dt); - //Serial.print(F("kPa/h --> ")); - //Serial.println(weather[forecast]); - - return forecast; +int sample(float pressure) { + // Calculate the average of the last n minutes. + int index = minuteCount % LAST_SAMPLES_COUNT; + lastPressureSamples[index] = pressure; + + minuteCount++; + if (minuteCount > 185) { + minuteCount = 6; + } + + if (minuteCount == 5) { + pressureAvg = getLastPressureSamplesAverage(); + } + else if (minuteCount == 35) { + float lastPressureAvg = getLastPressureSamplesAverage(); + float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; + if (firstRound) { // first time initial 3 hour + dP_dt = change * 2; // note this is for t = 0.5hour + } + else { + dP_dt = change / 1.5; // divide by 1.5 as this is the difference in time from 0 value. + } + } + else if (minuteCount == 65) { + float lastPressureAvg = getLastPressureSamplesAverage(); + float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; + if (firstRound) { //first time initial 3 hour + dP_dt = change; //note this is for t = 1 hour + } + else { + dP_dt = change / 2; //divide by 2 as this is the difference in time from 0 value + } + } + else if (minuteCount == 95) { + float lastPressureAvg = getLastPressureSamplesAverage(); + float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; + if (firstRound) { // first time initial 3 hour + dP_dt = change / 1.5; // note this is for t = 1.5 hour + } + else { + dP_dt = change / 2.5; // divide by 2.5 as this is the difference in time from 0 value + } + } + else if (minuteCount == 125) { + float lastPressureAvg = getLastPressureSamplesAverage(); + pressureAvg2 = lastPressureAvg; // store for later use. + float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; + if (firstRound) { // first time initial 3 hour + dP_dt = change / 2; // note this is for t = 2 hour + } + else { + dP_dt = change / 3; // divide by 3 as this is the difference in time from 0 value + } + } + else if (minuteCount == 155) { + float lastPressureAvg = getLastPressureSamplesAverage(); + float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; + if (firstRound) { // first time initial 3 hour + dP_dt = change / 2.5; // note this is for t = 2.5 hour + } + else { + dP_dt = change / 3.5; // divide by 3.5 as this is the difference in time from 0 value + } + } + else if (minuteCount == 185) { + float lastPressureAvg = getLastPressureSamplesAverage(); + float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; + if (firstRound) { // first time initial 3 hour + dP_dt = change / 3; // note this is for t = 3 hour + } + else { + dP_dt = change / 4; // divide by 4 as this is the difference in time from 0 value + } + pressureAvg = pressureAvg2; // Equating the pressure at 0 to the pressure at 2 hour after 3 hours have past. + firstRound = false; // flag to let you know that this is on the past 3 hour mark. Initialized to 0 outside main loop. + } + + int forecast = UNKNOWN; + if (minuteCount < 35 && firstRound) { //if time is less than 35 min on the first 3 hour interval. + forecast = UNKNOWN; + } + else if (dP_dt < (-0.25)) { + forecast = THUNDERSTORM; + } + else if (dP_dt > 0.25) { + forecast = UNSTABLE; + } + else if ((dP_dt > (-0.25)) && (dP_dt < (-0.05))) { + forecast = CLOUDY; + } + else if ((dP_dt > 0.05) && (dP_dt < 0.25)) { + forecast = SUNNY; + } + else if ((dP_dt >(-0.05)) && (dP_dt < 0.05)) { + forecast = STABLE; + } + else { + forecast = UNKNOWN; + } + + // uncomment when debugging + //Serial.print(F("BME280 - Forecast at minute ")); + //Serial.print(minuteCount); + //Serial.print(F(" dP/dt = ")); + //Serial.print(dP_dt); + //Serial.print(F("kPa/h --> ")); + //Serial.println(weather[forecast]); + + return forecast; } +#endif diff --git a/examples/RFIDLockSensor/RFIDLockSensor.ino b/examples/RFIDLockSensor/RFIDLockSensor.ino old mode 100644 new mode 100755 index 1344c80..1477bcd --- a/examples/RFIDLockSensor/RFIDLockSensor.ino +++ b/examples/RFIDLockSensor/RFIDLockSensor.ino @@ -1,184 +1,181 @@ -/** - * 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 - * 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. - * - ******************************* - * - * REVISION HISTORY - * Version 1.0 - Henrik Ekblad - * - * DESCRIPTION - * RFID Lock sensor/actuator - * - * Use RFID tag to lock/unlock a door or trigger a scene on your controller. - * This example sketch allows you to add an optional relay or solenoid - * which can be activated/opened by RFID or controller. - * - * Use the I2C wiring option for your RFID module and connect to the following Arduino pins. - * - * RFID Arduino - * ----- ------- - * GND -> GND - * VCC -> +5V - * SCL -> A5 - * SDA -> A4 - * - * Use normal wiring for NRF24L01 radio - * - * Attach a optional relay or solonoid lock to pin 4 - * http://www.mysensors.org/build/rfid - */ - -// Enable debug prints to serial monitor -#define MY_DEBUG - -// Enable and select radio type attached -#define MY_RADIO_NRF24 -//#define MY_RADIO_RFM69 - -#include -#include -#include -#include -#include - - -// Add your valid rfid keys here. To find you your key just run sketch; hold your new RFID tag in fron ot the reader; -// and copy the key from serial output of this sketch. -const uint8_t maxKeyLength = 7; -uint8_t validKeys[][maxKeyLength] = { - { 0xB3, 0xC6, 0xD9, 0x80, 0x00, 0x00, 0x00 }, - { 0, 0, 0, 0, 0, 0, 0 }, // ADD YOUR KEYS HERE! - { 0, 0, 0, 0, 0, 0, 0 }}; -int keyCount = sizeof validKeys / maxKeyLength; - - -#define CHILD_ID 99 // Id of the sensor child - -// Pin definition -const int lockPin = 4; // (Digital 4) The pin that activates the relay/solenoid lock. -bool lockStatus; -MyMessage lockMsg(CHILD_ID, V_LOCK_STATUS); -PN532_I2C pn532i2c(Wire); -PN532 nfc(pn532i2c); - -void setup() { - - pinMode(lockPin, OUTPUT); - - nfc.begin(); - uint32_t versiondata = nfc.getFirmwareVersion(); - if (! versiondata) { - Serial.print("Couldn't find PN53x board"); - while (1); // halt - } - Serial.print("Found NFC chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); - Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); - Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC); - // Set the max number of retry attempts to read from a card - // This prevents us from waiting forever for a card, which is - // the default behaviour of the PN532. - nfc.setPassiveActivationRetries(0x3); - - // configure board to read RFID tags - nfc.SAMConfig(); - - lockStatus = loadState(0); // Read last lock status from eeprom - setLockState(lockStatus, true); // Now set the last known state and send it to controller -} - -void presentation() { - sendSketchInfo("RFID Lock", "1.0"); - present(CHILD_ID, S_LOCK); -} - -void loop() { - bool success; - uint8_t key[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID - uint8_t currentKeyLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) - - - // Wait for an ISO14443A type cards (Mifare, etc.). When one is found - // 'uid' will be populated with the UID, and uidLength will indicate - // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) - success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], ¤tKeyLength); - - if (success) { - Serial.print("Found tag id: "); - for (uint8_t i=0; i < currentKeyLength; i++) - { - if (i>0) Serial.print(","); - Serial.print("0x");Serial.print(key[i], HEX); - } - for (uint8_t i=currentKeyLength; i < maxKeyLength; i++) - { - Serial.print(",0x00"); - } - - - Serial.println(""); - - bool valid = false; - // Compare this key to the valid once registered here in sketch - for (int i=0;i + * 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. + * + ******************************* + * + * REVISION HISTORY + * Version 1.0 - Henrik Ekblad + * + * DESCRIPTION + * RFID Lock sensor/actuator + * + * Use RFID tag to lock/unlock a door or trigger a scene on your controller. + * This example sketch allows you to add an optional relay or solenoid + * which can be activated/opened by RFID or controller. + * + * Use the I2C wiring option for your RFID module and connect to the following Arduino pins. + * + * RFID Arduino + * ----- ------- + * GND -> GND + * VCC -> +5V + * SCL -> A5 + * SDA -> A4 + * + * Use normal wiring for NRF24L01 radio + * + * Attach a optional relay or solonoid lock to pin 4 + * http://www.mysensors.org/build/rfid + */ + +// Enable debug prints to serial monitor +#define MY_DEBUG + +// Enable and select radio type attached +#define MY_RADIO_RF24 +//#define MY_RADIO_RFM69 + +#include +#include +#include +#include + + +// Add your valid rfid keys here. To find you your key just run sketch; hold your new RFID tag in fron ot the reader; +// and copy the key from serial output of this sketch. +const uint8_t maxKeyLength = 7; +uint8_t validKeys[][maxKeyLength] = { + { 0xB3, 0xC6, 0xD9, 0x80, 0x00, 0x00, 0x00 }, + { 0, 0, 0, 0, 0, 0, 0 }, // ADD YOUR KEYS HERE! + { 0, 0, 0, 0, 0, 0, 0 }}; +int keyCount = sizeof validKeys / maxKeyLength; + + +#define CHILD_ID 99 // Id of the sensor child + +// Pin definition +const int lockPin = 4; // (Digital 4) The pin that activates the relay/solenoid lock. +bool lockStatus; +MyMessage lockMsg(CHILD_ID, V_LOCK_STATUS); +PN532_I2C pn532i2c(Wire); +PN532 nfc(pn532i2c); + +void setup() { + + pinMode(lockPin, OUTPUT); + + nfc.begin(); + uint32_t versiondata = nfc.getFirmwareVersion(); + if (! versiondata) { + Serial.print("Couldn't find PN53x board"); + while (1); // halt + } + Serial.print("Found NFC chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); + Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); + Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC); + // Set the max number of retry attempts to read from a card + // This prevents us from waiting forever for a card, which is + // the default behaviour of the PN532. + nfc.setPassiveActivationRetries(0x3); + + // configure board to read RFID tags + nfc.SAMConfig(); + + lockStatus = loadState(0); // Read last lock status from eeprom + setLockState(lockStatus, true); // Now set the last known state and send it to controller +} + +void presentation() { + sendSketchInfo("RFID Lock", "1.0"); + present(CHILD_ID, S_LOCK); +} + +void loop() { + bool success; + uint8_t key[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID + uint8_t currentKeyLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) + + + // Wait for an ISO14443A type cards (Mifare, etc.). When one is found + // 'uid' will be populated with the UID, and uidLength will indicate + // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) + success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], ¤tKeyLength); + + if (success) { + Serial.print("Found tag id: "); + for (uint8_t i=0; i < currentKeyLength; i++) + { + if (i>0) Serial.print(","); + Serial.print("0x");Serial.print(key[i], HEX); + } + for (uint8_t i=currentKeyLength; i < maxKeyLength; i++) + { + Serial.print(",0x00"); + } + + + Serial.println(""); + + bool valid = false; + // Compare this key to the valid once registered here in sketch + for (int i=0;i - * 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. - * - ******************************* - * - * REVISION HISTORY - * Version 1.0 - Created by vil1driver - * - * DESCRIPTION - * RGB led strip controled with three dimmers + one On/Off for run/stop rgb color cycle :p - * - */ - -#define SN "RGB Led strip 3D" -#define SV "v1" - -// Enable debug prints to serial monitor -#define MY_DEBUG - -// Enable and select radio type attached -#define MY_RADIO_NRF24 -//#define MY_RADIO_RFM69 - -#include -#include - -// Arduino pin attached to MOSFET Gate pin -#define RED_PIN 3 -#define GREEN_PIN 5 -#define BLUE_PIN 6 - -// Define message name and type to send sensor info -MyMessage RedStatus(RED_PIN, V_DIMMER); -MyMessage GreenStatus(GREEN_PIN, V_DIMMER); -MyMessage BlueStatus(BLUE_PIN, V_DIMMER); -MyMessage Status(1, V_DIMMER); -MyMessage rgbShowState(0, V_LIGHT); - -// Serial.print translate sensor id to sensor name -char color[][6] = {"","","","RED","","GREEN","BLUE"}; - -// Vars for rgbShow function -int redval = 0; -int greenval = 0; -int blueval = 0; -long time=0; -int isShow; - -void setup() -{ - // Define pin mode (pin number, type) - pinMode(RED_PIN, OUTPUT); - pinMode(GREEN_PIN, OUTPUT); - pinMode(BLUE_PIN, OUTPUT); - - // Correct saved RGB value for first start - saveState(RED_PIN, constrain((int8_t)loadState(RED_PIN), 0, 100)); - saveState(GREEN_PIN, constrain((int8_t)loadState(GREEN_PIN), 0, 100)); - saveState(BLUE_PIN, constrain((int8_t)loadState(BLUE_PIN), 0, 100)); - - // Get value from eeprom and write to output - analogWrite(RED_PIN, 255 * loadState(RED_PIN) / 100); - analogWrite(GREEN_PIN, 255 * loadState(GREEN_PIN) / 100); - analogWrite(BLUE_PIN, 255 * loadState(BLUE_PIN) / 100); - - // Write some debug info - Serial.print("Load from eeprom RED: "); - Serial.print(loadState(RED_PIN)); - Serial.println("%"); - Serial.print("Load from eeprom GREEN: "); - Serial.print(loadState(GREEN_PIN)); - Serial.println("%"); - Serial.print("Load from eeprom BLUE: "); - Serial.print(loadState(BLUE_PIN)); - Serial.println("%"); - - // Send RGB value to controler (request ack back: true/false) - Serial.println("Send eeprom value to controler"); - send( RedStatus.set(loadState(RED_PIN)), false ); - send( GreenStatus.set(loadState(GREEN_PIN)), false ); - send( BlueStatus.set(loadState(BLUE_PIN)), false ); - - // Correct RGB show state for first start and load it (set to 'On' at first start) - saveState(0, constrain((int8_t)loadState(0), 0, 1)); - isShow=loadState(0); - - // Send RGB show state to controler (request ack back: true/false) - send( rgbShowState.set(isShow), false); - - if (isShow==1){Serial.println("RGB show running..."); } - Serial.println("Ready to receive messages..."); -} - -void presentation() { - // Present sketch (name, version) - sendSketchInfo(SN, SV); - - // Register sensors (id, type, description, ack back) - present(RED_PIN, S_DIMMER, "present RED light", false); - present(GREEN_PIN, S_DIMMER, "present GREEN light", false); - present(BLUE_PIN, S_DIMMER, "present BLUE light", false); - present(0, S_LIGHT, "present Show button", false); -} - -void loop() -{ - // Run RGB show if is set - if (isShow==1) - { - rgbShow(); - analogWrite(RED_PIN, redval); - analogWrite(GREEN_PIN, greenval); - analogWrite(BLUE_PIN, blueval); - } -} - - -void receive(const MyMessage &message) -{ - if (message.isAck()) - { - Serial.println("Got ack from gateway"); - } - if (message.type == V_LIGHT) - { - // Incoming on/off command sent from controller ("1" or "0") - int lightState = message.getString()[0] == '1'; - - // if receive RGB Show On commands, start the show - if (message.sensor==0 && lightState==1){ rgbShowOn(); } - // if receive RGB Show Off commands, stop the show - else if (message.sensor==0 && lightState==0){ rgbShowOff(); } - - // if receive RGB switch On command - else if (lightState==1) - { - // Write some debug info - Serial.print("Incoming change for "); - Serial.print(color[message.sensor]); - Serial.println(": On"); - Serial.print("Load from eeprom: "); - - if ( loadState(message.sensor) == 0) - { - // Pick up last saved dimmer level from the eeprom - analogWrite(message.sensor, 255 * loadState(10*message.sensor) / 100); - // Save loaded value to current - saveState(message.sensor, loadState(10*message.sensor)); - Serial.print(loadState(10*message.sensor)); - Serial.println("%"); - // Send value to controler - Serial.println("Send value to controler"); - send(Status.setSensor(message.sensor).set(loadState(10*message.sensor)),false); - } - else - { - // Pick up last saved dimmer level from the eeprom - analogWrite(message.sensor, 255 * loadState(message.sensor) / 100); - Serial.print(loadState(message.sensor)); - Serial.println("%"); - // Send value to controler - Serial.println("Send value to controler"); - send(Status.setSensor(message.sensor).set(loadState(message.sensor)),false); - } - // Stop the show if it's running - if (isShow==1){ rgbShowStop(message.sensor); } - } - // if recieve switch Off command - else if (lightState==0) - { - // Write output to 0 (Off) - analogWrite(message.sensor, 0); - // Save old value to eeprom if it'was not zero - if ( loadState(message.sensor) != 0 ) - { - saveState(10*message.sensor, constrain((int8_t)loadState(message.sensor), 0, 100)); - } - // Save new value to eeprom - saveState(message.sensor, 0); - // Write some debug info - Serial.print("Incoming change for "); - Serial.print(color[message.sensor]); - Serial.print(": "); - Serial.println("Off"); - Serial.print("Store old value: "); - Serial.print(loadState(10*message.sensor)); - Serial.println("%"); - // Send value to controler - Serial.println("Send value to controler"); - send(Status.setSensor(message.sensor).set(loadState(message.sensor)),false); - // Stop the show if it's running - if (isShow==1){ rgbShowStop(message.sensor); } - } - } - else if (message.type == V_DIMMER) - { - uint8_t incomingDimmerStatus = message.getByte(); - // limits range of sensor values to between 0 and 100 - incomingDimmerStatus = constrain((int8_t)incomingDimmerStatus, 0, 100); - // Change Dimmer level - analogWrite(message.sensor, 255 * incomingDimmerStatus / 100); - //Save value to eeprom - saveState(message.sensor, incomingDimmerStatus); - // Write some debug info - Serial.print("Incoming change for "); - Serial.print(color[message.sensor]); - Serial.print(": "); - Serial.print(incomingDimmerStatus); - Serial.println("%"); - // Send value to controler - Serial.println("Send value to controler"); - send(Status.setSensor(message.sensor).set(loadState(message.sensor)),false); - // Stop the show if it's running - if (isShow==1){ rgbShowStop(message.sensor); } - } -} - -void rgbShow() -{ - time = millis(); - redval = 128+250*cos(2*PI/300000*time); - greenval = 128+250*cos(2*PI/300000*time-222); - blueval = 128+250*cos(2*PI/300000*time-111); - // limits range of sensor values to between 0 and 255 - redval = constrain(redval, 0, 255); - greenval = constrain(greenval, 0, 255); - blueval = constrain(blueval, 0, 255); -} - -void rgbShowOn() -{ - // define show On - isShow=1; - // Save state - saveState(0, 1); - // Write some debug info - Serial.println("Show must go on"); -} - -void rgbShowOff() -{ - // define show Off - isShow=0; - // Save state - saveState(0, 0); - // Save RGB value to eeprom - saveState(RED_PIN, 100 * redval / 255); - saveState(GREEN_PIN, 100 * greenval / 255); - saveState(BLUE_PIN, 100 * blueval / 255); - // Write some debug info - Serial.println("Stop the show"); - // Send actual RGB value and state to controler and request ack back (true/false) - Serial.println("Send eeprom value to controler"); - send( RedStatus.set(loadState(RED_PIN)), false ); - send( GreenStatus.set(loadState(GREEN_PIN)), false ); - send( BlueStatus.set(loadState(BLUE_PIN)), false ); - send( rgbShowState.set(0), false); -} - -void rgbShowStop(int sensor) -{ - // define show Off - isShow=0; - // Save state - saveState(0, 0); - // Write some debug info - Serial.println("Stop the show"); - // Send actual RGB value and state to controler and request ack back (true/false) - Serial.println("Send eeprom value to controler"); - if (sensor != RED_PIN) - { - saveState(RED_PIN, 100 * redval / 255); - send( RedStatus.set(loadState(RED_PIN)), false ); - } - if (sensor != GREEN_PIN) - { - saveState(GREEN_PIN, 100 * greenval / 255); - send( GreenStatus.set(loadState(GREEN_PIN)), false ); - } - if (sensor != BLUE_PIN) - { - saveState(BLUE_PIN, 100 * blueval / 255); - send( BlueStatus.set(loadState(BLUE_PIN)), false ); - } - send( rgbShowState.set(0), false); -} - - - +/** + * 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 + * 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. + * + ******************************* + * + * REVISION HISTORY + * Version 1.0 - Created by vil1driver + * + * DESCRIPTION + * RGB led strip controled with three dimmers + one On/Off for run/stop rgb color cycle :p + * + */ + +#define SN "RGB Led strip 3D" +#define SV "v1" + +// Enable debug prints to serial monitor +#define MY_DEBUG + +// Enable and select radio type attached +#define MY_RADIO_RF24 +//#define MY_RADIO_RFM69 + +#include + +// Arduino pin attached to MOSFET Gate pin +#define RED_PIN 3 +#define GREEN_PIN 5 +#define BLUE_PIN 6 + +// Define message name and type to send sensor info +MyMessage RedStatus(RED_PIN, V_DIMMER); +MyMessage GreenStatus(GREEN_PIN, V_DIMMER); +MyMessage BlueStatus(BLUE_PIN, V_DIMMER); +MyMessage Status(1, V_DIMMER); +MyMessage rgbShowState(0, V_LIGHT); + +// Serial.print translate sensor id to sensor name +char color[][6] = {"","","","RED","","GREEN","BLUE"}; + +// Vars for rgbShow function +int redval = 0; +int greenval = 0; +int blueval = 0; +long time=0; +int isShow; + +void setup() +{ + // Define pin mode (pin number, type) + pinMode(RED_PIN, OUTPUT); + pinMode(GREEN_PIN, OUTPUT); + pinMode(BLUE_PIN, OUTPUT); + + // Correct saved RGB value for first start + saveState(RED_PIN, constrain((int8_t)loadState(RED_PIN), 0, 100)); + saveState(GREEN_PIN, constrain((int8_t)loadState(GREEN_PIN), 0, 100)); + saveState(BLUE_PIN, constrain((int8_t)loadState(BLUE_PIN), 0, 100)); + + // Get value from eeprom and write to output + analogWrite(RED_PIN, 255 * loadState(RED_PIN) / 100); + analogWrite(GREEN_PIN, 255 * loadState(GREEN_PIN) / 100); + analogWrite(BLUE_PIN, 255 * loadState(BLUE_PIN) / 100); + + // Write some debug info + Serial.print("Load from eeprom RED: "); + Serial.print(loadState(RED_PIN)); + Serial.println("%"); + Serial.print("Load from eeprom GREEN: "); + Serial.print(loadState(GREEN_PIN)); + Serial.println("%"); + Serial.print("Load from eeprom BLUE: "); + Serial.print(loadState(BLUE_PIN)); + Serial.println("%"); + + // Send RGB value to controler (request ack back: true/false) + Serial.println("Send eeprom value to controler"); + send( RedStatus.set(loadState(RED_PIN)), false ); + send( GreenStatus.set(loadState(GREEN_PIN)), false ); + send( BlueStatus.set(loadState(BLUE_PIN)), false ); + + // Correct RGB show state for first start and load it (set to 'On' at first start) + saveState(0, constrain((int8_t)loadState(0), 0, 1)); + isShow=loadState(0); + + // Send RGB show state to controler (request ack back: true/false) + send( rgbShowState.set(isShow), false); + + if (isShow==1){Serial.println("RGB show running..."); } + Serial.println("Ready to receive messages..."); +} + +void presentation() { + // Present sketch (name, version) + sendSketchInfo(SN, SV); + + // Register sensors (id, type, description, ack back) + present(RED_PIN, S_DIMMER, "present RED light", false); + present(GREEN_PIN, S_DIMMER, "present GREEN light", false); + present(BLUE_PIN, S_DIMMER, "present BLUE light", false); + present(0, S_LIGHT, "present Show button", false); +} + +void loop() +{ + // Run RGB show if is set + if (isShow==1) + { + rgbShow(); + analogWrite(RED_PIN, redval); + analogWrite(GREEN_PIN, greenval); + analogWrite(BLUE_PIN, blueval); + } +} + + +void receive(const MyMessage &message) +{ + if (message.isAck()) + { + Serial.println("Got ack from gateway"); + } + if (message.type == V_LIGHT) + { + // Incoming on/off command sent from controller ("1" or "0") + int lightState = message.getString()[0] == '1'; + + // if receive RGB Show On commands, start the show + if (message.sensor==0 && lightState==1){ rgbShowOn(); } + // if receive RGB Show Off commands, stop the show + else if (message.sensor==0 && lightState==0){ rgbShowOff(); } + + // if receive RGB switch On command + else if (lightState==1) + { + // Write some debug info + Serial.print("Incoming change for "); + Serial.print(color[message.sensor]); + Serial.println(": On"); + Serial.print("Load from eeprom: "); + + if ( loadState(message.sensor) == 0) + { + // Pick up last saved dimmer level from the eeprom + analogWrite(message.sensor, 255 * loadState(10*message.sensor) / 100); + // Save loaded value to current + saveState(message.sensor, loadState(10*message.sensor)); + Serial.print(loadState(10*message.sensor)); + Serial.println("%"); + // Send value to controler + Serial.println("Send value to controler"); + send(Status.setSensor(message.sensor).set(loadState(10*message.sensor)),false); + } + else + { + // Pick up last saved dimmer level from the eeprom + analogWrite(message.sensor, 255 * loadState(message.sensor) / 100); + Serial.print(loadState(message.sensor)); + Serial.println("%"); + // Send value to controler + Serial.println("Send value to controler"); + send(Status.setSensor(message.sensor).set(loadState(message.sensor)),false); + } + // Stop the show if it's running + if (isShow==1){ rgbShowStop(message.sensor); } + } + // if recieve switch Off command + else if (lightState==0) + { + // Write output to 0 (Off) + analogWrite(message.sensor, 0); + // Save old value to eeprom if it'was not zero + if ( loadState(message.sensor) != 0 ) + { + saveState(10*message.sensor, constrain((int8_t)loadState(message.sensor), 0, 100)); + } + // Save new value to eeprom + saveState(message.sensor, 0); + // Write some debug info + Serial.print("Incoming change for "); + Serial.print(color[message.sensor]); + Serial.print(": "); + Serial.println("Off"); + Serial.print("Store old value: "); + Serial.print(loadState(10*message.sensor)); + Serial.println("%"); + // Send value to controler + Serial.println("Send value to controler"); + send(Status.setSensor(message.sensor).set(loadState(message.sensor)),false); + // Stop the show if it's running + if (isShow==1){ rgbShowStop(message.sensor); } + } + } + else if (message.type == V_DIMMER) + { + uint8_t incomingDimmerStatus = message.getByte(); + // limits range of sensor values to between 0 and 100 + incomingDimmerStatus = constrain((int8_t)incomingDimmerStatus, 0, 100); + // Change Dimmer level + analogWrite(message.sensor, 255 * incomingDimmerStatus / 100); + //Save value to eeprom + saveState(message.sensor, incomingDimmerStatus); + // Write some debug info + Serial.print("Incoming change for "); + Serial.print(color[message.sensor]); + Serial.print(": "); + Serial.print(incomingDimmerStatus); + Serial.println("%"); + // Send value to controler + Serial.println("Send value to controler"); + send(Status.setSensor(message.sensor).set(loadState(message.sensor)),false); + // Stop the show if it's running + if (isShow==1){ rgbShowStop(message.sensor); } + } +} + +void rgbShow() +{ + time = millis(); + redval = 128+250*cos(2*PI/300000*time); + greenval = 128+250*cos(2*PI/300000*time-222); + blueval = 128+250*cos(2*PI/300000*time-111); + // limits range of sensor values to between 0 and 255 + redval = constrain(redval, 0, 255); + greenval = constrain(greenval, 0, 255); + blueval = constrain(blueval, 0, 255); +} + +void rgbShowOn() +{ + // define show On + isShow=1; + // Save state + saveState(0, 1); + // Write some debug info + Serial.println("Show must go on"); +} + +void rgbShowOff() +{ + // define show Off + isShow=0; + // Save state + saveState(0, 0); + // Save RGB value to eeprom + saveState(RED_PIN, 100 * redval / 255); + saveState(GREEN_PIN, 100 * greenval / 255); + saveState(BLUE_PIN, 100 * blueval / 255); + // Write some debug info + Serial.println("Stop the show"); + // Send actual RGB value and state to controler and request ack back (true/false) + Serial.println("Send eeprom value to controler"); + send( RedStatus.set(loadState(RED_PIN)), false ); + send( GreenStatus.set(loadState(GREEN_PIN)), false ); + send( BlueStatus.set(loadState(BLUE_PIN)), false ); + send( rgbShowState.set(0), false); +} + +void rgbShowStop(int sensor) +{ + // define show Off + isShow=0; + // Save state + saveState(0, 0); + // Write some debug info + Serial.println("Stop the show"); + // Send actual RGB value and state to controler and request ack back (true/false) + Serial.println("Send eeprom value to controler"); + if (sensor != RED_PIN) + { + saveState(RED_PIN, 100 * redval / 255); + send( RedStatus.set(loadState(RED_PIN)), false ); + } + if (sensor != GREEN_PIN) + { + saveState(GREEN_PIN, 100 * greenval / 255); + send( GreenStatus.set(loadState(GREEN_PIN)), false ); + } + if (sensor != BLUE_PIN) + { + saveState(BLUE_PIN, 100 * blueval / 255); + send( BlueStatus.set(loadState(BLUE_PIN)), false ); + } + send( rgbShowState.set(0), false); +} + + + diff --git a/examples/RainGauge/RainGauge.ino b/examples/RainGauge/RainGauge.ino index 3e430e0..aaf6975 100644 --- a/examples/RainGauge/RainGauge.ino +++ b/examples/RainGauge/RainGauge.ino @@ -41,7 +41,7 @@ //#define MY_DEBUG // Enable MySensors debug prints to serial monitor // Enable and select radio type attached -#define MY_RADIO_NRF24 +#define MY_RADIO_RF24 //#define MY_RADIO_RFM69 //#define MY_RF24_PA_LEVEL RF24_PA_MAX //Options: RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX diff --git a/examples/RealTimeClockDisplaySensor/RealTimeClockDisplaySensor.ino b/examples/RealTimeClockDisplaySensor/RealTimeClockDisplaySensor.ino index 848d8b3..4e56219 100644 --- a/examples/RealTimeClockDisplaySensor/RealTimeClockDisplaySensor.ino +++ b/examples/RealTimeClockDisplaySensor/RealTimeClockDisplaySensor.ino @@ -43,7 +43,7 @@ #define MY_DEBUG // Enable and select radio type attached -#define MY_RADIO_NRF24 +#define MY_RADIO_RF24 //#define MY_RADIO_RFM69 #include diff --git a/examples/RelayWithButtonActuator/RelayWithButtonActuator.ino b/examples/RelayWithButtonActuator/RelayWithButtonActuator.ino old mode 100644 new mode 100755 index 64c35fa..2a2b53a --- a/examples/RelayWithButtonActuator/RelayWithButtonActuator.ino +++ b/examples/RelayWithButtonActuator/RelayWithButtonActuator.ino @@ -1,121 +1,119 @@ -/** - * 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 - * 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. - * - ******************************* - * - * REVISION HISTORY - * Version 1.0 - Henrik Ekblad - * - * DESCRIPTION - * Example sketch for a "light switch" where you can control light or something - * else from both HA controller and a local physical button - * (connected between digital pin 3 and GND). - * This node also works as a repeader for other nodes - * http://www.mysensors.org/build/relay - */ - -// Enable debug prints to serial monitor -#define MY_DEBUG - -// Enable and select radio type attached -#define MY_RADIO_NRF24 -//#define MY_RADIO_RFM69 - -// Enabled repeater feature for this node -#define MY_REPEATER_FEATURE - -#include -#include -#include - -#define RELAY_PIN 4 // Arduino Digital I/O pin number for relay -#define BUTTON_PIN 3 // Arduino Digital I/O pin number for button -#define CHILD_ID 1 // Id of the sensor child -#define RELAY_ON 1 -#define RELAY_OFF 0 - -Bounce debouncer = Bounce(); -int oldValue=0; -bool state; - -MyMessage msg(CHILD_ID,V_LIGHT); - -void setup() -{ - // Setup the button - pinMode(BUTTON_PIN,INPUT); - // Activate internal pull-up - digitalWrite(BUTTON_PIN,HIGH); - - // After setting up the button, setup debouncer - debouncer.attach(BUTTON_PIN); - debouncer.interval(5); - - // Make sure relays are off when starting up - digitalWrite(RELAY_PIN, RELAY_OFF); - // Then set relay pins in output mode - pinMode(RELAY_PIN, OUTPUT); - - // Set relay to last known state (using eeprom storage) - state = loadState(CHILD_ID); - digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF); -} - -void presentation() { - // Send the sketch version information to the gateway and Controller - sendSketchInfo("Relay & Button", "1.0"); - - // Register all sensors to gw (they will be created as child devices) - present(CHILD_ID, S_LIGHT); -} - -/* -* Example on how to asynchronously check for new messages from gw -*/ -void loop() -{ - debouncer.update(); - // Get the update value - int value = debouncer.read(); - if (value != oldValue && value==0) { - send(msg.set(state?false:true), true); // Send new state and request ack back - } - oldValue = value; -} - -void receive(const MyMessage &message) { - // We only expect one type of message from controller. But we better check anyway. - if (message.isAck()) { - Serial.println("This is an ack from gateway"); - } - - if (message.type == V_LIGHT) { - // Change relay state - state = message.getBool(); - digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF); - // Store state in eeprom - saveState(CHILD_ID, state); - - // Write some debug info - Serial.print("Incoming change for sensor:"); - Serial.print(message.sensor); - Serial.print(", New status: "); - Serial.println(message.getBool()); - } -} - +/** + * 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 + * 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. + * + ******************************* + * + * REVISION HISTORY + * Version 1.0 - Henrik Ekblad + * + * DESCRIPTION + * Example sketch for a "light switch" where you can control light or something + * else from both HA controller and a local physical button + * (connected between digital pin 3 and GND). + * This node also works as a repeader for other nodes + * http://www.mysensors.org/build/relay + */ + +// Enable debug prints to serial monitor +#define MY_DEBUG + +// Enable and select radio type attached +#define MY_RADIO_RF24 +//#define MY_RADIO_RFM69 + +// Enabled repeater feature for this node +#define MY_REPEATER_FEATURE + +#include +#include + +#define RELAY_PIN 4 // Arduino Digital I/O pin number for relay +#define BUTTON_PIN 3 // Arduino Digital I/O pin number for button +#define CHILD_ID 1 // Id of the sensor child +#define RELAY_ON 1 +#define RELAY_OFF 0 + +Bounce debouncer = Bounce(); +int oldValue=0; +bool state; + +MyMessage msg(CHILD_ID,V_LIGHT); + +void setup() +{ + // Setup the button + pinMode(BUTTON_PIN,INPUT); + // Activate internal pull-up + digitalWrite(BUTTON_PIN,HIGH); + + // After setting up the button, setup debouncer + debouncer.attach(BUTTON_PIN); + debouncer.interval(5); + + // Make sure relays are off when starting up + digitalWrite(RELAY_PIN, RELAY_OFF); + // Then set relay pins in output mode + pinMode(RELAY_PIN, OUTPUT); + + // Set relay to last known state (using eeprom storage) + state = loadState(CHILD_ID); + digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF); +} + +void presentation() { + // Send the sketch version information to the gateway and Controller + sendSketchInfo("Relay & Button", "1.0"); + + // Register all sensors to gw (they will be created as child devices) + present(CHILD_ID, S_LIGHT); +} + +/* +* Example on how to asynchronously check for new messages from gw +*/ +void loop() +{ + debouncer.update(); + // Get the update value + int value = debouncer.read(); + if (value != oldValue && value==0) { + send(msg.set(state?false:true), true); // Send new state and request ack back + } + oldValue = value; +} + +void receive(const MyMessage &message) { + // We only expect one type of message from controller. But we better check anyway. + if (message.isAck()) { + Serial.println("This is an ack from gateway"); + } + + if (message.type == V_LIGHT) { + // Change relay state + state = message.getBool(); + digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF); + // Store state in eeprom + saveState(CHILD_ID, state); + + // Write some debug info + Serial.print("Incoming change for sensor:"); + Serial.print(message.sensor); + Serial.print(", New status: "); + Serial.println(message.getBool()); + } +} diff --git a/examples/SMSGatewayNode/SMSGatewayNode.ino b/examples/SMSGatewayNode/SMSGatewayNode.ino index 7f0b6e8..312b27e 100644 --- a/examples/SMSGatewayNode/SMSGatewayNode.ino +++ b/examples/SMSGatewayNode/SMSGatewayNode.ino @@ -83,7 +83,7 @@ #define GATEWAY_ID 0 // Enable and select radio type attached -#define MY_RADIO_NRF24 +#define MY_RADIO_RF24 //#define MY_RADIO_RFM69 // defines for sensors diff --git a/examples/ServoActuator/ServoActuator.ino b/examples/ServoActuator/ServoActuator.ino old mode 100644 new mode 100755 index 83cd718..d57d478 --- a/examples/ServoActuator/ServoActuator.ino +++ b/examples/ServoActuator/ServoActuator.ino @@ -1,106 +1,102 @@ -/** - * 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 - * 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. - * - ******************************* - * - * REVISION HISTORY - * Version 1.0 - Henrik Ekblad - * Contribution by: Derek Macias - * - * DESCRIPTION - * Example showing how to create an atuator for a servo. - * Connect red to +5V, Black or brown to GND and the last cable to Digital pin 3. - * The servo consumes much power and should probably have its own powersource.' - * The arduino might spontanally restart if too much power is used (happend - * to me when servo tried to pass the extreme positions = full load). - * http://www.mysensors.org/build/servo - */ - -// Enable debug prints to serial monitor -#define MY_DEBUG - -// Enable and select radio type attached -#define MY_RADIO_NRF24 -//#define MY_RADIO_RFM69 - -#include -#include -#include - -#define SERVO_DIGITAL_OUT_PIN 3 -#define SERVO_MIN 0 // Fine tune your servos min. 0-180 -#define SERVO_MAX 180 // Fine tune your servos max. 0-180 -#define DETACH_DELAY 900 // Tune this to let your movement finish before detaching the servo -#define CHILD_ID 10 // Id of the sensor child - -MyMessage msg(CHILD_ID, V_DIMMER); -Servo myservo; // create servo object to control a servo - // a maximum of eight servo objects can be created Sensor gw(9,10); -unsigned long timeOfLastChange = 0; -bool attachedServo = false; - -void setup() -{ - // Request last servo state at startup - request(CHILD_ID, V_DIMMER); -} - -void presentation() { - // Send the sketch version information to the gateway and Controller - sendSketchInfo("Servo", "1.0"); - - // Register all sensors to gw (they will be created as child devices) - present(CHILD_ID, S_COVER); -} - -void loop() -{ - if (attachedServo && millis() - timeOfLastChange > DETACH_DELAY) { - myservo.detach(); - attachedServo = false; - } -} - -void receive(const MyMessage &message) { - myservo.attach(SERVO_DIGITAL_OUT_PIN); - attachedServo = true; - if (message.type==V_DIMMER) { // This could be M_ACK_VARIABLE or M_SET_VARIABLE - int val = message.getInt(); - myservo.write(SERVO_MAX + (SERVO_MIN-SERVO_MAX)/100 * val); // sets the servo position 0-180 - // Write some debug info - Serial.print("Servo changed. new state: "); - Serial.println(val); - } else if (message.type==V_UP) { - Serial.println("Servo UP command"); - myservo.write(SERVO_MIN); - send(msg.set(100)); - } else if (message.type==V_DOWN) { - Serial.println("Servo DOWN command"); - myservo.write(SERVO_MAX); - send(msg.set(0)); - } else if (message.type==V_STOP) { - Serial.println("Servo STOP command"); - myservo.detach(); - attachedServo = false; - - } - timeOfLastChange = millis(); -} - - - +/** + * 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 + * 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. + * + ******************************* + * + * REVISION HISTORY + * Version 1.0 - Henrik Ekblad + * Contribution by: Derek Macias + * + * DESCRIPTION + * Example showing how to create an atuator for a servo. + * Connect red to +5V, Black or brown to GND and the last cable to Digital pin 3. + * The servo consumes much power and should probably have its own powersource.' + * The arduino might spontanally restart if too much power is used (happend + * to me when servo tried to pass the extreme positions = full load). + * http://www.mysensors.org/build/servo + */ + +// Enable debug prints to serial monitor +#define MY_DEBUG + +// Enable and select radio type attached +#define MY_RADIO_RF24 +//#define MY_RADIO_RFM69 + +#include +#include + +#define SERVO_DIGITAL_OUT_PIN 3 +#define SERVO_MIN 0 // Fine tune your servos min. 0-180 +#define SERVO_MAX 180 // Fine tune your servos max. 0-180 +#define DETACH_DELAY 900 // Tune this to let your movement finish before detaching the servo +#define CHILD_ID 10 // Id of the sensor child + +MyMessage msg(CHILD_ID, V_DIMMER); +Servo myservo; // create servo object to control a servo + // a maximum of eight servo objects can be created Sensor gw(9,10); +unsigned long timeOfLastChange = 0; +bool attachedServo = false; + +void setup() +{ + // Request last servo state at startup + request(CHILD_ID, V_DIMMER); +} + +void presentation() { + // Send the sketch version information to the gateway and Controller + sendSketchInfo("Servo", "1.0"); + + // Register all sensors to gw (they will be created as child devices) + present(CHILD_ID, S_COVER); +} + +void loop() +{ + if (attachedServo && millis() - timeOfLastChange > DETACH_DELAY) { + myservo.detach(); + attachedServo = false; + } +} + +void receive(const MyMessage &message) { + myservo.attach(SERVO_DIGITAL_OUT_PIN); + attachedServo = true; + if (message.type==V_DIMMER) { // This could be M_ACK_VARIABLE or M_SET_VARIABLE + int val = message.getInt(); + myservo.write(SERVO_MAX + (SERVO_MIN-SERVO_MAX)/100 * val); // sets the servo position 0-180 + // Write some debug info + Serial.print("Servo changed. new state: "); + Serial.println(val); + } else if (message.type==V_UP) { + Serial.println("Servo UP command"); + myservo.write(SERVO_MIN); + send(msg.set(100)); + } else if (message.type==V_DOWN) { + Serial.println("Servo DOWN command"); + myservo.write(SERVO_MAX); + send(msg.set(0)); + } else if (message.type==V_STOP) { + Serial.println("Servo STOP command"); + myservo.detach(); + attachedServo = false; + + } + timeOfLastChange = millis(); +} diff --git a/examples/Si7021TemperatureAndHumiditySensor/Si7021TemperatureAndHumiditySensor.ino b/examples/Si7021TemperatureAndHumiditySensor/Si7021TemperatureAndHumiditySensor.ino new file mode 100644 index 0000000..cc0cf2e --- /dev/null +++ b/examples/Si7021TemperatureAndHumiditySensor/Si7021TemperatureAndHumiditySensor.ino @@ -0,0 +1,128 @@ +/** + * 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 + * 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. + * + ******************************* + * + * REVISION HISTORY + * Version 1.0: Yveaux + * + * DESCRIPTION + * This sketch provides an example of how to implement a humidity/temperature + * sensor using a Si7021 sensor. + * + * For more information, please visit: + * http://www.mysensors.org/build/humiditySi7021 + * + */ + +// Enable debug prints +#define MY_DEBUG + +// Enable REPORT_BATTERY_LEVEL to measure battery level and send changes to gateway +//#define REPORT_BATTERY_LEVEL + +// Enable and select radio type attached +#define MY_RADIO_RF24 +//#define MY_RADIO_RFM69 +//#define MY_RS485 + +#include + +#define CHILD_ID_HUM 0 +#define CHILD_ID_TEMP 1 + +static bool metric = true; + +// Sleep time between sensor updates (in milliseconds) +static const uint64_t UPDATE_INTERVAL = 60000; + +#include +static SI7021 sensor; + +#ifdef REPORT_BATTERY_LEVEL +#include +static uint8_t oldBatteryPcnt = 200; // Initialize to 200 to assure first time value will be sent. +const float VccMin = 1.8; // Minimum expected Vcc level, in Volts: Brownout at 1.8V -> 0% +const float VccMax = 2.0*1.6; // Maximum expected Vcc level, in Volts: 2xAA fresh Alkaline -> 100% +const float VccCorrection = 1.0; // Measured Vcc by multimeter divided by reported Vcc +static Vcc vcc(VccCorrection); +#endif + +void presentation() +{ + // Send the sketch info to the gateway + sendSketchInfo("TemperatureAndHumidity", "1.0"); + + // Present sensors as children to gateway + present(CHILD_ID_HUM, S_HUM, "Humidity"); + present(CHILD_ID_TEMP, S_TEMP, "Temperature"); + + metric = getControllerConfig().isMetric; +} + +void setup() +{ + while (not sensor.begin()) + { + Serial.println(F("Sensor not detected!")); + delay(5000); + } +} + + +void loop() +{ + // Read temperature & humidity from sensor. + const float temperature = float( metric ? sensor.getCelsiusHundredths() : sensor.getFahrenheitHundredths() ) / 100.0; + const float humidity = float( sensor.getHumidityBasisPoints() ) / 100.0; + +#ifdef MY_DEBUG + Serial.print(F("Temp ")); + Serial.print(temperature); + Serial.print(metric ? 'C' : 'F'); + Serial.print(F("\tHum ")); + Serial.println(humidity); +#endif + + static MyMessage msgHum( CHILD_ID_HUM, V_HUM ); + static MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); + + send(msgTemp.set(temperature, 2)); + send(msgHum.set(humidity, 2)); + +#ifdef REPORT_BATTERY_LEVEL + const uint8_t batteryPcnt = static_cast(0.5 + vcc.Read_Perc(VccMin, VccMax)); + +#ifdef MY_DEBUG + Serial.print(F("Vbat ")); + Serial.print(vcc.Read_Volts()); + Serial.print(F("\tPerc ")); + Serial.println(batteryPcnt); +#endif + + // Battery readout should only go down. So report only when new value is smaller than previous one. + if ( batteryPcnt < oldBatteryPcnt ) + { + sendBatteryLevel(batteryPcnt); + oldBatteryPcnt = batteryPcnt; + } +#endif + + // Sleep until next update to save energy + sleep(UPDATE_INTERVAL); +} \ No newline at end of file diff --git a/examples/TimeAwareSensor/TimeAwareSensor.ino b/examples/TimeAwareSensor/TimeAwareSensor.ino old mode 100644 new mode 100755 index 053c5d1..38c8658 --- a/examples/TimeAwareSensor/TimeAwareSensor.ino +++ b/examples/TimeAwareSensor/TimeAwareSensor.ino @@ -1,98 +1,95 @@ -/** - * 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 - * 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. - * - ******************************* - * - * REVISION HISTORY - * Version 1.0 - Henrik Ekblad - * - * DESCRIPTION - * Example sketch showing how to request time from controller. - */ - -// Enable debug prints to serial monitor -#define MY_DEBUG - -// Enable and select radio type attached -#define MY_RADIO_NRF24 -//#define MY_RADIO_RFM69 - -#include -#include -#include - -bool timeReceived = false; -unsigned long lastUpdate=0, lastRequest=0; - -void setup() -{ - // Request time from controller. - requestTime(); -} - -void presentation() { - // Send the sketch version information to the gateway and Controller - sendSketchInfo("Clock", "1.0"); -} - -// This is called when a new time value was received -void receiveTime(unsigned long time) { - // Ok, set incoming time - setTime(time); - timeReceived = true; -} - -void loop() -{ - unsigned long now = millis(); - - // If no time has been received yet, request it every 10 second from controller - // When time has been received, request update every hour - if ((!timeReceived && (now-lastRequest) > (10UL*1000UL)) - || (timeReceived && (now-lastRequest) > (60UL*1000UL*60UL))) { - // Request time from controller. - Serial.println("requesting time"); - requestTime(); - lastRequest = now; - } - - // Print time every second - if (timeReceived && now-lastUpdate > 1000) { - Serial.print(hour()); - printDigits(minute()); - printDigits(second()); - Serial.print(" "); - Serial.print(day()); - Serial.print(" "); - Serial.print(month()); - Serial.print(" "); - Serial.print(year()); - Serial.println(); - lastUpdate = now; - } -} - -void printDigits(int digits){ - // utility function for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - - +/** + * 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 + * 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. + * + ******************************* + * + * REVISION HISTORY + * Version 1.0 - Henrik Ekblad + * + * DESCRIPTION + * Example sketch showing how to request time from controller. + */ + +// Enable debug prints to serial monitor +#define MY_DEBUG + +// Enable and select radio type attached +#define MY_RADIO_RF24 +//#define MY_RADIO_RFM69 + +#include +#include + +bool timeReceived = false; +unsigned long lastUpdate=0, lastRequest=0; + +void setup() +{ + // Request time from controller. + requestTime(); +} + +void presentation() { + // Send the sketch version information to the gateway and Controller + sendSketchInfo("Clock", "1.0"); +} + +// This is called when a new time value was received +void receiveTime(uint32_t time) { + // Ok, set incoming time + setTime(time); + timeReceived = true; +} + +void loop() +{ + unsigned long now = millis(); + + // If no time has been received yet, request it every 10 second from controller + // When time has been received, request update every hour + if ((!timeReceived && (now-lastRequest) > (10UL*1000UL)) + || (timeReceived && (now-lastRequest) > (60UL*1000UL*60UL))) { + // Request time from controller. + Serial.println("requesting time"); + requestTime(); + lastRequest = now; + } + + // Print time every second + if (timeReceived && now-lastUpdate > 1000) { + Serial.print(hour()); + printDigits(minute()); + printDigits(second()); + Serial.print(" "); + Serial.print(day()); + Serial.print(" "); + Serial.print(month()); + Serial.print(" "); + Serial.print(year()); + Serial.println(); + lastUpdate = now; + } +} + +void printDigits(int digits){ + // utility function for digital clock display: prints preceding colon and leading 0 + Serial.print(":"); + if(digits < 10) + Serial.print('0'); + Serial.print(digits); +} diff --git a/examples/TouchDisplaySceneControllerSensor/TouchDisplaySceneControllerSensor.ino b/examples/TouchDisplaySceneControllerSensor/TouchDisplaySceneControllerSensor.ino index 1636ef8..b8bd1e7 100644 --- a/examples/TouchDisplaySceneControllerSensor/TouchDisplaySceneControllerSensor.ino +++ b/examples/TouchDisplaySceneControllerSensor/TouchDisplaySceneControllerSensor.ino @@ -59,7 +59,7 @@ #define MY_SOFTSPI // Enable and select radio type attached -#define MY_RADIO_NRF24 +#define MY_RADIO_RF24 //#define MY_RADIO_RFM69 #include diff --git a/examples/WeatherStationSensor/WeatherStationSensor.ino b/examples/WeatherStationSensor/WeatherStationSensor.ino index f3747a8..a4a09e8 100644 --- a/examples/WeatherStationSensor/WeatherStationSensor.ino +++ b/examples/WeatherStationSensor/WeatherStationSensor.ino @@ -1,522 +1,422 @@ /** -* 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 -* 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. -* -******************************* -* -* REVISION HISTORY -* Version 1.0 - Henrik Ekblad -* -* DESCRIPTION -* Weather station sensor example using BMP085 and DHT-11 or DHT-22 module -* http://www.mysensors.org/build/pressure -* http://www.mysensors.org/build/humidity -* -* WIRING -* DHT Sensor -* PIN2 D3 Arduino -* 10k resistor between PIN1 and PIN2 -* -* BMP Sensor -* SCL A5 Arduino -* SDA A4 Arduino -*/ - -// Enable debug prints to serial monitor -//#define MY_DEBUG + * 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 + * 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. + * + ******************************* + * + * REVISION HISTORY + * Version 1.0 - Henrik Ekblad + * Version 1.1 - AnonymousZebra + * + * DESCRIPTION + * This is an example of using the Bosch BME280 module, which can measure temperature, humidity and airpressure, + * and do so really accurately, while using very little power. A 3.3v and a 5v version is available, make sure to check which one you have. + * It communicates over the I2C protocol. + * + * This script uses the BME280 library by Embedded Adventures. Download it, and place it in your Arduino library folder. + * https://github.com/embeddedadventures/BME280 + * + * Connect your sensor's powerlines, and connect your sensor to the SDA and SCL pins of your board. + * On Arduino Nano SDA is pin A4, and SCL is pin A5. + * On the Ardunio Mega and Due the SDA in pin 20, and the SCL is pin 21. + * On the Arduino Leonardo and Pro Micro 2 the SDA in pin 2, and the SCL is pin 3. + * + * This script has been written in such a way that it can at the same time function as a repeater-node. + * It can also easily be used on battery power. Booth features can be turned on in the code below. + * + * Finally, you can decide if you want the forecast feature to be turned on. This is a cool feature, + * but there is a catch: it also means that you are locked into taking a measurement exactly once a + * minute, to build up prediction data for the algorithm. + * + * The reason so many variables have BME280 at the beginning, is so that it is easier to combine multiple sensors on one Arduino. + * + * + * The BME280 datasheet: https://cdn.sparkfun.com/assets/learn_tutorials/4/1/9/BST-BME280_DS001-10.pdf + * + */ + +// if you uncomment this, you can get test and debug updates about everything the sensor is doing by using the serial monitor tool. +#define MY_DEBUG // Enable and select radio type attached -#define MY_RADIO_NRF24 -//#define MY_RADIO_RFM69 - -#include -#include - -#include -#include - -#include - -// Set this to true if you want to send values altough the values did not change. -// This is only recommended when not running on batteries. -const bool SEND_ALWAYS = true; - -// Adapt this constant: set it to the altitude above sealevel at your home location. -const float SEALEVEL = 688; // meters above sealevel - -// Constant for the world wide average pressure -const float SEALEVEL_PRESSURE = 1013.25; - -// ---------------------------------------------------------------------------- -// Child sensor ids -#define CHILD_ID_BARO 0 -#define CHILD_ID_TEMP1 1 -#define CHILD_ID_HUM 2 -#define CHILD_ID_TEMP2 3 - -// ---------------------------------------------------------------------------- -// DHT-11/22 -#define HUMIDITY_SENSOR_DIGITAL_PIN 3 - -DHT dht; -float lastTemp; -float lastHum; -bool metric = true; -MyMessage msgHum(CHILD_ID_HUM, V_HUM); -MyMessage msgTemp(CHILD_ID_TEMP2, V_TEMP); - -// ---------------------------------------------------------------------------- -// BMP085 -unsigned long SLEEP_TIME1 = 30000; // 1 minute required for forecast algorithm -unsigned long SLEEP_TIME2 = 30000; // 1 minute required for forecast algorithm - -Adafruit_BMP085 bmp = Adafruit_BMP085(); // Digital Pressure Sensor - - -/* -DP/Dt explanation -0 = "Stable Weather Pattern" -1 = "Slowly rising Good Weather", "Clear/Sunny " -2 = "Slowly falling L-Pressure ", "Cloudy/Rain " -3 = "Quickly rising H-Press", "Not Stable" -4 = "Quickly falling L-Press", "Thunderstorm" -5 = "Unknown (More Time needed) -*/ - -const char *weatherStrings[] = { "stable", "sunny", "cloudy", "unstable", "thunderstorm", "unknown" }; +#define MY_RADIO_RF24 // A 2.4Ghz transmitter and receiver, often used with MySensors. +// #define MY_RF24_PA_LEVEL RF24_PA_MIN // This sets a low-power mode for the radio. Useful if you use the version with the bigger antenna, but don't want to power that from a separate power source. It can also fix problems with fake Chinese versions of the radio. +// #define MY_RADIO_RFM69 // 433Mhz transmitter and reveiver. + +// Do you want this sensor to also be a repeater? +// #define MY_REPEATER_FEATURE // Just remove the two slashes at the beginning of this line to also enable this sensor to act as a repeater for other sensors. If this node is on battery power, you probably shouldn't enable this. + +// Are you using this sensor on battery power? +// #define BATTERY_POWERED // Just remove the two slashes at the beginning of this line if your node is battery powered. It will then go into deep sleep as much as possible. While it's sleeping it can't work as a repeater! + +// Would you like the sensor to generate a weather forecast based on the barometric pressure? +#define GENERATE_FORECAST // Just remove the two slashes at the beginning of this line to enable this feature. + + +// LIBRARIES +#include // A communication backbone, the Serial Peripheral Interface. +#include // The MySensors library. Hurray! +#include // Enables the Wire communication protocol. +//#include // alternative library you could try (DIY; no code for this is in here yet). +//#include // alternative library you could try (DIY; no code for this is in here yet). +#include // Bosch BME280 Embedded Adventures MOD-1022 weather multi-sensor Arduino code, written originally by Embedded Adventures. https://github.com/embeddedadventures/BME280 + + +// VARIABLES YOU CAN CHANGE +const float ALTITUDE = 14; // Change this value to your location's altitude (in m). Use your smartphone GPS to get an accurate value, or use an online map. +unsigned long BME280measurementInterval = 60000; // Sleep time between reads for the BME sensor (in ms). Keep this value at 60000 if you have enabled the forecast feature, as the forecast algorithm needs a sample every minute. +#define COMPARE_TEMP 1 // Send temperature only if it changed? 1 = Yes 0 = No. Can save battery. +float tempThreshold = 0.1; // How big a temperature difference has to minimally be before an update is sent. Makes the sensor less precise, but also less jittery, and can save battery. +#define COMPARE_HUM 1 // Send temperature only if changed? 1 = Yes 0 = No. Can save battery. +float humThreshold = 0.1; // How big a humidity difference has to minimally be before an update is sent. Makes the sensor less precise, but also less jittery, and can save battery. +#define COMPARE_BARO 1 // Send temperature only if changed? 1 = Yes 0 = No. Can save battery. +float presThreshold = 0.1; // How big a barometric difference has to minimally be before an update is sent. Makes the sensor less precise, but also less jittery, and can save battery. + + +//VARIABLES YOU PROBABLY SHOULDN'T CHANGE +#define TEMP_CHILD_ID 0 // for MySensors. Within this node each sensortype should have its own ID number. +#define HUM_CHILD_ID 1 // for MySensors. Within this node each sensortype should have its own ID number. +#define BARO_CHILD_ID 2 // for MySensors. Within this node each sensortype should have its own ID number. +float lastTemperature = -1; // Stores the previous measurement, so it can be compared with a new measurement. +float lastHumidity = -1; // Stores the previous measurement, so it can be compared with a new measurement. +float lastPressure = -1; // Stores the previous measurement, so it can be compared with a new measurement. +unsigned long BME280measurementSleepTime = 0; // variable to store the calculated Sleep time if the node is battery powered. +bool metric = true; // Variable that stores if the sensor will output the temperature in Fahrenheit of Celsius. The gateway sends this preference to the node, so you dont need to change it here. +bool receivedConfig = false; // The MySensors gateway will tell the node if it should output in metric or not. + +#define CONVERSION_FACTOR (1.0/10.0) // used by forecast algorithm to convert from Pa to kPa, by dividing hPa by 10. + +#ifdef GENERATE_FORECAST // Below you will find a lot of variables used by the forecast algorithm. +const char *weather[] = { "stable", "sunny", "cloudy", "unstable", "thunderstorm", "unknown" }; enum FORECAST { - STABLE = 0, // Stable weather - SUNNY = 1, // Slowly rising HP stable good weather - CLOUDY = 2, // Slowly falling Low Pressure System, stable rainy weather - UNSTABLE = 3, // Quickly rising HP, not stable weather - THUNDERSTORM = 4, // Quickly falling LP, Thunderstorm, not stable - UNKNOWN = 5 // Unknown, more time needed -}; - -const char *situationStrings[] = { "very low", "low", "normal", "high", "very high" }; -enum WEATHER_SITUATION -{ - VERY_LOW_PRESSURE = 0, // p > -7.5hPa - LOW_PRESSURE = 1, // p > -2.5hPa - NORMAL_PRESSURE = 2, // p < +/-2.5hPa - HIGH_PRESSURE = 3, // p > +2.5hPa - VERY_HIGH_PRESSURE = 4, // p > +7.5hPa + STABLE = 0, // "Stable Weather Pattern" + SUNNY = 1, // "Slowly rising Good Weather", "Clear/Sunny " + CLOUDY = 2, // "Slowly falling L-Pressure ", "Cloudy/Rain " + UNSTABLE = 3, // "Quickly rising H-Press", "Not Stable" + THUNDERSTORM = 4, // "Quickly falling L-Press", "Thunderstorm" + UNKNOWN = 5 // "Unknown (More Time needed) }; - -float lastPressure = -1; -float lastPressureTemp = -1; -int lastForecast = -1; -int lastSituation = NORMAL_PRESSURE; - +int lastForecast = -1; // Stores the previous forecast, so it can be compared with a new forecast. const int LAST_SAMPLES_COUNT = 5; float lastPressureSamples[LAST_SAMPLES_COUNT]; +int minuteCount = 0; // Helps the forecst algorithm keep time. +bool firstRound = true; // Helps the forecast algorithm recognise if the sensor has just been powered up. +float pressureAvg; // Average value is used in forecast algorithm. +float pressureAvg2; // Average after 2 hours is used as reference value for the next iteration. +float dP_dt; // Pressure delta over time +#endif + +// MYSENSORS COMMUNICATION VARIABLES +MyMessage temperatureMsg(TEMP_CHILD_ID, V_TEMP); +MyMessage humidityMsg(HUM_CHILD_ID, V_HUM); +MyMessage pressureMsg(BARO_CHILD_ID, V_PRESSURE); +#ifdef GENERATE_FORECAST +MyMessage forecastMsg(BARO_CHILD_ID, V_FORECAST); +#endif + + +void setup() { + Wire.begin(); // Wire.begin(sda, scl) // starts the wire communication protocol, used to chat with the BME280 sensor. + Serial.begin(115200); // for serial debugging over USB. + Serial.println("Hello world, I am a sensor node."); + +#ifdef BATTERY_POWERED // If the node is battery powered, we'll let Sleep take over the scheduling. + BME280measurementSleepTime = BME280measurementInterval; + BME280measurementInterval = 0; // When the Arduino is asleep, millis doesn't increment anymore (time stops as it were). To fix this, we'll set the measurement interval time to 1, so that when the arduino wakes up it will immediately try to measure again. +#endif -// get kPa/h be dividing hPa by 10 -#define CONVERSION_FACTOR (1.0/10.0) - -int minuteCount = 0; -bool firstRound = true; - -// average value is used in forecast algorithm. -float pressureAvg; -// average after 2 hours is used as reference value for the next iteration. -float pressureAvg2; -float dP_dt; - -MyMessage tempMsg(CHILD_ID_TEMP1, V_TEMP); -MyMessage pressureMsg(CHILD_ID_BARO, V_PRESSURE); -MyMessage forecastMsg(CHILD_ID_BARO, V_FORECAST); -MyMessage situationMsg(CHILD_ID_BARO, V_VAR1); -MyMessage forecastMsg2(CHILD_ID_BARO, V_VAR2); - - -void initPressureSensor() -{ - if (!bmp.begin()) - { - Serial.println(F("Could not find a valid BMP085 sensor, check wiring!")); - while (1) {} - } -} - -void initHumiditySensor() -{ - dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); } -void setup() -{ - initPressureSensor(); - initHumiditySensor(); - metric = getControllerConfig().isMetric; -} -void presentation() -{ - sendSketchInfo("Weather Station Sensor", "1.0"); +void presentation() { + // Send the sketch version information to the gateway and Controller + sendSketchInfo("BME280 Sensor", "1.1"); - present(CHILD_ID_BARO, S_BARO); - present(CHILD_ID_TEMP1, S_TEMP); - present(CHILD_ID_HUM, S_HUM); - present(CHILD_ID_TEMP2, S_TEMP); + // Tell the MySensors gateway what kind of sensors this node has, and what their ID's on the node are, as defined in the code above. + present(BARO_CHILD_ID, S_BARO); + present(TEMP_CHILD_ID, S_TEMP); + present(HUM_CHILD_ID, S_HUM); } -int getWeatherSituation(float pressure) -{ - int situation = NORMAL_PRESSURE; - - float delta = pressure - SEALEVEL_PRESSURE; - if (delta > 7.5) - { - situation = VERY_HIGH_PRESSURE; - } - else if (delta > 2.5) - { - situation = HIGH_PRESSURE; - } - else if (delta < -7.5) - { - situation = VERY_LOW_PRESSURE; - } - else if (delta < -2.5) - { - situation = LOW_PRESSURE; - } - else - { - situation = NORMAL_PRESSURE; - } - - return situation; -} - -// The BMP provides a temperature value, too! -bool updatePressureSensor() -{ - bool changed = false; - - //sealevel pressure p0 from absolute pressure. - float pressure = bmp.readSealevelPressure(SEALEVEL) / 100.0; - float temperature = bmp.readTemperature(); - if (!metric) - { - // Convert to fahrenheit - temperature = temperature * 9.0 / 5.0 + 32.0; - } - - int forecast = sample(pressure); - int situation = getWeatherSituation(pressure); - - - if (SEND_ALWAYS || (temperature != lastPressureTemp)) - { - changed = true; - lastPressureTemp = temperature; - #ifdef MY_DEBUG - Serial.print(F("Temperature = ")); - Serial.print(temperature); - Serial.println(metric ? F(" *C") : F(" *F")); - #endif - if (!send(tempMsg.set(lastPressureTemp, 1))) - { - lastPressureTemp = -1.0; - } - } - - if (SEND_ALWAYS || (pressure != lastPressure)) - { - changed = true; - lastPressure = pressure; - #ifdef MY_DEBUG - Serial.print(F("sealevel Pressure = ")); - Serial.print(pressure); - Serial.println(F(" hPa")); - #endif - if (!send(pressureMsg.set(lastPressure, 1))) - { - lastPressure = -1.0; - } - } - - if (SEND_ALWAYS || (forecast != lastForecast)) - { - changed = true; - lastForecast = forecast; - #ifdef MY_DEBUG - Serial.print(F("Forecast = ")); - Serial.println(weatherStrings[forecast]); - #endif - if (send(forecastMsg.set(weatherStrings[lastForecast]))) - { - if (!send(forecastMsg2.set(lastForecast))) - { - } - } - else - { - lastForecast = -1.0; - } - } - - if (SEND_ALWAYS || (situation != lastSituation)) - { - changed = true; - lastSituation = situation; - #ifdef MY_DEBUG - Serial.print(F("Situation = ")); - Serial.println(situationStrings[situation]); - #endif - if (!send(situationMsg.set(lastSituation, 0))) - { - lastSituation = -1.0; - } - } - - - return changed; -} - -// The dht provides a temperature, too! -bool updateHumiditySensor() -{ - bool changed = false; - - float temperature = dht.getTemperature(); - float humidity = dht.getHumidity(); - - if (!isnan(temperature)) - { - if (SEND_ALWAYS || (temperature != lastTemp)) - { - lastTemp = temperature; - if (!metric) - { - temperature = dht.toFahrenheit(temperature); - } - - changed = true; - #ifdef MY_DEBUG - Serial.print(F("T: ")); - Serial.println(temperature); - #endif - if (!send(msgTemp.set(temperature, 1))) - { - lastTemp = -1.0; - } - } - } - else - { - Serial.println(F("Failed reading temperature from DHT")); - } - - - if (!isnan(humidity)) - { - if (SEND_ALWAYS || (humidity != lastHum)) - { - lastHum = humidity; - changed = true; - #ifdef MY_DEBUG - Serial.print(F("H: ")); - Serial.println(humidity); - #endif - if (!send(msgHum.set(lastHum, 1))) - { - lastHum = -1.0; - } - } - } - else - { - Serial.println(F("Failed reading humidity from DHT")); - } - - return changed; -} - -void loop() -{ - updatePressureSensor(); - sleep(SLEEP_TIME1); - updateHumiditySensor(); - sleep(SLEEP_TIME2); -} +void loop() { + + // You should not change these variables: + static unsigned long previousBME280Millis = 0; // Used to remember the time that the BME280 sensor was asked for a measurement. + unsigned long currentMillis = millis(); // The time since the sensor started, counted in milliseconds. This script tries to avoid using the Sleep function, so that it could at the same time be a MySensors repeater. + static boolean BME280shouldAsk = true; // This is true when the time is right for a new measurement to be made. + static boolean BME280justAsked = false; // This indicates whether we have just asked the sensor module for a measurement, so the receiving part of the code (part 2) should be primed. This two-part construction helps to bridge the time where the BME280 module is busy, without blocking the entire node from doing anything else (like being a repeater, or working with other connected sensor modules). + + + // PART 1. If enough time has passed, a new measurement should be taken: + if (BME280shouldAsk == true && currentMillis - previousBME280Millis >= BME280measurementInterval) { + previousBME280Millis = currentMillis; // store the current time as the previous measurement start time. + BME280shouldAsk = false; + Serial.println(""); + Serial.println("BME280 - Requesting new data from sensor module."); + BME280.readCompensationParams(); // Need to read the NVM compensation parameters. + +#ifdef BATTERY_POWERED + // After taking the measurement the chip goes back to sleep. This code is only enabled if you enabled BATTERY POWERED at the top of this script. + // Oversampling settings (os1x, os2x, os4x, os8x or os16x). + BME280.writeFilterCoefficient(fc_16); // IIR Filter coefficient, higher numbers avoid sudden changes to be accounted for (such as slamming a door) + BME280.writeOversamplingPressure(os16x); // pressure x16 + BME280.writeOversamplingTemperature(os8x); // temperature x8 + BME280.writeOversamplingHumidity(os8x); // humidity x8 + BME280.writeMode(smForced); // Forced sample. After taking the measurement the chip goes back to sleep. +#else + // Normal mode for regular automatic samples + BME280.writeStandbyTime(tsb_0p5ms); // tsb = 0.5ms + BME280.writeFilterCoefficient(fc_16); // IIR Filter coefficient 16 + BME280.writeOversamplingPressure(os16x); // pressure x16 + BME280.writeOversamplingTemperature(os8x); // temperature x8 + BME280.writeOversamplingHumidity(os8x); // humidity x8 + BME280.writeMode(smNormal); +#endif + + // As we exit part 1, in theory BME280.isMeasuring() should now be true. + BME280justAsked = true; + } + + + // Part 2. This will trigger if the sensor has just been asked for a measurement, and is also just done figuring out those measurements. + if(BME280justAsked == true && BME280.isMeasuring() == false) { // + BME280justAsked = false; // makes sure we don't do this part again in the next pass through the main loop. + Serial.println("BME280 - Sensor module has some new values ready:"); + + // Read out the data - must do this before calling the getxxxxx routines + BME280.readMeasurements(); + + float temperature = BME280.getTemperatureMostAccurate(); // Must get the temperature first. + float humidity = BME280.getHumidityMostAccurate(); // Get the humidity. + float pressure_local = BME280.getPressureMostAccurate(); // Get pressure at current location + float pressure = pressure_local/pow((1.0 - ( ALTITUDE / 44330.0 )), 5.255); // Adjust to sea level pressure using user altitude +#ifdef GENERATE_FORECAST + int forecast = sample(pressure); // Run the forecast function with a new pressure update. +#endif + + if (!metric) { + // Convert temperature to fahrenheit + temperature = temperature * 9.0 / 5.0 + 32.0; + } + + // Useful for debugging + Serial.print("BME280 - Temperature = "); + Serial.print(temperature); + Serial.println(metric ? " °C" : " °F"); + Serial.print("BME280 - Humidity = "); + Serial.print(humidity); + Serial.println(" %"); + Serial.print("BME280 - Pressure = "); + Serial.print(pressure); + Serial.println(" hPa"); +#ifdef GENERATE_FORECAST + Serial.print("BME280 - Forecast = "); + Serial.println(weather[forecast]); +#endif + + // Now, let's send the measurements to the gateway. + + // Send temperature + if (COMPARE_TEMP == 1 && abs(temperature - lastTemperature) < tempThreshold) { // is the temperature difference bigger than the threshold? + Serial.print(temperature - lastTemperature); + Serial.print("- BME280 - Temperature difference too small, so not sending the new measurement to the gateway.\n"); + } else { + Serial.print("BME280 - Sending the new temperature to the gateway.\n"); + send(temperatureMsg.set(temperature, 1)); + lastTemperature = temperature; // Save new temperatures to be able to compare in the next round. + } + + // Send humidity + if (COMPARE_TEMP == 1 && abs(humidity - lastHumidity) < humThreshold) { // is the humidity difference bigger than the threshold? + Serial.print(humidity - lastHumidity); + Serial.println("- BME280 - Humidity difference too small, so not sending the new measurement to the gateway."); + } else { + Serial.println("BME280 - Sending the new humidity to the gateway."); + send(humidityMsg.set(humidity, 1)); + lastHumidity = humidity; // Save new humidity to be able to compare in the next round. + } + + // Send pressure + if (COMPARE_TEMP == 1 && abs(pressure - lastPressure) < presThreshold) { // is the pressure difference bigger than the threshold? + Serial.print(pressure - lastPressure); + Serial.println("- BME280 - Pressure difference too small, so not sending the new measurement to the gateway."); + } else { + Serial.println("BME280 - Sending the new pressure to the gateway."); + send(pressureMsg.set(pressure, 1)); + lastPressure = pressure; // Save new pressure to be able to compare in the next round. + } + +#ifdef GENERATE_FORECAST + // Send forecast + if (forecast != lastForecast) { + Serial.println("BME280 - Sending the latest forecast to the gateway."); + send(forecastMsg.set(weather[forecast])); + lastForecast = forecast; + } +#endif + + Serial.println("BME280 - Measurement complete. Going to wait until next measurement."); + BME280shouldAsk = true; // Ready for the new round. + } + +#ifdef BATTERY_POWERED + // This code will only be included in the sketch if the BATTERY POWERED feature is enabled. + if(BME280shouldAsk == true && BME280justAsked == false) { // Both parts are done, so we can let the sensor sleep again. + unsigned long quicktimecheck = millis(); // To check how much time has passed since the beginning of being awake, and then calculate from that how long to sleep until the next intended measuring time, we need to know how many milliseconds have passed. + unsigned long sleeptime = BME280measurementSleepTime - (quicktimecheck - previousBME280Millis); // How much time has passed already during the calculating? Subtract that from the intended interval time. + Serial.println("BME280 - zzzzZZZZzzzzZZZZzzzz"); + sleep (sleeptime); + Serial.println("BME280 - Waking up."); + } +#endif + +} // end of main loop. + + + +#ifdef GENERATE_FORECAST +// These functions are only included if the forecast function is enables. The are used to generate a weater prediction by checking if the barometric pressure is rising or falling over time. float getLastPressureSamplesAverage() { - float lastPressureSamplesAverage = 0; - for (int i = 0; i < LAST_SAMPLES_COUNT; i++) - { - lastPressureSamplesAverage += lastPressureSamples[i]; - } - - lastPressureSamplesAverage /= LAST_SAMPLES_COUNT; - - // Uncomment when dubugging - //Serial.print(F("### 5min-Average:")); - //Serial.print(lastPressureSamplesAverage); - //Serial.println(F(" hPa")); + float lastPressureSamplesAverage = 0; + for (int i = 0; i < LAST_SAMPLES_COUNT; i++) { + lastPressureSamplesAverage += lastPressureSamples[i]; + } + lastPressureSamplesAverage /= LAST_SAMPLES_COUNT; - return lastPressureSamplesAverage; + return lastPressureSamplesAverage; } - -// Algorithm found here +// Forecast algorithm found here // http://www.freescale.com/files/sensors/doc/app_note/AN3914.pdf // Pressure in hPa --> forecast done by calculating kPa/h -int sample(float pressure) -{ - // Calculate the average of the last n minutes. - int index = minuteCount % LAST_SAMPLES_COUNT; - lastPressureSamples[index] = pressure; - - minuteCount++; - if (minuteCount > 185) - { - minuteCount = 6; - } - - if (minuteCount == 5) - { - pressureAvg = getLastPressureSamplesAverage(); - } - else if (minuteCount == 35) - { - float lastPressureAvg = getLastPressureSamplesAverage(); - float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; - if (firstRound) // first time initial 3 hour - { - dP_dt = change * 2; // note this is for t = 0.5hour - } - else - { - dP_dt = change / 1.5; // divide by 1.5 as this is the difference in time from 0 value. - } - } - else if (minuteCount == 65) - { - float lastPressureAvg = getLastPressureSamplesAverage(); - float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; - if (firstRound) //first time initial 3 hour - { - dP_dt = change; //note this is for t = 1 hour - } - else - { - dP_dt = change / 2; //divide by 2 as this is the difference in time from 0 value - } - } - else if (minuteCount == 95) - { - float lastPressureAvg = getLastPressureSamplesAverage(); - float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; - if (firstRound) // first time initial 3 hour - { - dP_dt = change / 1.5; // note this is for t = 1.5 hour - } - else - { - dP_dt = change / 2.5; // divide by 2.5 as this is the difference in time from 0 value - } - } - else if (minuteCount == 125) - { - float lastPressureAvg = getLastPressureSamplesAverage(); - pressureAvg2 = lastPressureAvg; // store for later use. - float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; - if (firstRound) // first time initial 3 hour - { - dP_dt = change / 2; // note this is for t = 2 hour - } - else - { - dP_dt = change / 3; // divide by 3 as this is the difference in time from 0 value - } - } - else if (minuteCount == 155) - { - float lastPressureAvg = getLastPressureSamplesAverage(); - float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; - if (firstRound) // first time initial 3 hour - { - dP_dt = change / 2.5; // note this is for t = 2.5 hour - } - else - { - dP_dt = change / 3.5; // divide by 3.5 as this is the difference in time from 0 value - } - } - else if (minuteCount == 185) - { - float lastPressureAvg = getLastPressureSamplesAverage(); - float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; - if (firstRound) // first time initial 3 hour - { - dP_dt = change / 3; // note this is for t = 3 hour - } - else - { - dP_dt = change / 4; // divide by 4 as this is the difference in time from 0 value - } - pressureAvg = pressureAvg2; // Equating the pressure at 0 to the pressure at 2 hour after 3 hours have past. - firstRound = false; // flag to let you know that this is on the past 3 hour mark. Initialized to 0 outside main loop. - } - - int forecast = UNKNOWN; - if (minuteCount < 35 && firstRound) //if time is less than 35 min on the first 3 hour interval. - { - forecast = UNKNOWN; - } - else if (dP_dt < (-0.25)) - { - forecast = THUNDERSTORM; - } - else if (dP_dt > 0.25) - { - forecast = UNSTABLE; - } - else if ((dP_dt > (-0.25)) && (dP_dt < (-0.05))) - { - forecast = CLOUDY; - } - else if ((dP_dt > 0.05) && (dP_dt < 0.25)) - { - forecast = SUNNY; - } - else if ((dP_dt >(-0.05)) && (dP_dt < 0.05)) - { - forecast = STABLE; - } - else - { - forecast = UNKNOWN; - } - - // Uncomment when dubugging - // Serial.print(F("Forecast at minute ")); - // Serial.print(minuteCount); - // Serial.print(F(" dP/dt = ")); - // Serial.print(dP_dt); - // Serial.print(F("kPa/h --> ")); - // Serial.println(weatherStrings[forecast]); - - return forecast; +int sample(float pressure) { + // Calculate the average of the last n minutes. + int index = minuteCount % LAST_SAMPLES_COUNT; + lastPressureSamples[index] = pressure; + + minuteCount++; + if (minuteCount > 185) { + minuteCount = 6; + } + + if (minuteCount == 5) { + pressureAvg = getLastPressureSamplesAverage(); + } + else if (minuteCount == 35) { + float lastPressureAvg = getLastPressureSamplesAverage(); + float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; + if (firstRound) { // first time initial 3 hour + dP_dt = change * 2; // note this is for t = 0.5hour + } + else { + dP_dt = change / 1.5; // divide by 1.5 as this is the difference in time from 0 value. + } + } + else if (minuteCount == 65) { + float lastPressureAvg = getLastPressureSamplesAverage(); + float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; + if (firstRound) { //first time initial 3 hour + dP_dt = change; //note this is for t = 1 hour + } + else { + dP_dt = change / 2; //divide by 2 as this is the difference in time from 0 value + } + } + else if (minuteCount == 95) { + float lastPressureAvg = getLastPressureSamplesAverage(); + float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; + if (firstRound) { // first time initial 3 hour + dP_dt = change / 1.5; // note this is for t = 1.5 hour + } + else { + dP_dt = change / 2.5; // divide by 2.5 as this is the difference in time from 0 value + } + } + else if (minuteCount == 125) { + float lastPressureAvg = getLastPressureSamplesAverage(); + pressureAvg2 = lastPressureAvg; // store for later use. + float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; + if (firstRound) { // first time initial 3 hour + dP_dt = change / 2; // note this is for t = 2 hour + } + else { + dP_dt = change / 3; // divide by 3 as this is the difference in time from 0 value + } + } + else if (minuteCount == 155) { + float lastPressureAvg = getLastPressureSamplesAverage(); + float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; + if (firstRound) { // first time initial 3 hour + dP_dt = change / 2.5; // note this is for t = 2.5 hour + } + else { + dP_dt = change / 3.5; // divide by 3.5 as this is the difference in time from 0 value + } + } + else if (minuteCount == 185) { + float lastPressureAvg = getLastPressureSamplesAverage(); + float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR; + if (firstRound) { // first time initial 3 hour + dP_dt = change / 3; // note this is for t = 3 hour + } + else { + dP_dt = change / 4; // divide by 4 as this is the difference in time from 0 value + } + pressureAvg = pressureAvg2; // Equating the pressure at 0 to the pressure at 2 hour after 3 hours have past. + firstRound = false; // flag to let you know that this is on the past 3 hour mark. Initialized to 0 outside main loop. + } + + int forecast = UNKNOWN; + if (minuteCount < 35 && firstRound) { //if time is less than 35 min on the first 3 hour interval. + forecast = UNKNOWN; + } + else if (dP_dt < (-0.25)) { + forecast = THUNDERSTORM; + } + else if (dP_dt > 0.25) { + forecast = UNSTABLE; + } + else if ((dP_dt > (-0.25)) && (dP_dt < (-0.05))) { + forecast = CLOUDY; + } + else if ((dP_dt > 0.05) && (dP_dt < 0.25)) + { + forecast = SUNNY; + } + else if ((dP_dt >(-0.05)) && (dP_dt < 0.05)) { + forecast = STABLE; + } + else { + forecast = UNKNOWN; + } + + // uncomment when debugging + //Serial.print(F("BME280 - Forecast at minute ")); + //Serial.print(minuteCount); + //Serial.print(F(" dP/dt = ")); + //Serial.print(dP_dt); + //Serial.print(F("kPa/h --> ")); + //Serial.println(weather[forecast]); + + return forecast; } +#endif diff --git a/libraries/EmonLib/EmonLib.cpp b/libraries/EmonLib/EmonLib.cpp deleted file mode 100644 index 1a1fd29..0000000 --- a/libraries/EmonLib/EmonLib.cpp +++ /dev/null @@ -1,259 +0,0 @@ -/* - Emon.cpp - Library for openenergymonitor - Created by Trystan Lea, April 27 2010 - GNU GPL - modified to use up to 12 bits ADC resolution (ex. Arduino Due) - by boredman@boredomprojects.net 26.12.2013 -*/ - -//#include "WProgram.h" un-comment for use on older versions of Arduino IDE -#include "EmonLib.h" - -#if defined(ARDUINO) && ARDUINO >= 100 - -#include "Arduino.h" - -#else - -#include "WProgram.h" - -#endif - -//-------------------------------------------------------------------------------------- -// Sets the pins to be used for voltage and current sensors -//-------------------------------------------------------------------------------------- -void EnergyMonitor::voltage(int _inPinV, double _VCAL, double _PHASECAL) -{ - inPinV = _inPinV; - VCAL = _VCAL; - PHASECAL = _PHASECAL; -} - -void EnergyMonitor::current(int _inPinI, double _ICAL) -{ - inPinI = _inPinI; - ICAL = _ICAL; -} - -//-------------------------------------------------------------------------------------- -// Sets the pins to be used for voltage and current sensors based on emontx pin map -//-------------------------------------------------------------------------------------- -void EnergyMonitor::voltageTX(double _VCAL, double _PHASECAL) -{ - inPinV = 2; - VCAL = _VCAL; - PHASECAL = _PHASECAL; -} - -void EnergyMonitor::currentTX(int _channel, double _ICAL) -{ - if (_channel == 1) inPinI = 3; - if (_channel == 2) inPinI = 0; - if (_channel == 3) inPinI = 1; - ICAL = _ICAL; -} - -//-------------------------------------------------------------------------------------- -// emon_calc procedure -// Calculates realPower,apparentPower,powerFactor,Vrms,Irms,kwh increment -// From a sample window of the mains AC voltage and current. -// The Sample window length is defined by the number of half wavelengths or crossings we choose to measure. -//-------------------------------------------------------------------------------------- -void EnergyMonitor::calcVI(int crossings, int timeout) -{ - #if defined emonTxV3 - int SUPPLYVOLTAGE=3300; - #else - int SUPPLYVOLTAGE = readVcc(); - #endif - - int crossCount = 0; //Used to measure number of times threshold is crossed. - int numberOfSamples = 0; //This is now incremented - - //------------------------------------------------------------------------------------------------------------------------- - // 1) Waits for the waveform to be close to 'zero' (500 adc) part in sin curve. - //------------------------------------------------------------------------------------------------------------------------- - boolean st=false; //an indicator to exit the while loop - - unsigned long start = millis(); //millis()-start makes sure it doesnt get stuck in the loop if there is an error. - - while(st==false) //the while loop... - { - startV = analogRead(inPinV); //using the voltage waveform - if ((startV < (ADC_COUNTS/2+50)) && (startV > (ADC_COUNTS/2-50))) st=true; //check its within range - if ((millis()-start)>timeout) st = true; - } - - //------------------------------------------------------------------------------------------------------------------------- - // 2) Main measurment loop - //------------------------------------------------------------------------------------------------------------------------- - start = millis(); - - while ((crossCount < crossings) && ((millis()-start) startV) checkVCross = true; - else checkVCross = false; - if (numberOfSamples==1) lastVCross = checkVCross; - - if (lastVCross != checkVCross) crossCount++; - } - - //------------------------------------------------------------------------------------------------------------------------- - // 3) Post loop calculations - //------------------------------------------------------------------------------------------------------------------------- - //Calculation of the root of the mean of the voltage and current squared (rms) - //Calibration coeficients applied. - - double V_RATIO = VCAL *((SUPPLYVOLTAGE/1000.0) / (ADC_COUNTS)); - Vrms = V_RATIO * sqrt(sumV / numberOfSamples); - - double I_RATIO = ICAL *((SUPPLYVOLTAGE/1000.0) / (ADC_COUNTS)); - Irms = I_RATIO * sqrt(sumI / numberOfSamples); - - //Calculation power values - realPower = V_RATIO * I_RATIO * sumP / numberOfSamples; - apparentPower = Vrms * Irms; - powerFactor=realPower / apparentPower; - - //Reset accumulators - sumV = 0; - sumI = 0; - sumP = 0; -//-------------------------------------------------------------------------------------- -} - -//-------------------------------------------------------------------------------------- -double EnergyMonitor::calcIrms(int NUMBER_OF_SAMPLES) -{ - - #if defined emonTxV3 - int SUPPLYVOLTAGE=3300; - #else - int SUPPLYVOLTAGE = readVcc(); - #endif - - - for (int n = 0; n < NUMBER_OF_SAMPLES; n++) - { - lastSampleI = sampleI; - sampleI = analogRead(inPinI); - lastFilteredI = filteredI; - filteredI = 0.996*(lastFilteredI+sampleI-lastSampleI); - - // Root-mean-square method current - // 1) square current values - sqI = filteredI * filteredI; - // 2) sum - sumI += sqI; - } - - double I_RATIO = ICAL *((SUPPLYVOLTAGE/1000.0) / (ADC_COUNTS)); - Irms = I_RATIO * sqrt(sumI / NUMBER_OF_SAMPLES); - - //Reset accumulators - sumI = 0; -//-------------------------------------------------------------------------------------- - - return Irms; -} - -void EnergyMonitor::serialprint() -{ - Serial.print(realPower); - Serial.print(' '); - Serial.print(apparentPower); - Serial.print(' '); - Serial.print(Vrms); - Serial.print(' '); - Serial.print(Irms); - Serial.print(' '); - Serial.print(powerFactor); - Serial.println(' '); - delay(100); -} - -//thanks to http://hacking.majenko.co.uk/making-accurate-adc-readings-on-arduino -//and Jérôme who alerted us to http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/ - -long EnergyMonitor::readVcc() { - long result; - - //not used on emonTx V3 - as Vcc is always 3.3V - eliminates bandgap error and need for calibration http://harizanov.com/2013/09/thoughts-on-avr-adc-accuracy/ - - #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328__) || defined (__AVR_ATmega328P__) - ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); - #elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_AT90USB1286__) - ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); - ADCSRB &= ~_BV(MUX5); // Without this the function always returns -1 on the ATmega2560 http://openenergymonitor.org/emon/node/2253#comment-11432 - #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) - ADMUX = _BV(MUX5) | _BV(MUX0); - #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) - ADMUX = _BV(MUX3) | _BV(MUX2); - - #endif - - - #if defined(__AVR__) - delay(2); // Wait for Vref to settle - ADCSRA |= _BV(ADSC); // Convert - while (bit_is_set(ADCSRA,ADSC)); - result = ADCL; - result |= ADCH<<8; - result = 1126400L / result; //1100mV*1024 ADC steps http://openenergymonitor.org/emon/node/1186 - return result; - #elif defined(__arm__) - return (3300); //Arduino Due - #else - return (3300); //Guess that other un-supported architectures will be running a 3.3V! - #endif -} - diff --git a/libraries/EmonLib/EmonLib.h b/libraries/EmonLib/EmonLib.h deleted file mode 100644 index 41cafcb..0000000 --- a/libraries/EmonLib/EmonLib.h +++ /dev/null @@ -1,89 +0,0 @@ -/* - Emon.h - Library for openenergymonitor - Created by Trystan Lea, April 27 2010 - GNU GPL - modified to use up to 12 bits ADC resolution (ex. Arduino Due) - by boredman@boredomprojects.net 26.12.2013 -*/ - -#ifndef EmonLib_h -#define EmonLib_h - -#if defined(ARDUINO) && ARDUINO >= 100 - -#include "Arduino.h" - -#else - -#include "WProgram.h" - -#endif - -// to enable 12-bit ADC resolution on Arduino Due, -// include the following line in main sketch inside setup() function: -// analogReadResolution(ADC_BITS); -// otherwise will default to 10 bits, as in regular Arduino-based boards. -#if defined(__arm__) -#define ADC_BITS 12 -#else -#define ADC_BITS 10 -#endif - -#define ADC_COUNTS (1< - -volatile irparams_t irparams; -/* - * Returns a pointer to a flash stored string that is the name of the protocol received. - */ -const __FlashStringHelper *Pnames(IRTYPES Type) { - if(Type>LAST_PROTOCOL) Type=UNKNOWN; - // You can add additional strings before the entry for hash code. - const __FlashStringHelper *Names[LAST_PROTOCOL+1]={F("Unknown"),F("NEC"),F("Sony"),F("RC5"),F("RC6"),F("Panasonic Old"),F("JVC"),F("NECx"),F("Hash Code")}; - return Names[Type]; -}; - - -#define TOPBIT 0x80000000 - -/* - * The IRsend classes contain a series of methods for sending various protocols. - * Each of these begin by calling enableIROut(unsigned char kHz) to set the carrier frequency. - * It then calls mark(int usec) and space(inc usec) to transmit marks and - * spaces of varying length of microseconds however the protocol defines. - * Because we want to separate the hardware specific portions of the code from the general programming - * portions of the code, the code for IRsendBase::IRsendBase, IRsendBase::enableIROut, - * IRsendBase::mark and IRsendBase::space can be found in the lower section of this file. - */ - -/* - * Most of the protocols have a header consisting of a mark/space of a particular length followed by - * a series of variable length mark/space signals. Depending on the protocol they very the lengths of the - * mark or the space to indicate a data bit of "0" or "1". Most also end with a stop bit of "1". - * The basic structure of the sending and decoding these protocols led to lots of redundant code. - * Therefore I have implemented generic sending and decoding routines. You just need to pass a bunch of customized - * parameters and it does the work. This reduces compiled code size with only minor speed degradation. - * You may be able to implement additional protocols by simply passing the proper values to these generic routines. - * The decoding routines do not encode stop bits. So you have to tell this routine whether or not to send one. - */ -void IRsendBase::sendGeneric(unsigned long data, unsigned char Num_Bits, unsigned int Head_Mark, unsigned int Head_Space, - unsigned int Mark_One, unsigned int Mark_Zero, unsigned int Space_One, unsigned int Space_Zero, - unsigned char kHz, bool Use_Stop, unsigned long Max_Extent) { - Extent=0; - data = data << (32 - Num_Bits); - enableIROut(kHz); -//Some protocols do not send a header when sending repeat codes. So we pass a zero value to indicate skipping this. - if(Head_Mark) mark(Head_Mark); - if(Head_Space) space(Head_Space); - for (int i = 0; i rawbuf,sizeof(irparams.rawbuf)); - rawlen=source->rawlen; -}; - -/* - * This routine is actually quite useful. Allows extended classes to call their parent - * if they fail to decode themselves. - */ -bool IRdecodeBase::decode(void) { - return false; -}; - -void IRdecodeBase::Reset(void) { - decode_type= UNKNOWN; - value=0; - bits=0; - rawlen=0; -}; - -/* - * This method dumps useful information about the decoded values. - */ -void IRdecodeBase::DumpResults(void) { - int i;unsigned long Extent;int interval; - if(decode_type<=LAST_PROTOCOL){ - Serial.print(F("Decoded ")); Serial.print(Pnames(decode_type)); - Serial.print(F(": Value:")); Serial.print(value, HEX); - }; - Serial.print(F(" (")); Serial.print(bits, DEC); Serial.println(F(" bits)")); - Serial.print(F("Raw samples(")); Serial.print(rawlen, DEC); - Serial.print(F("): Gap:")); Serial.println(rawbuf[0], DEC); - Serial.print(F(" Head: m")); Serial.print(rawbuf[1], DEC); - Serial.print(F(" s")); Serial.println(rawbuf[2], DEC); - int LowSpace= 32767; int LowMark= 32767; - int HiSpace=0; int HiMark= 0; - Extent=rawbuf[1]+rawbuf[2]; - for (i = 3; i < rawlen; i++) { - Extent+=(interval= rawbuf[i]); - if (i % 2) { - LowMark=min(LowMark, interval); HiMark=max(HiMark, interval); - Serial.print(i/2-1,DEC); Serial.print(F(":m")); - } - else { - if(interval>0)LowSpace=min(LowSpace, interval); HiSpace=max (HiSpace, interval); - Serial.print(F(" s")); - } - Serial.print(interval, DEC); - int j=i-1; - if ((j % 2)==1)Serial.print(F("\t")); - if ((j % 4)==1)Serial.print(F("\t ")); - if ((j % 8)==1)Serial.println(); - if ((j % 32)==1)Serial.println(); - } - Serial.println(); - Serial.print(F("Extent=")); Serial.println(Extent,DEC); - Serial.print(F("Mark min:")); Serial.print(LowMark,DEC);Serial.print(F("\t max:")); Serial.println(HiMark,DEC); - Serial.print(F("Space min:")); Serial.print(LowSpace,DEC);Serial.print(F("\t max:")); Serial.println(HiSpace,DEC); - Serial.println(); -} - -/* - * Again we use a generic routine because most protocols have the same basic structure. However we need to - * indicate whether or not the protocol varies the length of the mark or the space to indicate a "0" or "1". - * If "Mark_One" is zero. We assume that the length of the space varies. If "Mark_One" is not zero then - * we assume that the length of Mark varies and the value passed as "Space_Zero" is ignored. - * When using variable length Mark, assumes Head_Space==Space_One. If it doesn't, you need a specialized decoder. - */ -bool IRdecodeBase::decodeGeneric(unsigned char Raw_Count, unsigned int Head_Mark, unsigned int Head_Space, - unsigned int Mark_One, unsigned int Mark_Zero, unsigned int Space_One, unsigned int Space_Zero) { -// If raw samples count or head mark are zero then don't perform these tests. -// Some protocols need to do custom header work. - unsigned long data = 0; unsigned char Max; offset=1; - if (Raw_Count) {if (rawlen != Raw_Count) return RAW_COUNT_ERROR;} - if (Head_Mark) {if (!MATCH(rawbuf[offset],Head_Mark)) return HEADER_MARK_ERROR(Head_Mark);} - offset++; - if (Head_Space) {if (!MATCH(rawbuf[offset],Head_Space)) return HEADER_SPACE_ERROR(Head_Space);} - - if (Mark_One) {//Length of a mark indicates data "0" or "1". Space_Zero is ignored. - offset=2;//skip initial gap plus header Mark. - Max=rawlen; - while (offset < Max) { - if (!MATCH(rawbuf[offset], Space_One)) return DATA_SPACE_ERROR(Space_One); - offset++; - if (MATCH(rawbuf[offset], Mark_One)) { - data = (data << 1) | 1; - } - else if (MATCH(rawbuf[offset], Mark_Zero)) { - data <<= 1; - } - else return DATA_MARK_ERROR(Mark_Zero); - offset++; - } - bits = (offset - 1) / 2; - } - else {//Mark_One was 0 therefore length of a space indicates data "0" or "1". - Max=rawlen-1; //ignore stop bit - offset=3;//skip initial gap plus two header items - while (offset < Max) { - if (!MATCH (rawbuf[offset],Mark_Zero)) return DATA_MARK_ERROR(Mark_Zero); - offset++; - if (MATCH(rawbuf[offset],Space_One)) { - data = (data << 1) | 1; - } - else if (MATCH (rawbuf[offset],Space_Zero)) { - data <<= 1; - } - else return DATA_SPACE_ERROR(Space_Zero); - offset++; - } - bits = (offset - 1) / 2 -1;//didn't encode stop bit - } - // Success - value = data; - return true; -} - -/* - * This routine has been modified significantly from the original IRremote. - * It assumes you've already called IRrecvBase::GetResults and it was true. - * The purpose of GetResults is to determine if a complete set of signals - * has been received. It then copies the raw data into your decoder's rawbuf - * By moving the test for completion and the copying of the buffer - * outside of this "decode" method you can use the individual decode - * methods or make your own custom "decode" without checking for - * protocols you don't use. - * Note: Don't forget to call IRrecvBase::resume(); after decoding is complete. - */ -bool IRdecode::decode(void) { - if (IRdecodeNEC::decode()) return true; - if (IRdecodeSony::decode()) return true; - if (IRdecodeRC5::decode()) return true; - if (IRdecodeRC6::decode()) return true; - if (IRdecodePanasonic_Old::decode()) return true; - if (IRdecodeNECx::decode()) return true; - if (IRdecodeJVC::decode()) return true; -//if (IRdecodeADDITIONAL::decode()) return true;//add additional protocols here -//Deliberately did not add hash code decoding. If you get decode_type==UNKNOWN and -// you want to know a hash code you can call IRhash::decode() yourself. -// BTW This is another reason we separated IRrecv from IRdecode. - return false; -} - -#define NEC_RPT_SPACE 2250 -bool IRdecodeNEC::decode(void) { - IRLIB_ATTEMPT_MESSAGE(F("NEC")); - // Check for repeat - if (rawlen == 4 && MATCH(rawbuf[2], NEC_RPT_SPACE) && - MATCH(rawbuf[3],564)) { - bits = 0; - value = REPEAT; - decode_type = NEC; - return true; - } - if(!decodeGeneric(68, 564*16, 564*8, 0, 564, 564*3, 564)) return false; - decode_type = NEC; - return true; -} - -// According to http://www.hifi-remote.com/johnsfine/DecodeIR.html#Sony8 -// Sony protocol can only be 8, 12, 15, or 20 bits in length. -bool IRdecodeSony::decode(void) { - IRLIB_ATTEMPT_MESSAGE(F("Sony")); - if(rawlen!=2*8+2 && rawlen!=2*12+2 && rawlen!=2*15+2 && rawlen!=2*20+2) return RAW_COUNT_ERROR; - if(!decodeGeneric(0, 600*4, 600, 600*2, 600, 600,0)) return false; - decode_type = SONY; - return true; -} - -/* - * The next several decoders were added by Chris Young. They illustrate some of the special cases - * that can come up when decoding using the generic decoder. - */ - -/* - * A very good source for protocol information is... http://www.hifi-remote.com/johnsfine/DecodeIR.html - * I used that information to understand what they call the "Panasonic old" protocol which is used by - * Scientific Atlanta cable boxes. That website uses a very strange notation called IRP notation. - * For this protocol, the notation was: - * {57.6k,833}<1,-1|1,-3>(4,-4,D:5,F:6,~D:5,~F:6,1,-???)+ - * This indicates that the frequency is 57.6, the base length for the pulse is 833 - * The first part of the section tells you what a "0" is and the second part - * tells you what a "1" is. That means "0" is 833 on, 833 off while an "1" is 833 on - * followed by 833*3=2499 off. The section in parentheses tells you what data gets sent. - * The protocol begins with header consisting of 4*833 on and 4*833 off. The other items - * describe what the remaining data bits are. - * It reads as 5 device bits followed by 6 function bits. You then repeat those bits complemented. - * It concludes with a single "1" bit followed by and an undetermined amount of blank space. - * This makes the entire protocol 5+6+5+6= 22 bits long since we don't encode the stop bit. - * The "+" at the end means you only need to send it once and it can repeat as many times as you want. - */ -bool IRdecodePanasonic_Old::decode(void) { - IRLIB_ATTEMPT_MESSAGE(F("Panasonic_Old")); - if(!decodeGeneric(48,833*4,833*4,0,833,833*3,833)) return false; - /* - * The protocol spec says that the first 11 bits described the device and function. - * The next 11 bits are the same thing only it is the logical Bitwise complement. - * Many protocols have such check features in their definition but our code typically doesn't - * perform these checks. For example NEC's least significant 8 bits are the complement of - * of the next more significant 8 bits. While it's probably not necessary to error check this, - * here is some sample code to show you how. - */ - long S1= (value & 0x0007ff); // 00 0000 0000 0111 1111 1111 //00000 000000 11111 111111 - long S2= (value & 0x3ff800)>> 11; // 11 1111 1111 1000 0000 0000 //11111 111111 00000 000000 - S2= (~S2) & 0x0007ff; - if (S1!=S2) return IRLIB_REJECTION_MESSAGE(F("inverted bit redundancy")); - // Success - decode_type = PANASONIC_OLD; - return true; -} - -bool IRdecodeNECx::decode(void) { - IRLIB_ATTEMPT_MESSAGE(F("NECx")); - if(!decodeGeneric(68,564*8,564*8,0,564,564*3,564)) return false; - decode_type = NECX; - return true; -} - -// JVC does not send any header if there is a repeat. -bool IRdecodeJVC::decode(void) { - IRLIB_ATTEMPT_MESSAGE(F("JVC")); - if(!decodeGeneric(36,525*16,525*8,0,525,525*3,525)) - { - IRLIB_ATTEMPT_MESSAGE(F("JVC Repeat")); - if (rawlen==34) - { - if(!decodeGeneric(0,525,0,0,525,525*3,525)) - {return IRLIB_REJECTION_MESSAGE(F("JVC repeat failed generic"));} - else { - //If this is a repeat code then IRdecodeBase::decode fails to add the most significant bit - if (MATCH(rawbuf[4],(525*3))) - { - value |= 0x8000; - } - else - { - if (!MATCH(rawbuf[4],525)) return DATA_SPACE_ERROR(525); - } - } - bits++; - } - else return RAW_COUNT_ERROR; - } - decode_type =JVC; - return true; -} - -/* - * The remaining protocols from the original IRremote library require special handling - * This routine gets one undecoded level at a time from the raw buffer. - * The RC5/6 decoding is easier if the data is broken into time intervals. - * E.g. if the buffer has MARK for 2 time intervals and SPACE for 1, - * successive calls to getRClevel will return MARK, MARK, SPACE. - * offset and used are updated to keep track of the current position. - * t1 is the time interval for a single bit in microseconds. - * Returns ERROR if the measured time interval is not a multiple of t1. - */ -IRdecodeRC::RCLevel IRdecodeRC::getRClevel(unsigned char *used, const unsigned int t1) { - if (offset >= rawlen) { - // After end of recorded buffer, assume SPACE. - return SPACE; - } - unsigned int width = rawbuf[offset]; - IRdecodeRC::RCLevel val; - if ((offset) % 2) val=MARK; else val=SPACE; - - unsigned char avail; - if (MATCH(width, t1)) { - avail = 1; - } - else if (MATCH(width, 2*t1)) { - avail = 2; - } - else if (MATCH(width, 3*t1)) { - avail = 3; - } - else { - return ERROR; - } - (*used)++; - if (*used >= avail) { - *used = 0; - (offset)++; - } - return val; -} - -#define MIN_RC5_SAMPLES 11 -#define MIN_RC6_SAMPLES 1 - -bool IRdecodeRC5::decode(void) { - IRLIB_ATTEMPT_MESSAGE(F("RC5")); - if (rawlen < MIN_RC5_SAMPLES + 2) return RAW_COUNT_ERROR; - offset = 1; // Skip gap space - data = 0; - used = 0; - // Get start bits - if (getRClevel(&used, RC5_T1) != MARK) return HEADER_MARK_ERROR(RC5_T1); -//Note: Original IRremote library incorrectly assumed second bit was always a "1" -//bit patterns from this decoder are not backward compatible with patterns produced -//by original library. Uncomment the following two lines to maintain backward compatibility. - //if (getRClevel(&used, RC5_T1) != SPACE) return HEADER_SPACE_ERROR(RC5_T1); - //if (getRClevel(&used, RC5_T1) != MARK) return HEADER_MARK_ERROR(RC5_T1); - for (nbits = 0; offset < rawlen; nbits++) { - RCLevel levelA = getRClevel(&used, RC5_T1); - RCLevel levelB = getRClevel(&used, RC5_T1); - if (levelA == SPACE && levelB == MARK) { - // 1 bit - data = (data << 1) | 1; - } - else if (levelA == MARK && levelB == SPACE) { - // zero bit - data <<= 1; - } - else return DATA_MARK_ERROR(RC5_T1); - } - // Success - bits = 13; - value = data; - decode_type = RC5; - return true; -} - -bool IRdecodeRC6::decode(void) { - IRLIB_ATTEMPT_MESSAGE(F("RC6")); - if (rawlen < MIN_RC6_SAMPLES) return RAW_COUNT_ERROR; - // Initial mark - if (!MATCH(rawbuf[1], RC6_HDR_MARK)) return HEADER_MARK_ERROR(RC6_HDR_MARK); - if (!MATCH(rawbuf[2], RC6_HDR_SPACE)) return HEADER_SPACE_ERROR(RC6_HDR_SPACE); - offset=3;//Skip gap and header - data = 0; - used = 0; - // Get start bit (1) - if (getRClevel(&used, RC6_T1) != MARK) return DATA_MARK_ERROR(RC6_T1); - if (getRClevel(&used, RC6_T1) != SPACE) return DATA_SPACE_ERROR(RC6_T1); - for (nbits = 0; offset < rawlen; nbits++) { - RCLevel levelA, levelB; // Next two levels - levelA = getRClevel(&used, RC6_T1); - if (nbits == 3) { - // T bit is double wide; make sure second half matches - if (levelA != getRClevel(&used, RC6_T1)) return TRAILER_BIT_ERROR(RC6_T1); - } - levelB = getRClevel(&used, RC6_T1); - if (nbits == 3) { - // T bit is double wide; make sure second half matches - if (levelB != getRClevel(&used, RC6_T1)) return TRAILER_BIT_ERROR(RC6_T1); - } - if (levelA == MARK && levelB == SPACE) { // reversed compared to RC5 - // 1 bit - data = (data << 1) | 1; - } - else if (levelA == SPACE && levelB == MARK) { - // zero bit - data <<= 1; - } - else { - return DATA_MARK_ERROR(RC6_T1); - } - } - // Success - bits = nbits; - value = data; - decode_type = RC6; - return true; -} - -/* - * This Hash decoder is based on IRhashcode - * Copyright 2010 Ken Shirriff - * For details see http://www.righto.com/2010/01/using-arbitrary-remotes-with-arduino.html - * Use FNV hash algorithm: http://isthe.com/chongo/tech/comp/fnv/#FNV-param - * Converts the raw code values into a 32-bit hash code. - * Hopefully this code is unique for each button. - */ -#define FNV_PRIME_32 16777619 -#define FNV_BASIS_32 2166136261UL -// Compare two tick values, returning 0 if newval is shorter, -// 1 if newval is equal, and 2 if newval is longer -int IRdecodeHash::compare(unsigned int oldval, unsigned int newval) { - if (newval < oldval * .8) return 0; - if (oldval < newval * .8) return 2; - return 1; -} - -bool IRdecodeHash::decode(void) { - hash = FNV_BASIS_32; - for (int i = 1; i+2 < rawlen; i++) { - hash = (hash * FNV_PRIME_32) ^ compare(rawbuf[i], rawbuf[i+2]); - } -//note: does not set decode_type=HASH_CODE nor "value" because you might not want to. - return true; -} - -/* We have created a new receiver base class so that we can use its code to implement - * additional receiver classes in addition to the original IRremote code which used - * 50us interrupt sampling of the input pin. See IRrecvLoop and IRrecvPCI classes - * below. IRrecv is the original receiver class with the 50us sampling. - */ -IRrecvBase::IRrecvBase(unsigned char recvpin) -{ - irparams.recvpin = recvpin; - Init(); -} -void IRrecvBase::Init(void) { - irparams.blinkflag = 0; - Mark_Excess=100; -} - -unsigned char IRrecvBase::getPinNum(void){ - return irparams.recvpin; -} - -/* Any receiver class must implement a GetResults method that will return true when a complete code - * has been received. At a successful end of your GetResults code you should then call IRrecvBase::GetResults - * and it will copy the data from the receiver structures into your decoder. Some receivers - * provide results in rawbuf measured in ticks on some number of microseconds while others - * return results in actual microseconds. If you use ticks then you should pass a multiplier - * value in Time_per_Ticks. - */ -bool IRrecvBase::GetResults(IRdecodeBase *decoder, const unsigned int Time_per_Tick) { - decoder->Reset();//clear out any old values. - decoder->rawlen = irparams.rawlen; -/* Typically IR receivers over-report the length of a mark and under-report the length of a space. - * This routine adjusts for that by subtracting Mark_Excess from recorded marks and - * deleting it from a recorded spaces. The amount of adjustment used to be defined in IRLibMatch.h. - * It is now user adjustable with the old default of 100; - * By copying the the values from irparams to decoder we can call IRrecvBase::resume - * immediately while decoding is still in progress. - */ - for(unsigned char i=0; irawbuf[i]=irparams.rawbuf[i]*Time_per_Tick + ( (i % 2)? -Mark_Excess:Mark_Excess); - } - return true; -} - -void IRrecvBase::enableIRIn(void) { - pinMode(irparams.recvpin, INPUT); - resume(); -} - -void IRrecvBase::resume() { - irparams.rawlen = 0; -} - -/* This receiver uses no interrupts or timers. Other interrupt driven receivers - * allow you to do other things and call GetResults at your leisure to see if perhaps - * a sequence has been received. Typically you would put GetResults in your loop - * and it would return false until the sequence had been received. However because this - * receiver uses no interrupts, it takes control of your program when you call GetResults - * and doesn't let go until it's got something to show you. The advantage is you don't need - * interrupts which would make it easier to use and nonstandard hardware and will allow you to - * use any digital input pin. Timing of this routine is only as accurate as your "micros();" - */ -bool IRrecvLoop::GetResults(IRdecodeBase *decoder) { - bool Finished=false; - byte OldState=HIGH;byte NewState; - unsigned long StartTime, DeltaTime=0, EndTime=0; - StartTime=micros(); - while(irparams.rawlen 10000) { //If it's a very long wait - if((Finished=irparams.rawlen)) break; //finished unless it's the opening gap - } - } - if(Finished) break; - do_Blink(); - irparams.rawbuf[irparams.rawlen++]=DeltaTime; - OldState=NewState;StartTime=EndTime; - }; - IRrecvBase::GetResults(decoder); - return true; -} - -/* This receiver uses the pin change hardware interrupt to detect when your input pin - * changes state. It gives more detailed results than the 50�s interrupts of IRrecv - * and theoretically is more accurate than IRrecvLoop. However because it only detects - * pin changes, it doesn't always know when it's finished. GetResults attempts to detect - * a long gap of space but sometimes the next signal gets there before GetResults notices. - * This means the second set of signals can get messed up unless there is a long gap. - * This receiver is based in part on Arduino firmware for use with AnalysIR IR signal analysis - * software for Windows PCs. Many thanks to the people at http://analysir.com for their - * assistance in developing this section of code. - */ - -IRrecvPCI::IRrecvPCI(unsigned char inum) { - Init(); - switch(intrnum=inum) { -#if defined(__AVR_ATmega32U4__) //Assume Arduino Leonardo - case 0: irparams.recvpin=3; break; - case 1: irparams.recvpin=2; break; - case 2: irparams.recvpin=0; break; - case 3: irparams.recvpin=1; break; - case 4: irparams.recvpin=7; break; -#else //Arduino Uno or Mega - case 0: irparams.recvpin=2; break; - case 1: irparams.recvpin=3; break; - #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)//Mega only - case 2: irparams.recvpin=21; break; - case 3: irparams.recvpin=20; break; - case 4: irparams.recvpin=19; break; - case 5: irparams.recvpin=18; break; - #endif -#endif - //Illegal vaalue to flag you that something is wrong - default: irparams.recvpin=255; - } -} -void IRrecvPCI_Handler(){ - unsigned long volatile ChangeTime=micros(); - unsigned long DeltaTime=ChangeTime-irparams.timer; - switch(irparams.rcvstate) { - case STATE_STOP: return; - case STATE_RUNNING: - do_Blink(); - if (DeltaTime>10000) { - irparams.rcvstate=STATE_STOP; - //Setting gap to 0 is a flag to let you know why we stopped For debugging purposes - //irparams.rawbuf[0]=0; - return; - }; - break; - case STATE_IDLE: - if(digitalRead(irparams.recvpin)) return; else irparams.rcvstate=STATE_RUNNING; - break; - default: - // what of STATE_UNKNOWN, STATE_MARK and STATE_SPACE? - break; - }; - irparams.rawbuf[irparams.rawlen]=DeltaTime; - irparams.timer=ChangeTime; - if(++irparams.rawlen>=RAWBUF) { - irparams.rcvstate=STATE_STOP; - //Setting gap to 1 is a flag to let you know why we stopped For debugging purposes - //irparams.rawbuf[0]=1; - } -} - -void IRrecvPCI::resume(void) { - irparams.rcvstate = STATE_IDLE; - IRrecvBase::resume(); - irparams.timer=micros(); - attachInterrupt(intrnum, IRrecvPCI_Handler, CHANGE); -}; - -bool IRrecvPCI::GetResults(IRdecodeBase *decoder) { - if(irparams.rcvstate==STATE_RUNNING) { - unsigned long ChangeTime=irparams.timer; - if( (micros()-ChangeTime) > 10000) { - irparams.rcvstate=STATE_STOP; - //Setting gap to 2 is a flag to let you know why we stopped For debugging purposes - //irparams.rawbuf[0]=2; - } - } - if (irparams.rcvstate != STATE_STOP) return false; - detachInterrupt(intrnum); - IRrecvBase::GetResults(decoder); - return true; -}; - - - - - -/* - * The remainder of this file is all related to interrupt handling and hardware issues. It has - * nothing to do with IR protocols. You need not understand this is all you're doing is adding - * new protocols or improving the receiving, decoding and sending of protocols. - */ -// Provides ISR -#include -// defines for setting and clearing register bits -#ifndef cbi -#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) -#endif -#ifndef sbi -#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) -#endif -#define CLKFUDGE 5 // fudge factor for clock interrupt overhead -#ifdef F_CPU -#define SYSCLOCK F_CPU // main Arduino clock -#else -#define SYSCLOCK 16000000 // main Arduino clock -#endif -#define PRESCALE 8 // timer clock prescale -#define CLKSPERUSEC (SYSCLOCK/PRESCALE/1000000) // timer clocks per microsecond - -#include - -/* - * This section contains the hardware specific portions of IRrecvBase - */ -/* If your hardware is set up to do both output and input but your particular sketch - * doesn't do any output, this method will ensure that your output pin is low - * and doesn't turn on your IR LED or any output circuit. - */ -void IRrecvBase::No_Output (void) { - pinMode(TIMER_PWM_PIN, OUTPUT); - digitalWrite(TIMER_PWM_PIN, LOW); // When not sending PWM, we want it low -} - -// enable/disable blinking of pin 13 on IR processing -void IRrecvBase::blink13(bool blinkflag) -{ - irparams.blinkflag = blinkflag; - if (blinkflag) - pinMode(BLINKLED, OUTPUT); -} - -//Do the actual blinking off and on -//This is not part of IRrecvBase because it may need to be inside an ISR -//and we cannot pass parameters to them. -void do_Blink(void) { - if (irparams.blinkflag) { - if(irparams.rawlen % 2) { - BLINKLED_ON(); // turn pin 13 LED on - } - else { - BLINKLED_OFF(); // turn pin 13 LED off - } - } -} - -/* - * The original IRrecv which uses 50�s timer driven interrupts to sample input pin. - */ -void IRrecv::resume() { - // initialize state machine variables - irparams.rcvstate = STATE_IDLE; - IRrecvBase::resume(); -} - -void IRrecv::enableIRIn(void) { - IRrecvBase::enableIRIn(); - // setup pulse clock timer interrupt - cli(); - TIMER_CONFIG_NORMAL(); - TIMER_ENABLE_INTR; - TIMER_RESET; - sei(); -} - -bool IRrecv::GetResults(IRdecodeBase *decoder) { - if (irparams.rcvstate != STATE_STOP) return false; - IRrecvBase::GetResults(decoder,USECPERTICK); - return true; -} - -#define _GAP 5000 // Minimum map between transmissions -#define GAP_TICKS (_GAP/USECPERTICK) -/* - * This interrupt service routine is only used by IRrecv and may or may not be used by other - * extensions of the IRrecBase. It is timer driven interrupt code to collect raw data. - * Widths of alternating SPACE, MARK are recorded in rawbuf. Recorded in ticks of 50 microseconds. - * rawlen counts the number of entries recorded so far. First entry is the SPACE between transmissions. - * As soon as a SPACE gets long, ready is set, state switches to IDLE, timing of SPACE continues. - * As soon as first MARK arrives, gap width is recorded, ready is cleared, and new logging starts. - */ -ISR(TIMER_INTR_NAME) -{ - TIMER_RESET; - enum irdata_t {IR_MARK=0, IR_SPACE=1}; - irdata_t irdata = (irdata_t)digitalRead(irparams.recvpin); - irparams.timer++; // One more 50us tick - if (irparams.rawlen >= RAWBUF) { - // Buffer overflow - irparams.rcvstate = STATE_STOP; - } - switch(irparams.rcvstate) { - case STATE_IDLE: // In the middle of a gap - if (irdata == IR_MARK) { - if (irparams.timer < GAP_TICKS) { - // Not big enough to be a gap. - irparams.timer = 0; - } - else { - // gap just ended, record duration and start recording transmission - irparams.rawlen = 0; - irparams.rawbuf[irparams.rawlen++] = irparams.timer; - irparams.timer = 0; - irparams.rcvstate = STATE_MARK; - } - } - break; - case STATE_MARK: // timing MARK - if (irdata == IR_SPACE) { // MARK ended, record time - irparams.rawbuf[irparams.rawlen++] = irparams.timer; - irparams.timer = 0; - irparams.rcvstate = STATE_SPACE; - } - break; - case STATE_SPACE: // timing SPACE - if (irdata == IR_MARK) { // SPACE just ended, record it - irparams.rawbuf[irparams.rawlen++] = irparams.timer; - irparams.timer = 0; - irparams.rcvstate = STATE_MARK; - } - else { // SPACE - if (irparams.timer > GAP_TICKS) { - // big SPACE, indicates gap between codes - // Mark current code as ready for processing - // Switch to STOP - // Don't reset timer; keep counting space width - irparams.rcvstate = STATE_STOP; - } - } - break; - case STATE_STOP: // waiting, measuring gap - if (irdata == IR_MARK) { // reset gap timer - irparams.timer = 0; - } - break; - default: - // what of STATE_UNKNOWN and STATE_RUNNING? - break; - } - do_Blink(); -} - -/* - * The hardware specific portions of IRsendBase - */ -void IRsendBase::enableIROut(unsigned char khz) { -//NOTE: the comments on this routine accompanied the original early version of IRremote library -//which only used TIMER2. The parameters defined in IRLibTimer.h may or may not work this way. - // Enables IR output. The khz value controls the modulation frequency in kilohertz. - // The IR output will be on pin 3 (OC2B). - // This routine is designed for 36-40KHz; if you use it for other values, it's up to you - // to make sure it gives reasonable results. (Watch out for overflow / underflow / rounding.) - // TIMER2 is used in phase-correct PWM mode, with OCR2A controlling the frequency and OCR2B - // controlling the duty cycle. - // There is no prescaling, so the output frequency is 16MHz / (2 * OCR2A) - // To turn the output on and off, we leave the PWM running, but connect and disconnect the output pin. - // A few hours staring at the ATmega documentation and this will all make sense. - // See my Secrets of Arduino PWM at http://www.righto.com/2009/07/secrets-of-arduino-pwm.html for details. - - // Disable the Timer2 Interrupt (which is used for receiving IR) - TIMER_DISABLE_INTR; //Timer2 Overflow Interrupt - pinMode(TIMER_PWM_PIN, OUTPUT); - digitalWrite(TIMER_PWM_PIN, LOW); // When not sending PWM, we want it low - TIMER_CONFIG_KHZ(khz); - } - -IRsendBase::IRsendBase () { - pinMode(TIMER_PWM_PIN, OUTPUT); - digitalWrite(TIMER_PWM_PIN, LOW); // When not sending PWM, we want it low -} - -//The Arduino built in function delayMicroseconds has limits we wish to exceed -//Therefore we have created this alternative -void My_delay_uSecs(unsigned int T) { - if(T){if(T>16000) {delayMicroseconds(T % 1000); delay(T/1000); } else delayMicroseconds(T);}; -} - -void IRsendBase::mark(unsigned int time) { - TIMER_ENABLE_PWM; - My_delay_uSecs(time); - Extent+=time; -} - -void IRsendBase::space(unsigned int time) { - TIMER_DISABLE_PWM; - My_delay_uSecs(time); - Extent+=time; -} - -/* - * Various debugging routines - */ - - -#ifdef IRLIB_TRACE -void IRLIB_ATTEMPT_MESSAGE(const __FlashStringHelper * s) {Serial.print(F("Attempting ")); Serial.print(s); Serial.println(F(" decode:"));}; -void IRLIB_TRACE_MESSAGE(const __FlashStringHelper * s) {Serial.print(F("Executing ")); Serial.println(s);}; -byte IRLIB_REJECTION_MESSAGE(const __FlashStringHelper * s) { Serial.print(F(" Protocol failed because ")); Serial.print(s); Serial.println(F(" wrong.")); return false;}; -byte IRLIB_DATA_ERROR_MESSAGE(const __FlashStringHelper * s, unsigned char index, unsigned int value, unsigned int expected) { - IRLIB_REJECTION_MESSAGE(s); Serial.print(F("Error occurred with rawbuf[")); Serial.print(index,DEC); Serial.print(F("]=")); Serial.print(value,DEC); - Serial.print(F(" expected:")); Serial.println(expected,DEC); return false; -}; -#endif diff --git a/libraries/IRLib/IRLib.h b/libraries/IRLib/IRLib.h deleted file mode 100644 index 0dd05a2..0000000 --- a/libraries/IRLib/IRLib.h +++ /dev/null @@ -1,323 +0,0 @@ -/* IRLib.h from IRLib � an Arduino library for infrared encoding and decoding - * Version 1.4 March 2014 - * Copyright 2014 by Chris Young http://cyborg5.com - * - * This library is a major rewrite of IRemote by Ken Shirriff which was covered by - * GNU LESSER GENERAL PUBLIC LICENSE which as I read it allows me to make modified versions. - * That same license applies to this modified version. See his original copyright below. - * The latest Ken Shirriff code can be found at https://github.com/shirriff/Arduino-IRremote - * My purpose was to reorganize the code to make it easier to add or remove protocols. - * As a result I have separated the act of receiving a set of raw timing codes from the act of decoding them - * by making them separate classes. That way the receiving aspect can be more black box and implementers - * of decoders and senders can just deal with the decoding of protocols. It also allows for alternative - * types of receivers independent of the decoding. This makes porting to different hardware platforms easier. - * Also added provisions to make the classes base classes that could be extended with new protocols - * which would not require recompiling of the original library nor understanding of its detailed contents. - * Some of the changes were made to reduce code size such as unnecessary use of long versus bool. - * Some changes were just my weird programming style. Also extended debugging information added. - */ -/* - * IRremote - * Version 0.1 July, 2009 - * Copyright 2009 Ken Shirriff - * For details, see http://www.righto.com/2009/08/multi-protocol-infrared-remote-library.html http://www.righto.com/ - * - * Interrupt code based on NECIRrcv by Joe Knapp - * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556 - * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/ - */ - -#ifndef IRLib_h -#define IRLib_h -#include - -// The following are compile-time library options. -// If you change them, recompile the library. -// If IRLIB_TRACE is defined, some debugging information about the decode will be printed -// IRLIB_TEST must be defined for the IRtest unittests to work. It will make some -// methods virtual, which will be slightly slower, which is why it is optional. -// #define IRLIB_TRACE -// #define IRLIB_TEST - -// Only used for testing; can remove virtual for shorter code -#ifdef IRLIB_TEST -#define VIRTUAL virtual -#else -#define VIRTUAL -#endif - -#define RAWBUF 100 // Length of raw duration buffer (cannot exceed 255) - -typedef unsigned char IRTYPES; //formerly was an enum -#define UNKNOWN 0 -#define NEC 1 -#define SONY 2 -#define RC5 3 -#define RC6 4 -#define PANASONIC_OLD 5 -#define JVC 6 -#define NECX 7 -//#define ADDITIONAL (number) //make additional protocol 8 and change HASH_CODE to 9 -#define HASH_CODE 8 -#define LAST_PROTOCOL HASH_CODE - -const __FlashStringHelper *Pnames(IRTYPES Type); //Returns a character string that is name of protocol. - -// Base class for decoding raw results -class IRdecodeBase -{ -public: - IRdecodeBase(void); - IRTYPES decode_type; // NEC, SONY, RC5, UNKNOWN etc. - unsigned long value; // Decoded value - unsigned char bits; // Number of bits in decoded value - volatile unsigned int *rawbuf; // Raw intervals in microseconds - unsigned char rawlen; // Number of records in rawbuf. - virtual void Reset(void); // Initializes the decoder - virtual bool decode(void); // This base routine always returns false override with your routine - bool decodeGeneric(unsigned char Raw_Count, unsigned int Head_Mark, unsigned int Head_Space, - unsigned int Mark_One, unsigned int Mark_Zero, unsigned int Space_One, unsigned int Space_Zero); - virtual void DumpResults (void); - void UseExtnBuf(void *P); //Normally uses same rawbuf as IRrecv. Use this to define your own buffer. - void copyBuf (IRdecodeBase *source);//copies rawbuf and rawlen from one decoder to another -protected: - unsigned char offset; // Index into rawbuf used various places -}; - -class IRdecodeHash: public virtual IRdecodeBase -{ -public: - unsigned long hash; - virtual bool decode(void);//made virtual in case you want to substitute your own hash code -protected: - int compare(unsigned int oldval, unsigned int newval);//used by decodeHash -}; - - -class IRdecodeNEC: public virtual IRdecodeBase -{ -public: - virtual bool decode(void); -}; - -class IRdecodeSony: public virtual IRdecodeBase -{ -public: - virtual bool decode(void); -}; - -class IRdecodeRC: public virtual IRdecodeBase -{ -public: - enum RCLevel {MARK, SPACE, ERROR};//used by decoders for RC5/RC6 - // These are called by decode - RCLevel getRClevel(unsigned char *used, const unsigned int t1); -protected: - unsigned char nbits; - unsigned char used; - long data; -}; - -class IRdecodeRC5: public virtual IRdecodeRC -{ -public: - virtual bool decode(void); -}; - -class IRdecodeRC6: public virtual IRdecodeRC -{ -public: - virtual bool decode(void); -}; - -class IRdecodePanasonic_Old: public virtual IRdecodeBase -{ -public: - virtual bool decode(void); -}; - -class IRdecodeJVC: public virtual IRdecodeBase -{ -public: - virtual bool decode(void); -}; - -class IRdecodeNECx: public virtual IRdecodeBase -{ -public: - virtual bool decode(void); -}; - -// main class for decoding all supported protocols -class IRdecode: -public virtual IRdecodeNEC, -public virtual IRdecodeSony, -public virtual IRdecodeRC5, -public virtual IRdecodeRC6, -public virtual IRdecodePanasonic_Old, -public virtual IRdecodeJVC, -public virtual IRdecodeNECx -// , public virtual IRdecodeADDITIONAL //add additional protocols here -{ -public: - virtual bool decode(void); // Calls each decode routine individually -}; - -//Base class for sending signals -class IRsendBase -{ -public: - IRsendBase(); - void sendGeneric(unsigned long data, unsigned char Num_Bits, unsigned int Head_Mark, unsigned int Head_Space, - unsigned int Mark_One, unsigned int Mark_Zero, unsigned int Space_One, unsigned int Space_Zero, - unsigned char kHz, bool Stop_Bits, unsigned long Max_Extent=0); -protected: - void enableIROut(unsigned char khz); - VIRTUAL void mark(unsigned int usec); - VIRTUAL void space(unsigned int usec); - unsigned long Extent; -}; - -class IRsendNEC: public virtual IRsendBase -{ -public: - void send(unsigned long data); -}; - -class IRsendSony: public virtual IRsendBase -{ -public: - void send(unsigned long data, int nbits); -}; - -class IRsendRaw: public virtual IRsendBase -{ -public: - void send(unsigned int buf[], unsigned char len, unsigned char khz); -}; - -class IRsendRC5: public virtual IRsendBase -{ -public: - void send(unsigned long data); -}; - -class IRsendRC6: public virtual IRsendBase -{ -public: - void send(unsigned long data, unsigned char nbits); -}; - -class IRsendPanasonic_Old: public virtual IRsendBase -{ -public: - void send(unsigned long data); -}; - -class IRsendJVC: public virtual IRsendBase -{ -public: - void send(unsigned long data, bool First); -}; - -class IRsendNECx: public virtual IRsendBase -{ -public: - void send(unsigned long data); -}; - -class IRsend: -public virtual IRsendNEC, -public virtual IRsendSony, -public virtual IRsendRaw, -public virtual IRsendRC5, -public virtual IRsendRC6, -public virtual IRsendPanasonic_Old, -public virtual IRsendJVC, -public virtual IRsendNECx -// , public virtual IRsendADDITIONAL //add additional protocols here -{ -public: - void send(IRTYPES Type, unsigned long data, unsigned int data2); -}; - -// Changed this to a base class so it can be extended -class IRrecvBase -{ -public: - IRrecvBase(void) {}; - IRrecvBase(unsigned char recvpin); - void No_Output(void); - void blink13(bool blinkflag); - bool GetResults(IRdecodeBase *decoder, const unsigned int Time_per_Ticks=1); - void enableIRIn(void); - virtual void resume(void); - unsigned char getPinNum(void); - unsigned char Mark_Excess; -protected: - void Init(void); -}; - -/* Original IRrecv class uses 50�s interrupts to sample input. While this is generally - * accurate enough for everyday purposes, it may be difficult to port to other - * hardware unless you know a lot about hardware timers and interrupts. Also - * when trying to analyze unknown protocols, the 50�s granularity may not be sufficient. - * In that case use either the IRrecvLoop or the IRrecvPCI class. - */ -class IRrecv: public IRrecvBase -{ -public: - IRrecv(unsigned char recvpin):IRrecvBase(recvpin){}; - bool GetResults(IRdecodeBase *decoder); - void enableIRIn(void); - void resume(void); -}; -/* This receiver uses no interrupts or timers. Other interrupt driven receivers - * allow you to do other things and call GetResults at your leisure to see if perhaps - * a sequence has been received. Typically you would put GetResults in your loop - * and it would return false until the sequence had been received. However because this - * receiver uses no interrupts, it takes control of your program when you call GetResults - * and doesn't let go until it's got something to show you. The advantage is you don't need - * interrupts which would make it easier to use and nonstandard hardware and will allow you to - * use any digital input pin. Timing of this routine is only as accurate as your "micros();" - */ -class IRrecvLoop: public IRrecvBase -{ -public: - IRrecvLoop(unsigned char recvpin):IRrecvBase(recvpin){}; - bool GetResults(IRdecodeBase *decoder); -}; - -/* This receiver uses the pin change hardware interrupt to detect when your input pin - * changes state. It gives more detailed results than the 50�s interrupts of IRrecv - * and theoretically is more accurate than IRrecvLoop. However because it only detects - * pin changes, it doesn't always know when it's finished. GetResults attempts to detect - * a long gap of space but sometimes the next signal gets there before GetResults notices. - * This means the second set of signals can get messed up unless there is a long gap. - * This receiver is based in part on Arduino firmware for use with AnalysIR IR signal analysis - * software for Windows PCs. Many thanks to the people at http://analysir.com for their - * assistance in developing this section of code. - */ -class IRrecvPCI: public IRrecvBase -{ -public: - //Note this is interrupt number not pin number - IRrecvPCI(unsigned char inum); - bool GetResults(IRdecodeBase *decoder); - void resume(void); -private: - unsigned char intrnum; -}; - - -//Do the actual blinking off and on -//This is not part of IRrecvBase because it may need to be inside an ISR -//and we cannot pass parameters to them. -void do_Blink(void); - -// Some useful constants -// Decoded value for NEC when a repeat code is received -#define REPEAT 0xffffffff - - -#endif //IRLib_h diff --git a/libraries/IRLib/IRLibMatch.h b/libraries/IRLib/IRLibMatch.h deleted file mode 100644 index ddd2cc5..0000000 --- a/libraries/IRLib/IRLibMatch.h +++ /dev/null @@ -1,100 +0,0 @@ -/* IRLibMatch.h from IRLib � an Arduino library for infrared encoding and decoding - * Version 1.4 March 2014 - * Copyright 2014 by Chris Young http://cyborg5.com - * - * This library is a major rewrite of IRemote by Ken Shirriff which was covered by - * GNU LESSER GENERAL PUBLIC LICENSE which as I read it allows me to make modified versions. - * That same license applies to this modified version. See his original copyright below. - * The latest Ken Shirriff code can be found at https://github.com/shirriff/Arduino-IRremote - * My purpose was to reorganize the code to make it easier to add or remove protocols. - * As a result I have separated the act of receiving a set of raw timing codes from the act of decoding them - * by making them separate classes. That way the receiving aspect can be more black box and implementers - * of decoders and senders can just deal with the decoding of protocols. It also allows for alternative - * types of receivers independent of the decoding. This makes porting to different hardware platforms easier. - * Also added provisions to make the classes base classes that could be extended with new protocols - * which would not require recompiling of the original library nor understanding of its detailed contents. - * Some of the changes were made to reduce code size such as unnecessary use of long versus bool. - * Some changes were just my weird programming style. Also extended debugging information added. - */ -/* - * IRremote - * Version 0.1 July, 2009 - * Copyright 2009 Ken Shirriff - * For details, see http://www.righto.com/2009/08/multi-protocol-infrared-remote-library.html http://www.righto.com/ - * - * Interrupt code based on NECIRrcv by Joe Knapp - * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556 - * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/ - */ - -#ifndef IRLibMatch_h -#define IRLibMatch_h - -/* - * Originally all timing comparisons for decoding were based on a percent of the - * target value. However when target values are relatively large, the percent tolerance - * is too much. In some instances an absolute tolerance is needed. In order to maintain - * backward compatibility, the default will be to continue to use percent. If you wish to default - * to an absolute tolerance, you should comment out the line below. - */ -#define IRLIB_USE_PERCENT - -/* - * These are some miscellaneous definitions that are needed by the decoding routines. - * You need not include this file unless you are creating custom decode routines - * which will require these macros and definitions. Even if you include it, you probably - * don't need to be intimately familiar with the internal details. - */ - -#define USECPERTICK 50 // microseconds per clock interrupt tick -#define PERCENT_TOLERANCE 25 // percent tolerance in measurements -#define DEFAULT_ABS_TOLERANCE 75 //absolute tolerance in microseconds - -/* - * These revised MATCH routines allow you to use either percentage were absolute tolerances. - * Use ABS_MATCH for absolute and PERC_MATCH percentages. The original MATCH macro - * is controlled by the IRLIB_USE_PERCENT definition a few lines above. - */ - -#define PERCENT_LOW(us) (unsigned int) (((us)*(1.0 - PERCENT_TOLERANCE/100.))) -#define PERCENT_HIGH(us) (unsigned int) (((us)*(1.0 + PERCENT_TOLERANCE/100.) + 1)) - -#define ABS_MATCH(v,e,t) ((v) >= ((e)-(t)) && (v) <= ((e)+(t))) -#define PERC_MATCH(v,e) ((v) >= PERCENT_LOW(e) && (v) <= PERCENT_HIGH(e)) - -#ifdef IRLIB_USE_PERCENT -#define MATCH(v,e) PERC_MATCH(v,e) -#else -#define MATCH(v,e) ABS_MATCH(v,e,DEFAULT_ABS_TOLERANCE) -#endif - -//The following two routines are no longer necessary because mark/space adjustments are done elsewhere -//These definitions maintain backward compatibility. -#define MATCH_MARK(t,u) MATCH(t,u) -#define MATCH_SPACE(t,u) MATCH(t,u) - -#ifdef IRLIB_TRACE -void IRLIB_ATTEMPT_MESSAGE(const __FlashStringHelper * s); -void IRLIB_TRACE_MESSAGE(const __FlashStringHelper * s); -byte IRLIB_REJECTION_MESSAGE(const __FlashStringHelper * s); -byte IRLIB_DATA_ERROR_MESSAGE(const __FlashStringHelper * s, unsigned char index, unsigned int value, unsigned int expected); -#define RAW_COUNT_ERROR IRLIB_REJECTION_MESSAGE(F("number of raw samples")); -#define HEADER_MARK_ERROR(expected) IRLIB_DATA_ERROR_MESSAGE(F("header mark"),offset,rawbuf[offset],expected); -#define HEADER_SPACE_ERROR(expected) IRLIB_DATA_ERROR_MESSAGE(F("header space"),offset,rawbuf[offset],expected); -#define DATA_MARK_ERROR(expected) IRLIB_DATA_ERROR_MESSAGE(F("data mark"),offset,rawbuf[offset],expected); -#define DATA_SPACE_ERROR(expected) IRLIB_DATA_ERROR_MESSAGE(F("data space"),offset,rawbuf[offset],expected); -#define TRAILER_BIT_ERROR(expected) IRLIB_DATA_ERROR_MESSAGE(F("RC5/RC6 trailer bit length"),offset,rawbuf[offset],expected); -#else -#define IRLIB_ATTEMPT_MESSAGE(s) -#define IRLIB_TRACE_MESSAGE(s) -#define IRLIB_REJECTION_MESSAGE(s) false -#define IRLIB_DATA_ERROR_MESSAGE(s,i,v,e) false -#define RAW_COUNT_ERROR false -#define HEADER_MARK_ERROR(expected) false -#define HEADER_SPACE_ERROR(expected) false -#define DATA_MARK_ERROR(expected) false -#define DATA_SPACE_ERROR(expected) false -#define TRAILER_BIT_ERROR(expected) false -#endif - -#endif //IRLibMatch_h diff --git a/libraries/IRLib/IRLibRData.h b/libraries/IRLib/IRLibRData.h deleted file mode 100644 index 4eae0ff..0000000 --- a/libraries/IRLib/IRLibRData.h +++ /dev/null @@ -1,54 +0,0 @@ -/* IRLibRData.h from IRLib � an Arduino library for infrared encoding and decoding - * Version 1.4 March 2014 - * Copyright 2014 by Chris Young http://cyborg5.com - * - * This library is a major rewrite of IRemote by Ken Shirriff which was covered by - * GNU LESSER GENERAL PUBLIC LICENSE which as I read it allows me to make modified versions. - * That same license applies to this modified version. See his original copyright below. - * The latest Ken Shirriff code can be found at https://github.com/shirriff/Arduino-IRremote - * My purpose was to reorganize the code to make it easier to add or remove protocols. - * As a result I have separated the act of receiving a set of raw timing codes from the act of decoding them - * by making them separate classes. That way the receiving aspect can be more black box and implementers - * of decoders and senders can just deal with the decoding of protocols. It also allows for alternative - * types of receivers independent of the decoding. This makes porting to different hardware platforms easier. - * Also added provisions to make the classes base classes that could be extended with new protocols - * which would not require recompiling of the original library nor understanding of its detailed contents. - * Some of the changes were made to reduce code size such as unnecessary use of long versus bool. - * Some changes were just my weird programming style. Also extended debugging information added. - */ -/* - * IRremote - * Version 0.1 July, 2009 - * Copyright 2009 Ken Shirriff - * For details, see http://www.righto.com/2009/08/multi-protocol-infrared-remote-library.html http://www.righto.com/ - * - * Interrupt code based on NECIRrcv by Joe Knapp - * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556 - * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/ - */ - -#ifndef IRLibRData_h -#define IRLibRData_h - -/* - * The structure contains a variety of variables needed by the receiver routines. - * Typically this data would be part of the IRrecv class however the interrupt service routine - * must have access to it and you cannot pass a parameter to such a routine. The data must be global. - * You need not include this file unless you are creating a custom receiver class or extending - * the provided IRrecv class. - */ - -// receiver states -enum rcvstate_t {STATE_UNKNOWN, STATE_IDLE, STATE_MARK, STATE_SPACE, STATE_STOP, STATE_RUNNING}; -// information for the interrupt handler -typedef struct { - unsigned char recvpin; // pin for IR data from detector - rcvstate_t rcvstate; // state machine - bool blinkflag; // TRUE to enable blinking of pin 13 on IR processing - unsigned long timer; // state timer, counts 50uS ticks.(and other uses) - unsigned int rawbuf[RAWBUF]; // raw data - unsigned char rawlen; // counter of entries in rawbuf -} -irparams_t; -extern volatile irparams_t irparams; -#endif diff --git a/libraries/IRLib/IRLibTimer.h b/libraries/IRLib/IRLibTimer.h deleted file mode 100644 index 9079ae0..0000000 --- a/libraries/IRLib/IRLibTimer.h +++ /dev/null @@ -1,330 +0,0 @@ -/* IRLibTimer.h from IRLib � an Arduino library for infrared encoding and decoding - * Version 1.4 March 2014 - * Copyright 2014 by Chris Young http://cyborg5.com - * - * This library is a major rewrite of IRemote by Ken Shirriff which was covered by - * GNU LESSER GENERAL PUBLIC LICENSE which as I read it allows me to make modified versions. - * That same license applies to this modified version. See his original copyright below. - * The latest Ken Shirriff code can be found at https://github.com/shirriff/Arduino-IRremote - * My purpose was to reorganize the code to make it easier to add or remove protocols. - * As a result I have separated the act of receiving a set of raw timing codes from the act of decoding them - * by making them separate classes. That way the receiving aspect can be more black box and implementers - * of decoders and senders can just deal with the decoding of protocols. It also allows for alternative - * types of receivers independent of the decoding. This makes porting to different hardware platforms easier. - * Also added provisions to make the classes base classes that could be extended with new protocols - * which would not require recompiling of the original library nor understanding of its detailed contents. - * Some of the changes were made to reduce code size such as unnecessary use of long versus bool. - * Some changes were just my weird programming style. Also extended debugging information added. - */ -/* - * IRremote - * Version 0.1 July, 2009 - * Copyright 2009 Ken Shirriff - * For details, see http://www.righto.com/2009/08/multi-protocol-infrared-remote-library.html http://www.righto.com/ - * - * Interrupt code based on NECIRrcv by Joe Knapp - * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556 - * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/ - */ -/* This file defines which timer you wish to use. Different versions of Arduino and related boards - * have different timers available to them. For various reasons you might want to use something other than - * timer 2. Some boards do not have timer 2. This attempts to detect which type of board you are using. - * You need uncomment wish to use on your board. You probably will not need to include this in your - * program unless you want to see which timer is being used and which board has been detected. - * This information came from an alternative fork of the original Ken Shirriff library found here - * https://github.com/TKJElectronics/Arduino-IRremote - */ -#ifndef IRLibTimer_h -#define IRLibTimer_h - -#if defined(ARDUINO) && ARDUINO >= 100 -#include "Arduino.h" -#else -#include "WProgram.h" -#endif - -// Arduino Mega -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) - //#define IR_USE_TIMER1 // tx = pin 11 - #define IR_USE_TIMER2 // tx = pin 9 - //#define IR_USE_TIMER3 // tx = pin 5 - //#define IR_USE_TIMER4 // tx = pin 6 - //#define IR_USE_TIMER5 // tx = pin 46 - -// Teensy 1.0 -#elif defined(__AVR_AT90USB162__) - #define IR_USE_TIMER1 // tx = pin 17 - -// Teensy 2.0 versus Leonardo -// These boards use the same chip but the pinouts are different. -#elif defined(__AVR_ATmega32U4__) -#ifdef CORE_TEENSY - // it's Teensy 2.0 - //#define IR_USE_TIMER1 // tx = pin 14 - //#define IR_USE_TIMER3 // tx = pin 9 - #define IR_USE_TIMER4_HS // tx = pin 10 -#else - // it's probably Leonardo - #define IR_USE_TIMER1 // tx = pin 9 - //#define IR_USE_TIMER3 // tx = pin 5 - //#define IR_USE_TIMER4_HS // tx = pin 13 -#endif - -// Teensy++ 1.0 & 2.0 -#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) - //#define IR_USE_TIMER1 // tx = pin 25 - #define IR_USE_TIMER2 // tx = pin 1 - //#define IR_USE_TIMER3 // tx = pin 16 - -// Sanguino -#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) - //#define IR_USE_TIMER1 // tx = pin 13 - #define IR_USE_TIMER2 // tx = pin 14 - -// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, etc -#else - //#define IR_USE_TIMER1 // tx = pin 9 - #define IR_USE_TIMER2 // tx = pin 3 -#endif - - - - - -// defines for timer2 (8 bits) -#if defined(IR_USE_TIMER2) -#define TIMER_RESET -#define TIMER_ENABLE_PWM (TCCR2A |= _BV(COM2B1)) -#define TIMER_DISABLE_PWM (TCCR2A &= ~(_BV(COM2B1))) -#define TIMER_ENABLE_INTR (TIMSK2 = _BV(OCIE2A)) -#define TIMER_DISABLE_INTR (TIMSK2 = 0) -#define TIMER_INTR_NAME TIMER2_COMPA_vect -#define TIMER_CONFIG_KHZ(val) ({ \ - const uint8_t pwmval = SYSCLOCK / 2000 / (val); \ - TCCR2A = _BV(WGM20); \ - TCCR2B = _BV(WGM22) | _BV(CS20); \ - OCR2A = pwmval; \ - OCR2B = pwmval / 3; \ -}) -#define TIMER_COUNT_TOP (SYSCLOCK * USECPERTICK / 1000000) -#if (TIMER_COUNT_TOP < 256) -#define TIMER_CONFIG_NORMAL() ({ \ - TCCR2A = _BV(WGM21); \ - TCCR2B = _BV(CS20); \ - OCR2A = TIMER_COUNT_TOP; \ - TCNT2 = 0; \ -}) -#else -#define TIMER_CONFIG_NORMAL() ({ \ - TCCR2A = _BV(WGM21); \ - TCCR2B = _BV(CS21); \ - OCR2A = TIMER_COUNT_TOP / 8; \ - TCNT2 = 0; \ -}) -#endif -#if defined(CORE_OC2B_PIN) -#define TIMER_PWM_PIN CORE_OC2B_PIN /* Teensy */ -#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) -#define TIMER_PWM_PIN 9 /* Arduino Mega */ -#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) -#define TIMER_PWM_PIN 14 /* Sanguino */ -#else -#define TIMER_PWM_PIN 3 /* Arduino Duemilanove, Diecimila, LilyPad, etc */ -#endif - - -// defines for timer1 (16 bits) -#elif defined(IR_USE_TIMER1) -#define TIMER_RESET -#define TIMER_ENABLE_PWM (TCCR1A |= _BV(COM1A1)) -#define TIMER_DISABLE_PWM (TCCR1A &= ~(_BV(COM1A1))) -#define TIMER_ENABLE_INTR (TIMSK1 = _BV(OCIE1A)) -#define TIMER_DISABLE_INTR (TIMSK1 = 0) -#define TIMER_INTR_NAME TIMER1_COMPA_vect -#define TIMER_CONFIG_KHZ(val) ({ \ - const uint16_t pwmval = SYSCLOCK / 2000 / (val); \ - TCCR1A = _BV(WGM11); \ - TCCR1B = _BV(WGM13) | _BV(CS10); \ - ICR1 = pwmval; \ - OCR1A = pwmval / 3; \ -}) -#define TIMER_CONFIG_NORMAL() ({ \ - TCCR1A = 0; \ - TCCR1B = _BV(WGM12) | _BV(CS10); \ - OCR1A = SYSCLOCK * USECPERTICK / 1000000; \ - TCNT1 = 0; \ -}) -#if defined(CORE_OC1A_PIN) -#define TIMER_PWM_PIN CORE_OC1A_PIN /* Teensy */ -#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) -#define TIMER_PWM_PIN 11 /* Arduino Mega */ -#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) -#define TIMER_PWM_PIN 13 /* Sanguino */ -#else -#define TIMER_PWM_PIN 9 /* Arduino Duemilanove, Diecimila, LilyPad, etc */ -#endif - - -// defines for timer3 (16 bits) -#elif defined(IR_USE_TIMER3) -#define TIMER_RESET -#define TIMER_ENABLE_PWM (TCCR3A |= _BV(COM3A1)) -#define TIMER_DISABLE_PWM (TCCR3A &= ~(_BV(COM3A1))) -#define TIMER_ENABLE_INTR (TIMSK3 = _BV(OCIE3A)) -#define TIMER_DISABLE_INTR (TIMSK3 = 0) -#define TIMER_INTR_NAME TIMER3_COMPA_vect -#define TIMER_CONFIG_KHZ(val) ({ \ - const uint16_t pwmval = SYSCLOCK / 2000 / (val); \ - TCCR3A = _BV(WGM31); \ - TCCR3B = _BV(WGM33) | _BV(CS30); \ - ICR3 = pwmval; \ - OCR3A = pwmval / 3; \ -}) -#define TIMER_CONFIG_NORMAL() ({ \ - TCCR3A = 0; \ - TCCR3B = _BV(WGM32) | _BV(CS30); \ - OCR3A = SYSCLOCK * USECPERTICK / 1000000; \ - TCNT3 = 0; \ -}) -#if defined(CORE_OC3A_PIN) -#define TIMER_PWM_PIN CORE_OC3A_PIN /* Teensy */ -#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) -#define TIMER_PWM_PIN 5 /* Arduino Mega */ -#elif defined(__AVR_ATmega32U4__) -#define TIMER_PWM_PIN 5 /* Arduino Leonardo note already checked for Teensy */ -#else -#error "Please add OC3A pin number here\n" -#endif - - -// defines for timer4 (10 bits, high speed option) -#elif defined(IR_USE_TIMER4_HS) -#define TIMER_RESET -#define TIMER_ENABLE_PWM (TCCR4A |= _BV(COM4A1)) -#define TIMER_DISABLE_PWM (TCCR4A &= ~(_BV(COM4A1))) -#define TIMER_ENABLE_INTR (TIMSK4 = _BV(TOIE4)) -#define TIMER_DISABLE_INTR (TIMSK4 = 0) -#define TIMER_INTR_NAME TIMER4_OVF_vect -#define TIMER_CONFIG_KHZ(val) ({ \ - const uint16_t pwmval = SYSCLOCK / 2000 / (val); \ - TCCR4A = (1<> 8; \ - OCR4C = pwmval; \ - TC4H = (pwmval / 3) >> 8; \ - OCR4A = (pwmval / 3) & 255; \ -}) -#define TIMER_CONFIG_NORMAL() ({ \ - TCCR4A = 0; \ - TCCR4B = _BV(CS40); \ - TCCR4C = 0; \ - TCCR4D = 0; \ - TCCR4E = 0; \ - TC4H = (SYSCLOCK * USECPERTICK / 1000000) >> 8; \ - OCR4C = (SYSCLOCK * USECPERTICK / 1000000) & 255; \ - TC4H = 0; \ - TCNT4 = 0; \ -}) -#if defined(CORE_OC4A_PIN) -#define TIMER_PWM_PIN CORE_OC4A_PIN /* Teensy */ -#elif defined(__AVR_ATmega32U4__) -#define TIMER_PWM_PIN 13 /*Leonardo*/ -#else -#error "Please add OC4A pin number here\n" -#endif - - -// defines for timer4 (16 bits) -#elif defined(IR_USE_TIMER4) -#define TIMER_RESET -#define TIMER_ENABLE_PWM (TCCR4A |= _BV(COM4A1)) -#define TIMER_DISABLE_PWM (TCCR4A &= ~(_BV(COM4A1))) -#define TIMER_ENABLE_INTR (TIMSK4 = _BV(OCIE4A)) -#define TIMER_DISABLE_INTR (TIMSK4 = 0) -#define TIMER_INTR_NAME TIMER4_COMPA_vect -#define TIMER_CONFIG_KHZ(val) ({ \ - const uint16_t pwmval = SYSCLOCK / 2000 / (val); \ - TCCR4A = _BV(WGM41); \ - TCCR4B = _BV(WGM43) | _BV(CS40); \ - ICR4 = pwmval; \ - OCR4A = pwmval / 3; \ -}) -#define TIMER_CONFIG_NORMAL() ({ \ - TCCR4A = 0; \ - TCCR4B = _BV(WGM42) | _BV(CS40); \ - OCR4A = SYSCLOCK * USECPERTICK / 1000000; \ - TCNT4 = 0; \ -}) -#if defined(CORE_OC4A_PIN) -#define TIMER_PWM_PIN CORE_OC4A_PIN -#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) -#define TIMER_PWM_PIN 6 /* Arduino Mega */ -#else -#error "Please add OC4A pin number here\n" -#endif - - -// defines for timer5 (16 bits) -#elif defined(IR_USE_TIMER5) -#define TIMER_RESET -#define TIMER_ENABLE_PWM (TCCR5A |= _BV(COM5A1)) -#define TIMER_DISABLE_PWM (TCCR5A &= ~(_BV(COM5A1))) -#define TIMER_ENABLE_INTR (TIMSK5 = _BV(OCIE5A)) -#define TIMER_DISABLE_INTR (TIMSK5 = 0) -#define TIMER_INTR_NAME TIMER5_COMPA_vect -#define TIMER_CONFIG_KHZ(val) ({ \ - const uint16_t pwmval = SYSCLOCK / 2000 / (val); \ - TCCR5A = _BV(WGM51); \ - TCCR5B = _BV(WGM53) | _BV(CS50); \ - ICR5 = pwmval; \ - OCR5A = pwmval / 3; \ -}) -#define TIMER_CONFIG_NORMAL() ({ \ - TCCR5A = 0; \ - TCCR5B = _BV(WGM52) | _BV(CS50); \ - OCR5A = SYSCLOCK * USECPERTICK / 1000000; \ - TCNT5 = 0; \ -}) -#if defined(CORE_OC5A_PIN) -#define TIMER_PWM_PIN CORE_OC5A_PIN -#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) -#define TIMER_PWM_PIN 46 /* Arduino Mega */ -#else -#error "Please add OC5A pin number here\n" -#endif - - -#else // unknown timer -#error "Internal code configuration error, no known IR_USE_TIMER# defined\n" -#endif - - -// defines for blinking the LED -#if defined(CORE_LED0_PIN) -#define BLINKLED CORE_LED0_PIN -#define BLINKLED_ON() (digitalWrite(CORE_LED0_PIN, HIGH)) -#define BLINKLED_OFF() (digitalWrite(CORE_LED0_PIN, LOW)) -#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) -#define BLINKLED 13 -#define BLINKLED_ON() (PORTB |= B10000000) -#define BLINKLED_OFF() (PORTB &= B01111111) -#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) -#define BLINKLED 0 -#define BLINKLED_ON() (PORTD |= B00000001) -#define BLINKLED_OFF() (PORTD &= B11111110) -#elif defined(__AVR_ATmega32U4__) && defined(IR_USE_TIMER4_HS) -//Leonardo not teensy. When using Timer4 output is on 13. Therefore disabling blink LED -//You can add an LED elsewhere if you want -#define BLINKLED 1 -#define BLINKLED_ON() (digitalWrite(BLINKLED, HIGH)) -#define BLINKLED_OFF() (digitalWrite(BLINKLED, LOW)) -#else -#define BLINKLED 13 -#define BLINKLED_ON() (PORTB |= B00100000) -#define BLINKLED_OFF() (PORTB &= B11011111) -#endif - -#endif //IRLibTimer_h diff --git a/libraries/IRLib/LICENSE.txt b/libraries/IRLib/LICENSE.txt deleted file mode 100644 index 1a3c3c4..0000000 Binary files a/libraries/IRLib/LICENSE.txt and /dev/null differ diff --git a/libraries/IRLib/README.txt b/libraries/IRLib/README.txt deleted file mode 100644 index ae6b786..0000000 --- a/libraries/IRLib/README.txt +++ /dev/null @@ -1,84 +0,0 @@ -IRLib � an Arduino library for infrared encoding and decoding -Version 1.32 January 2014 -Copyright 2013, 2014 by Chris Young http://cyborg5.com - -This library is a major rewrite of IRemote by Ken Shirriff which was covered -by GNU LESSER GENERAL PUBLIC LICENSE which as I read it allows me to make -modified versions. That same license applies to this modified version. See -his original copyright below. - -The latest Ken Shirriff code can be found at -https://github.com/shirriff/Arduino-IRremote - -My purpose was to reorganize the code to make it easier to add or remove -protocols. As a result I have separated the act of receiving a set of raw timing -codes from the act of decoding them by making them separate classes. That way -the receiving aspect can be more black box and implementers of decoders and -senders can just deal with the decoding of protocols. - -Also added provisions to make the classes base classes that could be extended -with new protocols which would not require recompiling of the original library nor -understanding of its detailed contents. Some of the changes were made to reduce -code size such as unnecessary use of long versus bool. Some changes were just my -weird programming style. Also extended debugging information added. - -IRremote -Version 0.1 July, 2009 -Copyright 2009 Ken Shirriff -For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.htm -http://arcfn.com - -Interrupt code based on NECIRrcv by Joe Knapp -http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556 -Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/ - -**************************************************** -The package contains: -IRLib.cpp Code for the library written in object C++ -IRLib.h Header file which you will include in your sketch -IRLibMatch.h Match macros used internally. Need not include this unless you implement - your own protocols -iRLibTimer.h Attempts to detect type of Arduino board and allows you to modify which - interrupt timer you will use. Defaults to timer 2 as did the original KS - library. Alternate board and timer information based on a fork of the - original KS library. That for can be found here. - https://github.com/TKJElectronics/Arduino-IRremote -IRLibRData.h Moved irparams structure and related data to this header to facilitate - user created extensions to IRrecvBase. - -Note: there is no "IRremoteInt.h" header as in the original library. Those values were - moved elsewhere. - -The examples directory contains: -IRanalyze Dumps detailed information about a recent signal. Useful for analyzing - unknown protocols -IRfreq Reports modulation frequency of IR signal. Requires TSMP58000 IR learner -IRhashdecode Demonstrates hash decoder. -IRrecord Recording incoming signal and play it back when a character is sent - through the serial console. By using the console you no longer need - to wire up a pushbutton to run this code. -IRrecvDump Receives a code, attempts to decode it, produces well formatted - output of the results using the new "dump" method. -IRsendDemo Simplistic demo to send a Sony DVD power signal every time a - character is received from the serial monitor. -IRsendJVC Demonstrates sending a code using JVC protocol which is tricky. -IRservo Demonstrates controlling a servo motor using an IR remote -IRserial_remote Demonstrates a Python application that runs on your PC and sends - serial data to Arduino which in turn sends IR remote signals. -Samsung36 Demonstrates how to expand the library without recompiling it. - Also demonstrates how to handle codes that are longer than 32 bits. -DirecTV Demonstrates additional protocol for DirecTV -GIcable Demonstrates additional protocol for GIcable used by Motorola cable boxes -rcmm Additional protocol Phillips RCMM used by AT&T U-Verse boxes -Note: I did not port any of the other demo sketches although I may add IRTest later. -The manuals directory contains: -IRLibReference.docx Reference manual in Microsoft Word format -IRLibReference.pdf Reference manual in Adobe PDF format -Online version of this manual is also available at: - http://tech.cyborg5.com/irlib/docs/ -**************************************************** -The library handles the following protocols: -NEC, Sony, RC5, RC6, Raw all of which were supported in the KS version. -Additionally added Panasonic_Old, JVC, NECx. -Also added KS hash code routines which he released separately. -Example code included but not in the library: Samsung36, DirecTV, GIcable. diff --git a/libraries/IRLib/examples/DirecTV/DirecTV.ino b/libraries/IRLib/examples/DirecTV/DirecTV.ino deleted file mode 100644 index ff57884..0000000 --- a/libraries/IRLib/examples/DirecTV/DirecTV.ino +++ /dev/null @@ -1,201 +0,0 @@ -/* Example program for from IRLib – an Arduino library for infrared encoding and decoding - * Version 1.4 March 2014 - * Copyright 2014 by Chris Young http://cyborg5.com - * Based on original example sketch for IRremote library - * Version 0.11 September, 2009I know prescription and no - * Copyright 2009 Ken Shirriff - * http://www.righto.com/ - */ -/* - * This example demonstrates how to extend this library to add a new protocol - * without actually modifying or recompiling the library itself. It implements the protocol - * used by DirecTV. This protocol actually comes in six different varieties. - * It uses three different frequencies 38, 40, or 57 kHz. It also uses two different varieties - * lead out times either 9000us or 30000us. The default is 38 kHz and 30000us. - * You can use a different constructor for the other varieties. - * This is a modified version of the IRecord example. - * If you would like to make this a permanent part of your library you could copy the class - * prototypes to IRLib.h and the code itself to IRLib.cpp. Search for the word "ADDITIONAL" - * to see where to add various pieces of code. Also see the Samsung36 example for details. - */ -#include -#include - -class IRdecodeDirecTV: public virtual IRdecodeBase -{ -public: - bool decode(void); - bool Repeat; -}; - -class IRsendDirecTV: public virtual IRsendBase -{ -public: - IRsendDirecTV (int khz= 38,bool LongLeadOut=true); - void send(unsigned long data, bool Repeat); - int Freq; - unsigned int LeadOut; -}; - -#define DIRECTV (LAST_PROTOCOL+1) - -/* - * According to http://www.hifi-remote.com/johnsfine/DecodeIR.html#DirecTV - * The IRP notation for this protocol is: - *{38k,600,msb}<1,-1|1,-2|2,-1|2,-2>(5,(5,-2,D:4,F:8,C:4,1,-50)+) {C=7*(F:2:6)+5*(F:2:4)+3*(F:2:2)+(F:2)} - * Unlike most protocols which use a fixed length mark and a variable length or a variable length - * mark a fixed length space, this protocol varies both the mark and the space. - * The stream is still a series of marks and spaces but the length of either of those - * denotes a one or zero. A length of 1200us=logical 1 and length 600us=logical 0 - * It also makes changes to the length of the header mark to devote repeat codes. - * The first header mark should be 6000us but repeat codes should only be 3000us. - */ -bool IRdecodeDirecTV::decode(void) { - long data; int offset; - IRLIB_ATTEMPT_MESSAGE(F("DirecTV")); - if (rawlen != 20) return RAW_COUNT_ERROR; - if (MATCH(rawbuf[1],3000)) - Repeat=true; - else - if (!MATCH(rawbuf[1],6000)){ - return HEADER_MARK_ERROR(6000); - } else { - Repeat=false; - } - if (!MATCH(rawbuf[2],1200)) return HEADER_SPACE_ERROR(1200); - offset=3; data=0; - while (offset < 18) { - if (MATCH(rawbuf[offset],1200)) { - data = (data << 1) | 1; - } - else if (MATCH(rawbuf[offset],600)) { - data <<= 1; - } - else return DATA_MARK_ERROR(1200); - offset++; - if (MATCH(rawbuf[offset],1200)) { - data = (data << 1) | 1; - } - else if (MATCH (rawbuf[offset],600)) { - data <<= 1; - } - else return DATA_SPACE_ERROR(1200); - offset++; - } - if (!MATCH(rawbuf[1],6000)) return DATA_MARK_ERROR(6000); - bits = 16;//set bit length - value = data;//put remaining bits in value - decode_type= static_castDIRECTV; - return true; -}; - -//See the comment at the top about different frequencies and lead out times -IRsendDirecTV::IRsendDirecTV (int khz, bool LongLeadOut){ - Freq=khz; - if (LongLeadOut) LeadOut=50*600; else LeadOut=15*600; -}; - -void IRsendDirecTV::send(unsigned long data, bool Repeat) { - enableIROut(Freq); - if(Repeat) mark(3000); else mark(6000); - space(1200);//Send header - for (int i = 0; i < 8; i++) { - if (data & 0x8000) mark(1200); else mark(600); - data <<= 1; - if (data & 0x8000) space(1200); else space(600); - data <<= 1; - }; - mark(600); - space(LeadOut); -}; - -//Create a custom class that combines this new protocol with all the others -class MyCustomSend: -public virtual IRsend, -public virtual IRsendDirecTV -{ -public: - void send(IRTYPES Type, unsigned long data, int nbits); -}; -void MyCustomSend::send(IRTYPES Type, unsigned long data, int nbits) { - if (Type==DIRECTV) - IRsendDirecTV::send(data,false); - else - IRsend::send(Type, data, nbits); -} -class MyCustomDecode: -public virtual IRdecode, -public virtual IRdecodeDirecTV -{ -public: - virtual bool decode(void); // Calls each decode routine individually - void DumpResults(void); -}; -bool MyCustomDecode::decode(void) { - if (IRdecodeDirecTV::decode()) return true; - return IRdecode::decode (); -} -void MyCustomDecode::DumpResults(void){ - if(decode_type==DIRECTV) { - Serial.print(F("Decoded DirecTV: Value:")); Serial.print(value, HEX); - if (Repeat) Serial.print(F(" repeat flag")); - }; - IRdecode::DumpResults(); -}; - -MyCustomDecode My_Decoder; - -MyCustomSend My_Sender; - -int RECV_PIN = 11; - -IRrecv My_Receiver(RECV_PIN); -IRTYPES codeType; // The type of code -unsigned long codeValue; // The data bits -int codeBits; // The length of the code in bits -bool GotOne; - -void setup() -{ - GotOne=false; - codeType=UNKNOWN; - codeValue=0; - codeBits=0; - Serial.begin(9600); - Serial.println(F("Send a code from your remote and we will record it.")); - Serial.println(F("Type any character and press enter. We will send the recorded code.")); - My_Receiver.enableIRIn(); // Start the receiver -} -void loop() { - if (Serial.available()>0) { - unsigned char c=Serial.read(); - if (c=='p') {//Send a test pattern - GotOne= true; codeType=DIRECTV; codeValue=0x1234; codeBits=16; - } - if(GotOne) { - My_Sender.send(codeType,codeValue,codeBits); - Serial.print(F("Sent ")); - if (codeType== DIRECTV) Serial.print(F("DirecTV")); else Serial.print(Pnames(codeType)); - Serial.print(F(" Value:0x")); - Serial.print(codeValue, HEX); - Serial.print(F(" Bits:")); - Serial.println(codeBits, DEC); - My_Receiver.enableIRIn(); // Re-enable receiver - } - } - else if (My_Receiver.GetResults(&My_Decoder)) { - My_Decoder.decode(); - if(My_Decoder.decode_type == UNKNOWN) { - Serial.println(F("Unknown type received. Ignoring.")); - } else { - codeType= My_Decoder.decode_type; - codeValue= My_Decoder.value; - codeBits= My_Decoder.bits; - GotOne=true; - } - My_Decoder.DumpResults(); - delay(1000); - My_Receiver.resume(); - } -} - diff --git a/libraries/IRLib/examples/GIcable/GIcable.ino b/libraries/IRLib/examples/GIcable/GIcable.ino deleted file mode 100644 index 72aa858..0000000 --- a/libraries/IRLib/examples/GIcable/GIcable.ino +++ /dev/null @@ -1,152 +0,0 @@ -/* Example program for from IRLib – an Arduino library for infrared encoding and decoding - * Version 1.4 March 2014 - * Copyright 2014 by Chris Young http://cyborg5.com - * Based on original example sketch for IRremote library - * Version 0.11 September, 2009I know prescription and no - * Copyright 2009 Ken Shirriff - * http://www.righto.com/ - */ -/* - * This example demonstrates how to extend this library to add a new protocol - * without actually modifying or recompiling the library itself. It implements the protocol - * known as GIcable which is used by some Motorola cable boxes. - * This is a modified version of the IRecord example. - * If you would like to make this a permanent part of your library you could copy the class - * prototypes to IRLib.h and the code itself to IRLib.cpp. Search for the word "ADDITIONAL" - * to see where to add various pieces of code. Also see the Samsung36 example for details. - */ -#include -#include - -class IRdecodeGIcable: public virtual IRdecodeBase -{ -public: - bool decode(void); -}; -class IRsendGIcable: public virtual IRsendBase -{ -public: - void send(unsigned long data, bool Repeat); -}; - -#define GICABLE (LAST_PROTOCOL+1) - -/* The IRP notation for this protocol according to - * http://www.hifi-remote.com/johnsfine/DecodeIR.html#G.I. Cable - * is "{38.7k,490}<1,-4.5|1,-9>(18,-9,F:8,D:4,C:4,1,-84,(18,-4.5,1,-178)*) {C = -(D + F:4 + F:4:4)}" - */ -bool IRdecodeGIcable::decode(void) { - IRLIB_ATTEMPT_MESSAGE(F("GIcable")); - // Check for repeat - if (rawlen == 4 && MATCH(rawbuf[1], 490*18) && MATCH(rawbuf[2],2205)) { - bits = 0; - value = REPEAT; - decode_type= static_castGICABLE; - return true; - } - if(!decodeGeneric(36, 18*490, 9*490, 0, 490, 9*490, 2205/*(4.5*490)*/)) return false; - decode_type= static_castGICABLE; - return true; -}; - -void IRsendGIcable::send(unsigned long data, bool Repeat) { - if(Repeat) { - enableIROut(39); - mark (490*18); space (2205); mark (490); space(220);delay (87);//actually "space(87200);" - } else { - sendGeneric(data,16, 490*18, 490*9, 490, 490, 490*9, 2205, 39, true); - } -}; - -//Create a custom class that combines this new protocol with all the others -class MyCustomSend: -public virtual IRsend, -public virtual IRsendGIcable -{ -public: - void send(IRTYPES Type, unsigned long data, int nbits); -}; -void MyCustomSend::send(IRTYPES Type, unsigned long data, int nbits) { - if (Type==GICABLE){ - IRsendGIcable::send(data,false); - //un-comment the line below to text repeats - //delay(3000); IRsendGIcable::send(data,true); - } - else - IRsend::send(Type, data, nbits); -} -class MyCustomDecode: -public virtual IRdecode, -public virtual IRdecodeGIcable -{ -public: - virtual bool decode(void); // Calls each decode routine individually - void DumpResults(void); -}; -bool MyCustomDecode::decode(void) { - if (IRdecodeGIcable::decode()) return true; - return IRdecode::decode (); -} -void MyCustomDecode::DumpResults(void){ - if(decode_type==GICABLE) { - Serial.print(F("Decoded GIcable: Value:")); Serial.print(value, HEX); - }; - IRdecode::DumpResults(); -}; - -MyCustomDecode My_Decoder; - -MyCustomSend My_Sender; - -int RECV_PIN =11; - -IRrecv My_Receiver(RECV_PIN); -IRTYPES codeType; // The type of code -unsigned long codeValue; // The data bits -int codeBits; // The length of the code in bits -bool GotOne; - -void setup() -{ - GotOne=false; - codeType=UNKNOWN; - codeValue=0; - codeBits=0; - Serial.begin(9600); - Serial.println(F("Send a code from your remote and we will record it.")); - Serial.println(F("Type any character and press enter. We will send the recorded code.")); - My_Receiver.enableIRIn(); // Start the receiver -} -void loop() { - if (Serial.available()>0) { - unsigned char c=Serial.read(); - if (c=='p') {//Send a test pattern - GotOne= true; codeType=GICABLE; codeValue=0x1234; codeBits=16; - } - if(GotOne) { - My_Sender.send(codeType,codeValue,codeBits); - Serial.print(F("Sent ")); - if (codeType== GICABLE) Serial.print(F("GIcable")); else Serial.print(Pnames(codeType)); - Serial.print(F(" Value:0x")); - Serial.print(codeValue, HEX); - Serial.print(F(" Bits:")); - Serial.println(codeBits, DEC); - My_Receiver.enableIRIn(); // Re-enable receiver - } - } - else if (My_Receiver.GetResults(&My_Decoder)) { - My_Decoder.decode(); - if(My_Decoder.decode_type == UNKNOWN) { - Serial.println(F("Unknown type received. Ignoring.")); - } else { - codeType= My_Decoder.decode_type; - codeValue= My_Decoder.value; - codeBits= My_Decoder.bits; - GotOne=true; - } - My_Decoder.DumpResults(); - delay(1000); - My_Receiver.resume(); - } -} - diff --git a/libraries/IRLib/examples/IRanalyze/IRanalyze.ino b/libraries/IRLib/examples/IRanalyze/IRanalyze.ino deleted file mode 100644 index aec2673..0000000 --- a/libraries/IRLib/examples/IRanalyze/IRanalyze.ino +++ /dev/null @@ -1,117 +0,0 @@ -/* Example program for from IRLib – an Arduino library for infrared encoding and decoding - * Version 1.3 January 2014 - * Copyright 2014 by Chris Young http://cyborg5.com - * Based on original example sketch for IRremote library - * Version 0.11 September, 2009 - * Copyright 2009 Ken Shirriff - * http://www.righto.com/ - */ -/* IRanalyze receives repeated values from a remote and averages the results. Should help in - * analyzing unknown protocols. You have to press the same key repeatedly. If you press a - * different key the totals reset and it computes new averages. - */ -#include -#include - -int RECV_PIN = 11; - -unsigned int Samples,i, LowSpace, LowMark, HighMark, HighSpace, interval, - balm,aalm,bahm,aahm,bals,aals,bahs,aahs; -unsigned char bacm,aacm,bacs,aacs, Mark_Count,Space_Count; -unsigned int Accumulated_Time[RAWBUF]; -unsigned long Mark_Avg, Space_Avg,baam,aaam,baas,aaas; -//Try this program with various receivers -IRrecv My_Receiver(RECV_PIN); -//IRrecvLoop My_Receiver(RECV_PIN); -//Use interrupt=0. This is pin 2 on Arduino Uno and Mega, pin 3 on Leonardo -//IRrecvPCI My_Receiver(0); - -IRdecode My_Decoder; -IRTYPES Old_Type; -unsigned long Old_Value; -void setup() -{ - Serial.begin(9600); - delay(1000);while(!Serial); - My_Receiver.enableIRIn(); -// My_Receiver.Mark_Excess=50;//Try different values here - Samples=0;Old_Value=0; Old_Type=UNKNOWN; - Serial.println(F("Send a signal repeatedly. We will report averages and statistics.")); -} -void Tab(void) {Serial.print("\t");}; -void loop() { - if (My_Receiver.GetResults(&My_Decoder)) { - My_Decoder.decode(); - if( (My_Decoder.decode_type != Old_Type) || (My_Decoder.value != Old_Value)) { - Serial.println(F("Resetting counters")); - for(i=0;i - -int RECV_PIN = 11; - -IRrecv My_Receiver(RECV_PIN); -IRdecode My_Decoder; -IRdecodeHash My_Hash_Decoder; - -void setup() -{ - My_Receiver.enableIRIn(); // Start the receiver - Serial.begin(9600); - delay(2000);while(!Serial);//delay for Leonardo -} - -void loop() { - if (My_Receiver.GetResults(&My_Decoder)) {//Puts results in My_Decoder - My_Hash_Decoder.copyBuf(&My_Decoder);//copy the results to the hash decoder - My_Decoder.decode(); - Serial.print("real decode type:"); - Serial.print(Pnames(My_Decoder.decode_type)); - Serial.print(" value: 0x"); - Serial.print(My_Decoder.value, HEX); - My_Hash_Decoder.decode(); - Serial.print(", hash decode: 0x"); - Serial.println(My_Hash_Decoder.hash, HEX); // Do something interesting with this value - delay(1000); - My_Receiver.resume(); - } -} - diff --git a/libraries/IRLib/examples/IRrecord/IRrecord.ino b/libraries/IRLib/examples/IRrecord/IRrecord.ino deleted file mode 100644 index 28e5e61..0000000 --- a/libraries/IRLib/examples/IRrecord/IRrecord.ino +++ /dev/null @@ -1,130 +0,0 @@ -/* Example program for from IRLib – an Arduino library for infrared encoding and decoding - * Version 1.3 January 2014 - * Copyright 2014 by Chris Young http://cyborg5.com - * Based on original example sketch for IRremote library - * Version 0.11 September, 2009 - * Copyright 2009 Ken Shirriff - * http://www.righto.com/ - */ -/* - * IRrecord: record and play back IR signals - * An IR detector/demodulator must be connected to the input RECV_PIN. - * An IR LED must be connected to the appropriate pin.(See IRLibTimer.h) for your - * machine's timers and erupts. - * Record a value by pointing your remote at the device then send any character - * from the serial monitor to send the recorded value. - * Also demonstrates how to use toggle bits which must be controlled outside - * the library routines. - * The logic is: - * If an IR code is received, record it. - * If g serial character is received, send the IR code. - */ - -#include - -int RECV_PIN = 11; - -IRrecv My_Receiver(RECV_PIN); -IRdecode My_Decoder; -IRsend My_Sender; -/* - * Because this version of the library separated the receiver from the decoder, - * technically you would not need to "store" the code outside the decoder object - * for this overly simple example. All of the details would remain in the object. - * However we are going to go ahead and store them just to show you how. - */ -// Storage for the recorded code -IRTYPES codeType; // The type of code -unsigned long codeValue; // The data bits if type is not raw -int codeBits; // The length of the code in bits - -// These values are only stored if it's an unknown type and we are going to use -// raw codes to resend the information. -unsigned int rawCodes[RAWBUF]; // The durations if raw -int rawCount; //The number of interval samples - -bool GotOne, GotNew; - -void setup() -{ - GotOne=false; GotNew=false; - codeType=UNKNOWN; - codeValue=0; - Serial.begin(9600); - delay(2000);while(!Serial);//delay for Leonardo - Serial.println(F("Send a code from your remote and we will record it.")); - Serial.println(F("Type any character and press enter. We will send the recorded code.")); - My_Receiver.enableIRIn(); // Start the receiver -} - -// Stores the code for later playback -void storeCode(void) { - GotNew=true; - codeType = My_Decoder.decode_type; - if (codeType == UNKNOWN) { - Serial.println("Received unknown code, saving as raw"); - // To store raw codes: - // Drop first value (gap) - // As of v1.3 of IRLib global values are already in microseconds rather than ticks - // They have also been adjusted for overreporting/underreporting of marks and spaces - rawCount = My_Decoder.rawlen-1; - for (int i = 1; i <=rawCount; i++) { - rawCodes[i - 1] = My_Decoder.rawbuf[i]; - }; - My_Decoder.DumpResults(); - codeType=UNKNOWN; - } - else { - Serial.print(F("Received ")); - Serial.print(Pnames(codeType)); - if (My_Decoder.value == REPEAT) { - // Don't record a NEC repeat value as that's useless. - Serial.println(F("repeat; ignoring.")); - } - else { - codeValue = My_Decoder.value; - codeBits = My_Decoder.bits; - } - Serial.print(F(" Value:0x")); - Serial.println(My_Decoder.value, HEX); - } -} -void sendCode(int repeat) { - if(codeType== UNKNOWN) { - // Assume 38 KHz - My_Sender.IRsendRaw::send(rawCodes,rawCount,38); - Serial.println(F("Sent raw")); - return; - } - if( !GotNew ) {//We've already sent this so handle toggle bits - if (codeType == RC5) { - codeValue ^= 0x0800; - } - else if (codeType == RC6) { - codeValue ^= 0x10000; - } - } - GotNew=false; - My_Sender.send(codeType,codeValue,codeBits); - Serial.print(F("Sent ")); - Serial.print(Pnames(codeType)); - Serial.print(F(" Value:0x")); - Serial.println(codeValue, HEX); -} - -void loop() { - if (Serial.read() != -1) { - if(GotOne) { - sendCode(0); - My_Receiver.enableIRIn(); // Re-enable receiver - } - } - else if (My_Receiver.GetResults(&My_Decoder)) { - My_Decoder.decode(); - GotOne=true; - storeCode(); - delay(1000); - My_Receiver.resume(); - } -} - diff --git a/libraries/IRLib/examples/IRrecvDump/IRrecvDump.ino b/libraries/IRLib/examples/IRrecvDump/IRrecvDump.ino deleted file mode 100644 index fb8828c..0000000 --- a/libraries/IRLib/examples/IRrecvDump/IRrecvDump.ino +++ /dev/null @@ -1,40 +0,0 @@ -/* Example program for from IRLib – an Arduino library for infrared encoding and decoding - * Version 1.3 January 2014 - * Copyright 2014 by Chris Young http://cyborg5.com - * Based on original example sketch for IRremote library - * Version 0.11 September, 2009 - * Copyright 2009 Ken Shirriff - * http://www.righto.com/ - */ -/* - * IRLib: IRrecvDump - dump details of IR codes with IRrecv - * An IR detector/demodulator must be connected to the input RECV_PIN. - */ - -#include - -int RECV_PIN = 11; - -IRrecv My_Receiver(RECV_PIN); - -IRdecode My_Decoder; -unsigned int Buffer[RAWBUF]; - -void setup() -{ - Serial.begin(9600); - delay(2000);while(!Serial);//delay for Leonardo - My_Receiver.enableIRIn(); // Start the receiver - My_Decoder.UseExtnBuf(Buffer); -} - -void loop() { - if (My_Receiver.GetResults(&My_Decoder)) { - //Restart the receiver so it can be capturing another code - //while we are working on decoding this one. - My_Receiver.resume(); - My_Decoder.decode(); - My_Decoder.DumpResults(); - } -} - diff --git a/libraries/IRLib/examples/IRsendDemo/IRsendDemo.ino b/libraries/IRLib/examples/IRsendDemo/IRsendDemo.ino deleted file mode 100644 index b61217e..0000000 --- a/libraries/IRLib/examples/IRsendDemo/IRsendDemo.ino +++ /dev/null @@ -1,25 +0,0 @@ -/* Example program for from IRLib – an Arduino library for infrared encoding and decoding - * Version 1.3 January 2014 - * Copyright 2014 by Chris Young http://cyborg5.com - * Based on original example sketch for IRremote library - * Version 0.11 September, 2009 - * Copyright 2009 Ken Shirriff - * http://www.righto.com/ - */ -#include - -IRsend My_Sender; - -void setup() -{ - Serial.begin(9600); -} - -void loop() { - if (Serial.read() != -1) { - //send a code every time a character is received from the serial port - //Sony DVD power A8BCA - My_Sender.send(SONY,0xa8bca, 20); - } -} - diff --git a/libraries/IRLib/examples/IRsendJVC/IRsendJVC.ino b/libraries/IRLib/examples/IRsendJVC/IRsendJVC.ino deleted file mode 100644 index a49f0a2..0000000 --- a/libraries/IRLib/examples/IRsendJVC/IRsendJVC.ino +++ /dev/null @@ -1,37 +0,0 @@ -/* Example program for from IRLib – an Arduino library for infrared encoding and decoding - * Version 1.3 January 2014 - * Copyright 2014 by Chris Young http://cyborg5.com - * Based on original example sketch for IRremote library - * Version 0.11 September, 2009 - * Copyright 2009 Ken Shirriff - * http://www.righto.com/ - */ -/* - * JVC sends repeat codes that are identical to the regular JVC codes - * however they have no header. Therefore there is an additional parameter - * that tells you whether or not to send as an original code or as a repeat. - * - * The only device I had to test this protocol was an old JVC VCR. It would only work if at least - * 2 frames are sent separated by 45us of "space". All JVC is the same bit length so we use - * the third parameter as a flag to tell it whether or not to send the header. - * Send once with the third parameter "1" then delay about 50 microseconds and send again - * with the third parameter "0". - */ - -#include - -IRsend My_Sender; - -void setup() -{ - Serial.begin(9600); -} - -//send a code every time a character is received from the serial port -void loop() { - if (Serial.read() != -1) { - My_Sender.send(JVC,0xc2d0,1); delayMicroseconds (50); - My_Sender.send(JVC,0xc2d0,0); delayMicroseconds (50); - } -} - diff --git a/libraries/IRLib/examples/IRserial_remote/IRserial_remote.ino b/libraries/IRLib/examples/IRserial_remote/IRserial_remote.ino deleted file mode 100644 index f710949..0000000 --- a/libraries/IRLib/examples/IRserial_remote/IRserial_remote.ino +++ /dev/null @@ -1,55 +0,0 @@ -/* Example program for from IRLib – an Arduino library for infrared encoding and decoding - * Version 1.3 January 2014 - * Copyright 2014 by Chris Young http://cyborg5.com - * Use serial port to send codes. For details on this project see - http://tech.cyborg5.com/2013/05/30/irlib-tutorial-part-3-overview-sending-ir-codes/ - */ -#include - -IRsend My_Sender; - -int protocol; -long code; -int bits; -void setup() { - Serial.begin(9600); - delay(2000);while(!Serial);//delay for Leonardo -} - -long parseHex (void) { - long Value=0; char C;delay(100); - while (Serial.available()>0) { - C= tolower(Serial.read()); - if ((C>='0')&&(C<='9')) - C=C-'0'; - else - if ((C>='a') && (C<='f')) - C=C-'a'+10; - else - return Value; - Value= C+(Value<<4); - }; - return Value; -} -void parseDelimiter () { - char C; - while(Serial.available()>0) { - C=tolower(Serial.peek()); - if( (C>='0') && (C<='9') )return; - if( (C>='a') && (C<='f') )return; - C=Serial.read();//throwaway delimiters - delay (5); - } -} - -void loop() { - if (Serial.available ()>0) { - protocol = Serial.parseInt (); parseDelimiter(); - code = parseHex (); parseDelimiter(); - bits = Serial.parseInt (); parseDelimiter(); - Serial.print("Prot:"); Serial.print(protocol); - Serial.print(" Code:"); Serial.print(code,HEX); - Serial.print(" Bits:"); Serial.println(bits); - My_Sender.send(IRTYPES(protocol), code, bits); - } -} diff --git a/libraries/IRLib/examples/IRserial_remote/IRserial_remote.py b/libraries/IRLib/examples/IRserial_remote/IRserial_remote.py deleted file mode 100644 index 33cb042..0000000 --- a/libraries/IRLib/examples/IRserial_remote/IRserial_remote.py +++ /dev/null @@ -1,133 +0,0 @@ -# IRLib demo script -# version 1.0 by Chris Young http://tech.cyborg5.com/irlib/ -# Displays a "Virtual remote" on your screen. Clicking on the -# buttons sends serial datato the Arduino which in turn -# since IR signals to a cable box in TV. - -# Import all of the necessary pieces of code -import serial, sys, pygame, pygame.mixer -from pygame.locals import * - -# You will have to edit this to the proper port and speed -ser = serial.Serial('COM4', 9600) - -pygame.init() -# Established screen size, size of buttons and position -size = width, height = 400, 768 -button_size=54; button_offset=71 -button_x1=65;button_y1=39 -max_rows=10; max_columns=4 - -# Specify a font. I'm using Arial narrow bold from my Windows -# font folder. However the default font shown below also works. -myfont =pygame.font.Font ("c:/windows/fonts/ARIALNB.TTF",30) -#myfont=pygame.font.Font(None,36) - -# These are the text labels that will appear on each button -label_text=("TVp", "CBp", "P^", "Pv",\ - "<<", ">", ">>", "->",\ - "Rec", "=", "s", "<-",\ - "Gd", "^", "Fav", "Inf",\ - - "<", "sel", ">", "Lis",\ - "ret", "v", "Prv", "Mnu",\ - "1", "2", "3", "Ch+",\ - - "4", "5", "6", "Ch-",\ - "7", "8", "9", "Vol+",\ - "Pip", "0", "Mut", "Vol-",\ - ) -# Each of these 40 strings of text correspond to the -# protocol in code which will be sent over the USB serial -# to the Arduino. The first number is the protocol number. -# See the defined protocols in "IRLib.h"for the -# enum IRTYPES at about line 50. This example uses -# protocol 3 which is "RC5" used by my Magnavox TV -# and protocol 5 "PANASONIC_OLD" used by my Scientific -# Atlantic SA 8300 DVR. The protocol number is followed by -# the hex code to be transmitted. That is followed by the -# number of bits. Note that the PANASONIC_OLD protocol -# does not need the number of bits specified so they are omitted. -IR_Codes= ("3,180c,13","5,37c107","5,36d924","5,37d904",\ - "5,37291a","5,37990c","5,36293a","5,36b129",\ - "5,375914","5,374117","5,365934","5,37c906",\ - "5,36c127","5,36812f","5,37f101","5,36213b",\ - - "5,37810f","5,366133","5,364137","5,36c926",\ - "5,366932","5,37a10b","5,36e123","5,373918",\ - "5,36113d","5,37111d","5,36912d","5,377111",\ - - "5,37910d","5,365135","5,375115","5,36f121",\ - "5,36d125","5,37d105","5,363139","3,1810,13",\ - "5,37b908","5,373119","3,180d,13","3,1811,13",\ - ) -# This function gets called to shut everything down -def Finished(): - pygame.quit() - sys.exit() - -# Gets the button index based on mouse position. Returned -# value is from 0 to 39 (number of buttons-1) -# Returns -1 if you are not over a button. -def ComputeButton(): - mx,my=pygame.mouse.get_pos() - mx=mx-button_x1 - my=my-button_y1 - bx=mx/button_offset; by=my/button_offset - if bx<0 or bx>=max_columns:return -1 - if by<0 or by> max_rows:return -1 - if (mx%button_offset)>button_size:return -1 - if (my%button_offset)>button_size:return -1 - return bx+by*max_columns - -# Blits the button text from button number "i" -# onto the specified layer using the specified color. -def Show_Text(i,Layer,color=(0,0,0)): - t=label_text[i] - label = myfont.render (t,1,color) - labelpos= label.get_rect() - labelpos.centerx=button_x1+button_size/2+i%max_columns*button_offset - labelpos.centery=button_y1+button_size/2+i/max_columns*button_offset - Layer.blit(label,labelpos) - -# Create the screen and load the background image. -screen = pygame.display.set_mode(size) -bg = pygame.image.load("remotebg.png") - -# Blit black text labels onto the background image -for i in range (max_rows*max_columns): - Show_Text(i, bg) -# Copy the background to the display -screen.blit(bg,(0,0)) -pygame.display.flip() - -# Load the clicking sound -Click=pygame.mixer.Sound("click.wav") - -# Used to detect when the mouse hovers over a different button -previous=-1 - -while 1: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - Finished() - elif event.type == KEYDOWN and event.key == K_ESCAPE: - Finished () - elif event.type == MOUSEBUTTONDOWN: - i=ComputeButton() #which button did we click - if i>=0: - Click.play() #play the sound - ser.write(IR_Codes[i]) #send the codes - elif event.type==MOUSEMOTION: - i=ComputeButton() #which button are we over - if i!=previous: #difference in the last one? - if i>=0: #turn it red - Show_Text(i,screen,(255,0,0)) - else: #or put it back the way it was - screen.blit(bg,(0,0)) - previous=i - pygame.display.flip() #update the display -# That's all folks - - - diff --git a/libraries/IRLib/examples/IRserial_remote/POV-Ray/remotebg.pov b/libraries/IRLib/examples/IRserial_remote/POV-Ray/remotebg.pov deleted file mode 100644 index d964b2f..0000000 --- a/libraries/IRLib/examples/IRserial_remote/POV-Ray/remotebg.pov +++ /dev/null @@ -1,62 +0,0 @@ -//POV-Ray source to generate the background image for IRserial_remote -// create rectangular areas with rounded corners for use as -// buttons and background objects. - -// Render at 1024x768 then crop 312 pixels from each side -// leaving 400x768 final image. -#declare Area=15; //size of area lights -#declare CR=0.1; //corner radius -#declare ER= 0.5; //edge radius -#declare CX= 3; //width from corner to corner -#declare CY= 7.75; //height from corner to corner -#declare BZ=-ER; //Z offset for buttons - -plane {z,0 pigment{rgb<0.8,0.85,1>*0.8}}//background - -#macro Thing (ER,CR,CX,CY,T) - #local Corner= - union { - torus {CR,ER rotate x*90} - cylinder {ER*z,-ER*z,CR} - } - union { - object{Corner translate< CX,CY,0>} - object{Corner translate<-CX,CY,0>} - object{Corner translate< CX,-CY,0>} - object{Corner translate<-CX,-CY,0>} - cylinder{CY*y,-CY*y,ER translate<-CX-CR,0,0>} - cylinder{CY*y,-CY*y,ER translate< CX+CR,0,0>} - cylinder{CX*x,-CX*x,ER translate<0,-CY-CR,0>} - cylinder{CX*x,-CX*x,ER translate<0, CY+CR,0>} - box{<-CX,-CY-CR,-ER>} - box{<-CX-CR,-CY,-ER>} - texture {T} - } -#end - -#declare BX= 0.4; #declare BY=BX;//size of the buttons -#declare White_Texture=texture{pigment{rgb 1}finish {ambient 0.3}} -#declare Blue_Texture=texture{pigment {rgb<0.85,0.9 ,1>}} - -object {Thing(ER,CR,CX,CY, White_Texture)}//main object -//loop through the buttons -#declare R=-4.5; -#while (R<5.5) - #declare C=-1.5; - #while (C<=1.5) - object{Thing(0.1,0.2,(BX*0.8),(BY*0.8), Blue_Texture) - translate - } - #declare C=C+1; - #end - #declare R=R+1; -#end - - -light_source{<50,50,-100>*5 color 0.8 - #if (Area)area_light x*Area,y*Area,9,9#end -} -light_source{<0,0,-400>*3 rgb 1} - -camera{orthographic location <0,0,-120> look_at <0,0,0> angle 11 } -//That's all folks! \ No newline at end of file diff --git a/libraries/IRLib/examples/IRserial_remote/click.wav b/libraries/IRLib/examples/IRserial_remote/click.wav deleted file mode 100644 index dbc4c22..0000000 Binary files a/libraries/IRLib/examples/IRserial_remote/click.wav and /dev/null differ diff --git a/libraries/IRLib/examples/IRserial_remote/remotebg.png b/libraries/IRLib/examples/IRserial_remote/remotebg.png deleted file mode 100644 index 28bc828..0000000 Binary files a/libraries/IRLib/examples/IRserial_remote/remotebg.png and /dev/null differ diff --git a/libraries/IRLib/examples/IRservo/IRservo.ino b/libraries/IRLib/examples/IRservo/IRservo.ino deleted file mode 100644 index 81d78d7..0000000 --- a/libraries/IRLib/examples/IRservo/IRservo.ino +++ /dev/null @@ -1,75 +0,0 @@ -/* Example program for from IRLib – an Arduino library for infrared encoding and decoding - * Version 1.3 January 2014 - * Copyright 2014 by Chris Young http://cyborg5.com - * "IRservo" Control a servo using an IR remote - */ -#include -#include -/* Note: Servo library uses TIMER1. The default timer for IRLib on Arduino Uno - * is TIMER2 so there is no conflict. However a default timer on Arduino Leonardo - * is TIMER1 so you will have to modify the timer used to use TIMER3 or TIMER4 - * as specified in IRLibTimer.h. Also you will need to modify the input being used. - */ -// You will have to set these values depending on the protocol -// and remote codes that you are using. These are from my Sony DVD/VCR -#define MY_PROTOCOL SONY -#define RIGHT_ARROW 0x86bca //Move several clockwise -#define LEFT_ARROW 0x46bca //Move servo counterclockwise -#define SELECT_BUTTON 0xd0bca //Center the servo -#define UP_ARROW 0x42bca //Increased number of degrees servo moves -#define DOWN_ARROW 0xc2bca //Decrease number of degrees servo moves -#define BUTTON_0 0x90bca //Pushing buttons 0-9 moves to fix positions -#define BUTTON_1 0x00bca // each 20 degrees greater -#define BUTTON_2 0x80bca -#define BUTTON_3 0x40bca -#define BUTTON_4 0xc0bca -#define BUTTON_5 0x20bca -#define BUTTON_6 0xa0bca -#define BUTTON_7 0x60bca -#define BUTTON_8 0xe0bca -#define BUTTON_9 0x10bca - -IRrecv My_Receiver(11);//Receive on pin 11 -IRdecode My_Decoder; -Servo My_Servo; // create servo object to control a servo -int pos; // variable to store the servo position -int Speed; // Number of degrees to move each time a left/right button is pressed - -void setup() -{ - My_Receiver.No_Output();//Turn off any unused IR LED output circuit - My_Servo.attach(9); // attaches the servo on pin 9 to the servo object - pos = 90; // start at midpoint 90 degrees - Speed = 3; // servo moves 3 degrees each time left/right is pushed - My_Servo.write(pos); // Set initial position - My_Receiver.enableIRIn(); // Start the receiver -} - -void loop() -{ - if (My_Receiver.GetResults(&My_Decoder)) { - My_Decoder.decode(); - if(My_Decoder.decode_type==MY_PROTOCOL) { - switch(My_Decoder.value) { - case LEFT_ARROW: pos=min(180,pos+Speed); break; - case RIGHT_ARROW: pos=max(0,pos-Speed); break; - case SELECT_BUTTON: pos=90; break; - case UP_ARROW: Speed=min(10, Speed+1); break; - case DOWN_ARROW: Speed=max(1, Speed-1); break; - case BUTTON_0: pos=0*20; break; - case BUTTON_1: pos=1*20; break; - case BUTTON_2: pos=2*20; break; - case BUTTON_3: pos=3*20; break; - case BUTTON_4: pos=4*20; break; - case BUTTON_5: pos=5*20; break; - case BUTTON_6: pos=6*20; break; - case BUTTON_7: pos=7*20; break; - case BUTTON_8: pos=8*20; break; - case BUTTON_9: pos=9*20; break; - } - My_Servo.write(pos); // tell servo to go to position in variable 'pos' - } - My_Receiver.resume(); - } -} - diff --git a/libraries/IRLib/examples/Samsung36/Samsung36.ino b/libraries/IRLib/examples/Samsung36/Samsung36.ino deleted file mode 100644 index b4865d5..0000000 --- a/libraries/IRLib/examples/Samsung36/Samsung36.ino +++ /dev/null @@ -1,258 +0,0 @@ -/* Example program for from IRLib – an Arduino library for infrared encoding and decoding - * Version 1.4 March 2014 - * Copyright 2014 by Chris Young http://cyborg5.com - * Based on original example sketch for IRremote library - * Version 0.11 September, 2009I know prescription and no - * Copyright 2009 Ken Shirriff - * http://www.righto.com/ - */ -/* - * This example demonstrates how to extend this library to add a new protocol - * without actually modifying or recompiling the library itself. It implements a 36 bit - * Samsung protocol that is used on a Blu-ray player that I own. - * Because a 36 bit value will not fit in the value field (only 32 bits) we have to create - * a second value field. This is a modified version of the IRecord example. - */ -#include -#include -/* Although it is not necessary for this example to run, everything in this section - * this section of the code could be moved to the actual IRLib.h file - */ -class IRdecodeSamsung36: public virtual IRdecodeBase -{ -public: - bool decode(void); - unsigned int value2; - void Reset(void); -private: - bool GetBit(void); - int offset; - unsigned long data; -}; - -class IRsendSamsung36: public virtual IRsendBase -{ -public: - void send(unsigned long data, unsigned long data2); -private: - void PutBits (unsigned long data, int nbits); -}; -/* If moving this code to IRLib.h instead of the line below at about - line 60 you should search for the line that says... -#define ADDITIONAL (number) - and change the code to read as follows‘ -#define NECX 7 -#define SAMSUNG36 8 -#define HASH_CODE 9 -#define LAST_PROTOCOL HASH_CODE - Also at approximately line 160 look for the line that says... - , public virtual IRdecodeADDITIONAL - and change it to read... -public virtual IRdecodeNECx, //Be sure to add the comma -public virtual IRdecodeSamsung36 //No comma here - And at about line 238 change to read... -public virtual IRsendNECx, //Be sure to add the comma -public virtual IRsendSamsung36 //No comma here - Which will add this protocol to the master send and decode classes -*/ -#define SAMSUNG36 (LAST_PROTOCOL+1) - -/* Although it is not necessary for this example to run, everything in this section - * this section of the code could be moved to the actual IRLib.cpp file - */ -/* - * According to http://www.hifi-remote.com/johnsfine/DecodeIR.html#Samsung36 - * The IRP notation for this protocol is: - * {38k,500}<1,-1|1,-3>(9,-9,D:8,S:8,1,-9,E:4,F:8,-68u,~F:8,1,-118)+ - * This means it uses 38k frequency. Base timing is multiples of 500. - * A "0" is mark(500) space(500). A "1" is mark (500) space(1500) - * The header is mark(4500) space(4500). - * The header is followed by 16 bits (8 device, 8 sub device) - * This is followed by a mark(500) space(4500). - * This is followed by 12 more bits (4+8) - * This is followed by 68us ofspace. Followed by eight more bits - * and a final stop bit. -*/ -/* Because not all of the data bits are contiguous in the stream - * we created this little routine to get one data bit. - * We then call it in a loop as needed. - */ -bool IRdecodeSamsung36::GetBit(void) { - if (!MATCH(rawbuf[offset],500)) return DATA_MARK_ERROR(500); - offset++; - if (MATCH(rawbuf[offset],1500)) - data = (data << 1) | 1; - else if (MATCH(rawbuf[offset],500)) - data <<= 1; - else return DATA_SPACE_ERROR(1500); - offset++; - return true; -}; -bool IRdecodeSamsung36::decode(void) { - IRLIB_ATTEMPT_MESSAGE(F("Samsung36")); - if (rawlen != 78) return RAW_COUNT_ERROR; - if (!MATCH(rawbuf[1],4500)) return HEADER_MARK_ERROR(4500); - if (!MATCH(rawbuf[2],4500)) return HEADER_SPACE_ERROR(4500); - offset=3; data=0; - //Get first 16 bits - while (offset < 16*2+2) if(!GetBit()) return false; - //Skip middle header - if (!MATCH(rawbuf[offset],500)) return DATA_MARK_ERROR(500); - offset++; - if (!MATCH(rawbuf[offset],4500)) return DATA_SPACE_ERROR(4400); - //save first 16 bits in "value2" and reset data - offset++; value2=data; data=0; - //12 bits into this second segment there is a 68us space - //so we find one of the raw values to eliminate that - rawbuf[62]=rawbuf[62]-68; - //this gets remaining 20 bits - while(offset<77)if(!GetBit()) return false; - bits =36;//set bit length - value = data;//put remaining 12 bits in value - decode_type= static_castSAMSUNG36; - return true; -}; -//The Reset method clears out the data however we have extra data to clear -void IRdecodeSamsung36::Reset(void) { - IRdecodeBase::Reset();//respect your parents - value2=0; -}; -//Similar to GetBits, this routine sends just one bit of data. -//Need this because data bits are not contiguous in the stream -void IRsendSamsung36::PutBits (unsigned long data, int nbits){ - for (int i = 0; i < nbits; i++) { - if (data & 0x80000000) { - mark(500); space(1500); - } else { - mark(500); space(500); - }; - data <<= 1; - } -} - -void IRsendSamsung36::send(unsigned long data, unsigned long data2) { - data <<= 32-20; - data2 <<= 32-16; - enableIROut(38); - mark(4500); space(4500);//Send header - PutBits (data2, 16);//Send data2 16 bits - mark (500); space (4500);//Send break - PutBits (data, 12);//Send 12 bits - space(68);//Send tiny break - data <<= 12; - PutBits (data, 8);mark(500); //Final eight bits and one stop bit - space(118*500);//Lead out is 118 times the base time 500 -}; -/* - * This concludes the portion that you could move to IRLib.cpp - * Additionally at apprx. line 39 add F("Samsung36"), to the list - * between NECx and hash code. - * Also add "case SAMSUNG36:IRsendSamsung36::send(data,nbits); break; - * at about line 238. Also at about 400 add - * if (IRdecodeSamsung36::decode()) return true; - */ - -/* - * The remaining code is unique to the example sketch. - * We will create a custom class which is a combination - * of all other protocols and our new protocol. - */ - -class MyCustomSend: -public virtual IRsend, -public virtual IRsendSamsung36 -{ -public: -//Note: data2 is the number of bits for non-Samsung36 protocols - void send(IRTYPES Type, unsigned long data, int data2); -}; -void MyCustomSend::send(IRTYPES Type, unsigned long data, int data2) { - if (Type==SAMSUNG36) - IRsendSamsung36::send(data,data2); - else - IRsend::send(Type, data, data2); -} -class MyCustomDecode: -public virtual IRdecode, -public virtual IRdecodeSamsung36 -{ -public: - virtual bool decode(void); // Calls each decode routine individually - void DumpResults(void); -}; -bool MyCustomDecode::decode(void) { - if (IRdecodeSamsung36::decode()) return true; - return IRdecode::decode (); -} -void MyCustomDecode::DumpResults(void){ - if(decode_type==SAMSUNG36) { - Serial.print(F("Decoded Samsung36: Value:")); Serial.print(value, HEX); - Serial.print(F(": Value2:")); Serial.print(value2, HEX); - }; - IRdecode::DumpResults(); -}; - -MyCustomDecode My_Decoder; - -MyCustomSend My_Sender; - -int RECV_PIN = 11; - -IRrecv My_Receiver(RECV_PIN); -IRTYPES codeType; // The type of code -unsigned long codeValue; // The data bits -int codeBits; // The length of the code in bits or for Samsung is storage for data2 -bool GotOne; - -void setup() -{ - GotOne=false; - codeType=UNKNOWN; - codeValue=0; - codeBits=0;//Stores data2 on Samsung36. Stores number of bits on another protocols - Serial.begin(9600); - delay(2000); while(!Serial); - Serial.println(F("Send a code from your remote and we will record it.")); - Serial.println(F("Type any character and press enter. We will send the recorded code.")); - My_Receiver.enableIRIn(); // Start the receiver -} -void loop() { - if (Serial.available()>0) { - unsigned char c=Serial.read(); - if (c=='p') {//Send a test pattern - GotOne= true; codeType=SAMSUNG36; codeValue=0x12345; codeBits=0x6789; - } - if(GotOne) { - My_Sender.send(codeType,codeValue,codeBits); - Serial.print(F("Sent ")); - if (codeType==SAMSUNG36) { - Serial.print(F("Samsung36 Value:0x")); - Serial.print(codeValue, HEX); - Serial.print(F(" Value2:0x")); - Serial.println(codeBits, HEX); - } else { - Serial.print(Pnames(codeType)); - Serial.print(F(" Value:0x")); - Serial.print(codeValue, HEX); - Serial.print(F(" Bits:")); - Serial.println(codeBits, DEC); - }; - My_Receiver.enableIRIn(); // Re-enable receiver - } - } - else if (My_Receiver.GetResults(&My_Decoder)) { - My_Decoder.decode(); - if(My_Decoder.decode_type == UNKNOWN) { - Serial.println(F("Unknown type received. Ignoring.")); - } else { - codeType= My_Decoder.decode_type; - codeValue= My_Decoder.value; - codeBits= My_Decoder.bits; - GotOne=true; - } - My_Decoder.DumpResults(); - delay(1000); - My_Receiver.resume(); - } -} - diff --git a/libraries/IRLib/examples/rcmm/rcmm.ino b/libraries/IRLib/examples/rcmm/rcmm.ino deleted file mode 100644 index 989a260..0000000 --- a/libraries/IRLib/examples/rcmm/rcmm.ino +++ /dev/null @@ -1,210 +0,0 @@ -/* Example program for from IRLib – an Arduino library for infrared encoding and decoding - * Version 1.4 March 2014 - * Copyright 2014 by Chris Young http://cyborg5.com - * Based on original example sketch for IRremote library - * Version 0.11 September, 2009I know prescription and no - * Copyright 2009 Ken Shirriff - * http://www.righto.com/ - */ -/* - * This example demonstrates how to extend this library to add a new protocol - * without actually modifying or recompiling the libnonstickrary itself. It implements the - * Phillips RC-MM also known as Nokia Protocol. Is used by AT&T U-Verse cable boxes. - * There are three different varieties that are 12, 24, or 32 bits. - * This is a modified version of the IRecord example. - * If you would like to make this a permanent part of your library you could copy the class - * prototypes to IRLib.h and the code itself to IRLib.cpp. Search for the word "ADDITIONAL" - * to see where to add various pieces of code. Also see the Samsung36 example for details. - */ -#include -#include - -class IRdecodeRCMM: public virtual IRdecodeBase -{ -public: - bool decode(void); -}; - -class IRsendRCMM: public virtual IRsendBase -{ -public: - void send(unsigned long data, unsigned char nbits); -}; - -#define RCMM (LAST_PROTOCOL+1) -#define RCMM_HEAD_MARK 417 -#define RCMM_DATA_MARK 167 -#define RCMM_ZERO 278 -#define RCMM_ONE 444 -#define RCMM_TWO 611 -#define RCMM_THREE 778 - -/* - * According to http://www.hifi-remote.com/johnsfine/DecodeIR.html#Nokia - * The IRP notation for these protocols are: - * Nokia 12 bit: {36k,msb}<164,-276|164,-445|164,-614|164,-783>(412,-276,D:4,F:8,164,-???)+ - * Nokia 24-bit: {36k,msb}<164,-276|164,-445|164,-614|164,-783>(412,-276,D:8,S:8,F:8,164,-???)+ - * Nokia 32 bit: {36k,msb}<164,-276|164,-445|164,-614|164,-783>(412,-276,D:8,S:8,X:8,F:8,164,^100m)+ - * Slightly different timing values are documented at - * http://www.sbprojects.com/knowledge/ir/rcmm.php - * We will use the timing from the latter reference. - * Unlike most protocols which defined sequences for a logical "0" and "1", this protocol - * encodes 2 bits per pulse. Therefore it encodes a logical "2" and "3" as well. - * The length of the mark is constant but the length of the space denotes the bit values. - * Note the 32-bit version uses a toggle bit of 0x8000 and as usual it is up to the end-user - * to implement it outside the library routines. - */ -/* - * Normally IRLib uses a plus or minus percentage to determine if an interval matches the - * desired value. However this protocol uses extremely long intervals of similar length. - * For example using the default 25% tolerance the RCMM_TWO value 611 would be accepted for - * anything between 458 and 763. The low end is actually closer to RCMM_ONE value of 444 - * and the upper range is closer to RCM_THREE value of 778. To implement this protocol - * we created a new match routine ABS_MATCH which allows you to specify an absolute - * number of microseconds of tolerance for comparison. - */ -#define RCMM_TOLERANCE 80 -bool IRdecodeRCMM::decode(void) { - long data; - IRLIB_ATTEMPT_MESSAGE(F("RCMM")); - if ( (rawlen!=(12+2)) && (rawlen!=(24+2)) && (rawlen!=(32+4)) ) return RAW_COUNT_ERROR; - if (!MATCH(rawbuf[1],RCMM_HEAD_MARK)) return HEADER_MARK_ERROR(RCMM_HEAD_MARK); - if (!MATCH(rawbuf[2],RCMM_ZERO)) return HEADER_SPACE_ERROR(RCMM_ZERO); - offset=3; data=0; - while (offset < (rawlen-1)) { - if (!ABS_MATCH(rawbuf[offset],RCMM_DATA_MARK, RCMM_TOLERANCE+50)) return DATA_MARK_ERROR(RCMM_DATA_MARK); - offset++; - if (ABS_MATCH(rawbuf[offset],RCMM_ZERO, RCMM_TOLERANCE) ) { //Logical "0" - data <<= 2; - } - else if (ABS_MATCH(rawbuf[offset],RCMM_ONE, RCMM_TOLERANCE) ) { //Logical "1" - data = (data<<2) + 1; - } - else if (ABS_MATCH(rawbuf[offset],RCMM_TWO, RCMM_TOLERANCE) ) { //Logical "2" - data = (data<<2) + 2; - } - else if (ABS_MATCH(rawbuf[offset],RCMM_THREE, RCMM_TOLERANCE) ) { //Logical "3" - data = (data<<2) + 3; - } - else return DATA_SPACE_ERROR(RCMM_ZERO); - offset++; - } - if (!MATCH(rawbuf[offset],RCMM_DATA_MARK)) return DATA_MARK_ERROR(RCMM_DATA_MARK); - bits = rawlen-4;//set bit length - value = data;//put remaining bits in value - decode_type= static_castRCMM; - return true; -}; - -void IRsendRCMM::send(unsigned long data, unsigned char nbits) { - Extent=0; - data = data << (32 - nbits); - nbits=nbits/2; - enableIROut(36); - mark(RCMM_HEAD_MARK); space(RCMM_ZERO);//Send header - for (int i = 0; i < nbits; i++) { - mark(RCMM_DATA_MARK); - switch (data & 0xC0000000UL) {//use the leftmost two bits - case 0x00000000UL: space(RCMM_ZERO); break; - case 0x40000000UL: space(RCMM_ONE); break; - case 0x80000000UL: space(RCMM_TWO); break; - case 0xC0000000UL: space(RCMM_THREE); break; - default: Serial.println("sending error"); - } - data <<= 2; - }; - mark(RCMM_DATA_MARK); - space(27778-Extent); -}; - -//Create a custom class that combines this new protocol with all the others -class MyCustomSend: -public virtual IRsend, -public virtual IRsendRCMM -{ -public: - void send(IRTYPES Type, unsigned long data, int nbits); -}; -void MyCustomSend::send(IRTYPES Type, unsigned long data, int nbits) { - if (Type==RCMM) - IRsendRCMM::send(data,nbits); - else - IRsend::send(Type, data, nbits); -} -class MyCustomDecode: -public virtual IRdecode, -public virtual IRdecodeRCMM -{ -public: - virtual bool decode(void); // Calls each decode routine individually - void DumpResults(void); -}; -bool MyCustomDecode::decode(void) { - if (IRdecodeRCMM::decode()) return true; - return IRdecode::decode (); -} -void MyCustomDecode::DumpResults(void){ - if(decode_type==RCMM) { - Serial.print(F("Decoded RCMM: Value:")); Serial.print(value, HEX); - }; - IRdecode::DumpResults(); -}; - -MyCustomDecode My_Decoder; - -MyCustomSend My_Sender; - -int RECV_PIN = 11; - -IRrecv My_Receiver(RECV_PIN); -IRTYPES codeType; // The type of code -unsigned long codeValue; // The data bits -int codeBits; // The length of the code in bits or for Samsung is storage for data2 -bool GotOne; -char c; - -void setup() -{ - GotOne=false; - codeType=UNKNOWN; - codeValue=0; - codeBits=0; - Serial.begin(9600);while (! Serial) {}; - Serial.println(F("Send a code from your remote and we will record it.")); - Serial.println(F("Type any character and press enter. We will send the recorded code.")); - My_Receiver.enableIRIn(); // Start the receiver - My_Receiver.Mark_Excess=40; -} -void loop() { - if (Serial.available()>0) { - c=Serial.read(); - if (c=='p') {//Send a test pattern - GotOne= true; codeType=RCMM; codeValue=0x12345678; codeBits= 32; - } - if(GotOne) { - My_Sender.send(codeType,codeValue,codeBits); - Serial.print(F("Sent ")); - if (codeType== RCMM) Serial.print(F("RCMM")); else Serial.print(Pnames(codeType)); - Serial.print(F(" Value:0x")); - Serial.print(codeValue, HEX); - Serial.print(F(" Bits:")); - Serial.println(codeBits, DEC); - My_Receiver.enableIRIn(); // Re-enable receiver - } - } - else if (My_Receiver.GetResults(&My_Decoder)) { - My_Decoder.decode(); - if(My_Decoder.decode_type == UNKNOWN) { - Serial.println(F("Unknown type received. Ignoring.")); - } else { - codeType= My_Decoder.decode_type; - codeValue= My_Decoder.value; - codeBits= My_Decoder.bits; - GotOne=true; - } - My_Decoder.DumpResults(); - delay(1000); - My_Receiver.resume(); - } -} - diff --git a/libraries/IRLib/manuals/IRLibReference.docx b/libraries/IRLib/manuals/IRLibReference.docx deleted file mode 100644 index 08be4cb..0000000 Binary files a/libraries/IRLib/manuals/IRLibReference.docx and /dev/null differ diff --git a/libraries/IRLib/manuals/IRLibReference.pdf b/libraries/IRLib/manuals/IRLibReference.pdf deleted file mode 100644 index 9fe0734..0000000 Binary files a/libraries/IRLib/manuals/IRLibReference.pdf and /dev/null differ diff --git a/libraries/Oregon/examples/MySensors_example/MySensors_example.ino b/libraries/Oregon/examples/MySensors_example/MySensors_example.ino index 86d27af..d0eb0c1 100644 --- a/libraries/Oregon/examples/MySensors_example/MySensors_example.ino +++ b/libraries/Oregon/examples/MySensors_example/MySensors_example.ino @@ -49,7 +49,7 @@ #define MY_NODE_ID 10 // Enable and select radio type attached -#define MY_RADIO_NRF24 +#define MY_RADIO_RF24 //#define MY_RADIO_RFM69 #include diff --git a/libraries/PinChangeInt/Examples/ByteBuffer/ByteBuffer.cpp b/libraries/PinChangeInt/Examples/ByteBuffer/ByteBuffer.cpp deleted file mode 100644 index db49dce..0000000 --- a/libraries/PinChangeInt/Examples/ByteBuffer/ByteBuffer.cpp +++ /dev/null @@ -1,272 +0,0 @@ -/* - ByteBuffer.cpp - A circular buffer implementation for Arduino - Created by Sigurdur Orn, July 19, 2010. - siggi@mit.edu - Updated by GreyGnome (aka Mike Schwager) Thu Feb 23 17:25:14 CST 2012 - added the putString() method and the fillError variable. - added the checkError() and resetError() methods. The checkError() method resets the fillError variable - to false as a side effect. - added the ByteBuffer(unsigned int buf_size) constructor. - added the init() method, and had the constructor call it automagically. - Also made the capacity, position, length, and fillError variables volatile, for safe use by interrupts. - */ - -#include "ByteBuffer.h" - -void ByteBuffer::init(){ - ByteBuffer::init(DEFAULTBUFSIZE); -} - -void ByteBuffer::init(unsigned int buf_length){ - data = (byte*)malloc(sizeof(byte)*buf_length); - capacity = buf_length; - position = 0; - length = 0; - fillError=false; -} - -void ByteBuffer::deAllocate(){ - free(data); -} - -void ByteBuffer::clear(){ - position = 0; - length = 0; -} - -void ByteBuffer::resetError(){ - fillError=false; -} - -boolean ByteBuffer::checkError(){ - /* - if (fillError) { - Serial.print("E: checkError: length "); - Serial.println(length, DEC); - } - */ - - boolean result=fillError; - fillError=false; - return(result); -} - -int ByteBuffer::getSize(){ - return length; -} - -int ByteBuffer::getCapacity(){ - return capacity; -} - -byte ByteBuffer::peek(unsigned int index){ - byte b = data[(position+index)%capacity]; - return b; -} - -uint8_t ByteBuffer::put(byte in){ - if(length < capacity){ - // save data byte at end of buffer - data[(position+length) % capacity] = in; - // increment the length - length++; - return 1; - } - // return failure - //Serial.print("E: put: "); - //Serial.println(length, DEC); - fillError=true; - return 0; -} - - -uint8_t ByteBuffer::putString(char *in){ - uint8_t count=0; - char *inString; - - inString=in; - uint8_t oldSREG = SREG; cli(); - while(length <= capacity){ - if (length == capacity) { - fillError=true; - return count; - } - // save data byte at end of buffer - data[(position+length) % capacity] = *inString; - // increment the length - length++; - inString++; - count++; - if (*inString == 0) { - if (count==0) fillError=true; // Serial.println("E: putString"); }; - SREG = oldSREG; // Restore register; reenables interrupts - return count; - } - } - SREG = oldSREG; // Restore register; reenables interrupts - return count; -} - -uint8_t ByteBuffer::putInFront(byte in){ - uint8_t oldSREG = SREG; cli(); - if(length < capacity){ - // save data byte at end of buffer - if( position == 0 ) - position = capacity-1; - else - position = (position-1)%capacity; - data[position] = in; - // increment the length - length++; - SREG = oldSREG; // Restore register; reenables interrupts - return 1; - } - // return failure - //Serial.println("E: putInFront"); - fillError=true; - SREG = oldSREG; // Restore register; reenables interrupts - return 0; -} - -byte ByteBuffer::get(){ - uint8_t oldSREG = SREG; cli(); - byte b = 0; - - if(length > 0){ - b = data[position]; - // move index down and decrement length - position = (position+1)%capacity; - length--; - } - SREG = oldSREG; // Restore register; reenables interrupts - return b; -} - -byte ByteBuffer::getFromBack(){ - byte b = 0; - if(length > 0){ - uint8_t oldSREG = SREG; cli(); - b = data[(position+length-1)%capacity]; - length--; - SREG = oldSREG; // Restore register; reenables interrupts - } - - return b; -} - -// -// Ints -// - -void ByteBuffer::putIntInFront(int in){ - byte *pointer = (byte *)∈ - putInFront(pointer[0]); - putInFront(pointer[1]); -} - -void ByteBuffer::putInt(int in){ - byte *pointer = (byte *)∈ - put(pointer[1]); - put(pointer[0]); -} - - -int ByteBuffer::getInt(){ - int ret; - byte *pointer = (byte *)&ret; - pointer[1] = get(); - pointer[0] = get(); - return ret; -} - -int ByteBuffer::getIntFromBack(){ - int ret; - byte *pointer = (byte *)&ret; - pointer[0] = getFromBack(); - pointer[1] = getFromBack(); - return ret; -} - -// -// Longs -// - -void ByteBuffer::putLongInFront(long in){ - byte *pointer = (byte *)∈ - putInFront(pointer[0]); - putInFront(pointer[1]); - putInFront(pointer[2]); - putInFront(pointer[3]); -} - -void ByteBuffer::putLong(long in){ - byte *pointer = (byte *)∈ - put(pointer[3]); - put(pointer[2]); - put(pointer[1]); - put(pointer[0]); -} - - -long ByteBuffer::getLong(){ - long ret; - byte *pointer = (byte *)&ret; - pointer[3] = get(); - pointer[2] = get(); - pointer[1] = get(); - pointer[0] = get(); - return ret; -} - -long ByteBuffer::getLongFromBack(){ - long ret; - byte *pointer = (byte *)&ret; - pointer[0] = getFromBack(); - pointer[1] = getFromBack(); - pointer[2] = getFromBack(); - pointer[3] = getFromBack(); - return ret; -} - - -// -// Floats -// - -void ByteBuffer::putFloatInFront(float in){ - byte *pointer = (byte *)∈ - putInFront(pointer[0]); - putInFront(pointer[1]); - putInFront(pointer[2]); - putInFront(pointer[3]); -} - -void ByteBuffer::putFloat(float in){ - byte *pointer = (byte *)∈ - put(pointer[3]); - put(pointer[2]); - put(pointer[1]); - put(pointer[0]); -} - -float ByteBuffer::getFloat(){ - float ret; - byte *pointer = (byte *)&ret; - pointer[3] = get(); - pointer[2] = get(); - pointer[1] = get(); - pointer[0] = get(); - return ret; -} - -float ByteBuffer::getFloatFromBack(){ - float ret; - byte *pointer = (byte *)&ret; - pointer[0] = getFromBack(); - pointer[1] = getFromBack(); - pointer[2] = getFromBack(); - pointer[3] = getFromBack(); - return ret; -} - - diff --git a/libraries/PinChangeInt/Examples/ByteBuffer/ByteBuffer.h b/libraries/PinChangeInt/Examples/ByteBuffer/ByteBuffer.h deleted file mode 100644 index 8916e58..0000000 --- a/libraries/PinChangeInt/Examples/ByteBuffer/ByteBuffer.h +++ /dev/null @@ -1,104 +0,0 @@ -/* - ByteBuffer.h - A circular buffer implementation for Arduino - Created by Sigurdur Orn, July 19, 2010. siggi@mit.edu - Updated by GreyGnome (aka Mike Schwager) Thu Feb 23 17:25:14 CST 2012 - added the putString() method and the fillError variable. - added the checkError() and resetError() methods. The checkError() method resets the fillError variable - to false as a side effect. - added the ByteBuffer(unsigned int buf_size) constructor. - added the init() method, and had the constructor call it automagically. - protected certain sections of the code with cli()/sei() calls, for safe use by interrupts. - Also made the capacity, position, length, and fillError variables volatile, for safe use by interrupts. - */ - -#ifndef ByteBuffer_h -#define ByteBuffer_h - -#if defined(ARDUINO) && ARDUINO >= 100 - #include -#else - #include -#endif -//#include - -#define DEFAULTBUFSIZE 32 -class ByteBuffer -{ -public: - ByteBuffer() { - init(); - }; - ByteBuffer(unsigned int buf_size) { - init(buf_size); - }; - - // This method initializes the datastore of the buffer to a certain size. - void init(unsigned int buf_size); - - // This method initializes the datastore of the buffer to the default size. - void init(); - - // This method resets the buffer into an original state (with no data) - void clear(); - - // This method resets the fillError variable to false. - void resetError(); - - // This method tells you if your buffer overflowed at some time since the last - // check. The error state will be reset to false. - boolean checkError(); - - // This releases resources for this buffer, after this has been called the buffer should NOT be used - void deAllocate(); - - // Returns how much space is used in the buffer - int getSize(); - - // Returns the maximum capacity of the buffer - int getCapacity(); - - // This method returns the byte that is located at index in the buffer but doesn't modify the buffer like the get methods (doesn't remove the retured byte from the buffer) - byte peek(unsigned int index); - - // - // Put methods, either a regular put in back or put in front - // - uint8_t putInFront(byte in); - uint8_t put(byte in); - uint8_t putString(char *in); - - void putIntInFront(int in); - void putInt(int in); - - void putLongInFront(long in); - void putLong(long in); - - void putFloatInFront(float in); - void putFloat(float in); - - // - // Get methods, either a regular get from front or from back - // - byte get(); - byte getFromBack(); - - int getInt(); - int getIntFromBack(); - - long getLong(); - long getLongFromBack(); - - float getFloat(); - float getFloatFromBack(); - -private: - byte* data; - - volatile unsigned int capacity; - volatile unsigned int position; - volatile unsigned int length; - volatile boolean fillError; -}; - -#endif - diff --git a/libraries/PinChangeInt/Examples/GetPSTR/GetPSTR.h b/libraries/PinChangeInt/Examples/GetPSTR/GetPSTR.h deleted file mode 100644 index aaab06a..0000000 --- a/libraries/PinChangeInt/Examples/GetPSTR/GetPSTR.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef INCLUDE_GETPSTR -#define INCLUDE_GETPSTR - -#if defined(ARDUINO) && ARDUINO >= 100 - #include -#else - #include "pins_arduino.h" - #include "WProgram.h" - #include "wiring.h" -#endif - -#define getPSTR(s) pgmStrToRAM(PSTR(s)) - -char *_pstr_to_print; -char *pgmStrToRAM(PROGMEM char *theString) { - free(_pstr_to_print); - _pstr_to_print=(char *) malloc(strlen_P(theString)); - strcpy_P(_pstr_to_print, theString); - return (_pstr_to_print); -} -#endif diff --git a/libraries/PinChangeInt/Examples/PinChangeIntExample/PinChangeIntExample.pde b/libraries/PinChangeInt/Examples/PinChangeIntExample/PinChangeIntExample.pde deleted file mode 100644 index 2f254d4..0000000 --- a/libraries/PinChangeInt/Examples/PinChangeIntExample/PinChangeIntExample.pde +++ /dev/null @@ -1,77 +0,0 @@ -// PinChangeIntExample, version 1.1 Sun Jan 15 06:24:19 CST 2012 -// See the Wiki at http://code.google.com/p/arduino-pinchangeint/wiki for more information. -//-------- define these in your sketch, if applicable ---------------------------------------------------------- -// You can reduce the memory footprint of this handler by declaring that there will be no pin change interrupts -// on any one or two of the three ports. If only a single port remains, the handler will be declared inline -// reducing the size and latency of the handler. -//#define NO_PORTB_PINCHANGES // to indicate that port b will not be used for pin change interrupts -//#define NO_PORTC_PINCHANGES // to indicate that port c will not be used for pin change interrupts -// #define NO_PORTD_PINCHANGES // to indicate that port d will not be used for pin change interrupts -// if there is only one PCInt vector in use the code can be inlined -// reducing latency and code size -// define DISABLE_PCINT_MULTI_SERVICE below to limit the handler to servicing a single interrupt per invocation. -// #define DISABLE_PCINT_MULTI_SERVICE -//-------- define the above in your sketch, if applicable ------------------------------------------------------ -#include - -// This example demonstrates a configuration of 3 interrupting pins and 2 interrupt functions. -// All interrupts are serviced immediately, but one of the pins (pin 4) will show you immediately -// on the Terminal. The other function connected to 2 pins sets an array member that is queried in loop(). -// You can then query the array at your leisure. -// This makes loop timing non-critical. - -// Add more Pins at your leisure. -// For the Analog Input pins used as digital input pins, and you can use 14, 15, 16, etc. -// or you can use A0, A1, A2, etc. (the Arduino code comes with #define's -// for the Analog Input pins and will properly recognize e.g., pinMode(A0, INPUT); -#define PIN1 2 -#define PIN2 3 -#define PIN3 4 - -uint8_t latest_interrupted_pin; -uint8_t interrupt_count[20]={0}; // 20 possible arduino pins -void quicfunc() { - latest_interrupted_pin=PCintPort::arduinoPin; - interrupt_count[latest_interrupted_pin]++; -}; - -// You can assign any number of functions to any number of pins. -// How cool is that? -void pin3func() { - Serial.print("Pin "); Serial.print(PIN3, DEC); Serial.println("!"); -} - -void setup() { - pinMode(PIN1, INPUT); digitalWrite(PIN1, HIGH); - PCintPort::attachInterrupt(PIN1, &quicfunc, FALLING); // add more attachInterrupt code as required - pinMode(PIN2, INPUT); digitalWrite(PIN2, HIGH); - PCintPort::attachInterrupt(PIN2, &quicfunc, FALLING); - pinMode(PIN3, INPUT); digitalWrite(PIN3, HIGH); - PCintPort::attachInterrupt(PIN3, &pin3func, CHANGE); - Serial.begin(115200); - Serial.println("---------------------------------------"); -} - -uint8_t i; -void loop() { - uint8_t count; - Serial.print("."); - delay(1000); - for (i=0; i < 20; i++) { - if (interrupt_count[i] != 0) { - count=interrupt_count[i]; - interrupt_count[i]=0; - Serial.print("Count for pin "); - if (i < 14) { - Serial.print("D"); - Serial.print(i, DEC); - } else { - Serial.print("A"); - Serial.print(i-14, DEC); - } - Serial.print(" is "); - Serial.println(count, DEC); - } - } -} - diff --git a/libraries/PinChangeInt/Examples/PinChangeIntSpeedTest/PinChangeIntSpeedTest.pde b/libraries/PinChangeInt/Examples/PinChangeIntSpeedTest/PinChangeIntSpeedTest.pde deleted file mode 100644 index f0ac0f2..0000000 --- a/libraries/PinChangeInt/Examples/PinChangeIntSpeedTest/PinChangeIntSpeedTest.pde +++ /dev/null @@ -1,265 +0,0 @@ -// PinChangeIntSpeedTest by GreyGnome aka Mike Schwager. Version numbers here refer to this sketch. -// Version 1.0 - initial version -// Version 1.1 - added code to test digitalRead() -// Version 1.2 - added new comments for the #define's for the NO_PORTx_PINCHANGES. -// Version 1.3 - includes cbiface.h with ooPinChangeInt, rather than cb.h -// Version 1.4 - testing version 2.10Beta with robtillaart's optimization -// Also added a #define/#undef INLINE_PCINTFUNC for inlining of the function called by the interrupt. -// Default: #undef for using the function as per usual. Changed PCIVERSION so that -// ooPinChangeInt starts at 1000 instead of 200. Modified the "Start" message to show "Start..", pause -// for 1 second, show "*\n" (where \n is a newline), pause for 1 second, then run the test. -// Version 1.4 - made this compatible with version 1.5 of PinChangeInt -// Version 1.5 - modified it to use #define OOPCIVERSION for ooPinChangeInt - -// This version number is for ooPinChangeInt -//#define OOPCIVERSION 1030 -#ifndef OOPCIVERSION -#define PCIVERSION 217 // 110 if using PinChangeInt-1.1, 120 for version 1.2 - // 1000 for ooPinChangeIntversion 1.00, 1001 for ooPinChangeInt version 1.01, etc. -#endif - -//-------- define these in your sketch, if applicable ---------------------------------------------------------- -// You can reduce the memory footprint of this handler by declaring that there will be no pin change interrupts -// on any one or two of the three ports. If only a single port remains, the handler will be declared inline -// reducing the size and latency of the handler. -#undef NO_PORTB_PINCHANGES // to indicate that port b will not be used for pin change interrupts -#undef NO_PORTC_PINCHANGES // to indicate that port c will not be used for pin change interrupts -// #define NO_PORTD_PINCHANGES // to indicate that port d will not be used for pin change interrupts -// You can reduce the code size by 20-50 bytes, and you can speed up the interrupt routine -// slightly by declaring that you don't care if the static variables PCintPort::pinState and/or -// PCintPort::arduinoPin are set and made available to your interrupt routine. -// #define NO_PIN_STATE // to indicate that you don't need the pinState -// #define NO_PIN_NUMBER // to indicate that you don't need the arduinoPin -// if there is only one PCInt vector in use the code can be inlined -// reducing latency and code size -// define DISABLE_PCINT_MULTI_SERVICE below to limit the handler to servicing a single interrupt per invocation. -//#define DISABLE_PCINT_MULTI_SERVICE -//-------- define the above in your sketch, if applicable ------------------------------------------------------ -#if defined(OOPCIVERSION) - #define LIBRARYUNDERTEST "ooPinChangeInt" - #include - #if PCIVERSION == 1001 - #include - #else - #include - #endif -#else - #define LIBRARYUNDERTEST "PinChangeInt" - #include -#endif - -#define SERIALSTUFF // undef to take out all serial statements. Default: #define for measuring time. -#undef MEMTEST // undef to take out memory tests. Default: #undef for measuring time. -#undef INLINE_PCINTFUNC // define to inline the function called from the interrupt. This should have no effect, - // because the compiler will store the registers upon calling the interrupt routine, just - // like calling a function. Still, we test all assumptions. -//----------------------- -// NOTE: BECAUSE OF COLLISIONS in these libraries, you CANNOT have both libraries: PinChangeInt -// and ooPinChangeInt in the libraries directory at the same time. That said, under UNIX-y operating -// systems, it's easy to move the library directory to a name such as "PinChangeInt-1.3", which the -// Arduino will not recognize, and then create a symbolic link when you want to use a library. Such as: -// cd ~/Documents/Arduino/libaries -// mv PinChangeInt PinChangeInt-1.30 -// mv ooPinChangeInt ooPinChangeInt-1.00 -// ln -s PinChangeInt-1.30 PinChangeInt - -#undef FLASH // to flash LED on pin 13 during test - -#ifdef MEMTEST -#include -#endif - -#define TEST 6 - -#if TEST == 1 -#define PTEST 2 // pin to trigger interrupt. pins 0 and 1 are used -#define PLOW 2 // by Serial, so steer clear of them! -#define PHIGH 2 // Interrupts are attached to these pins - -#elif TEST == 2 // see the #if TEST == 2 || TEST == 3 code, below -#define PTEST 2 -#define PLOW 2 -#define PHIGH 2 // need to attachInterrupt to 5 in the code - -#elif TEST == 3 // see the #if TEST == 2 || TEST == 3 code, below -#define PTEST 5 -#define PLOW 2 -#define PHIGH 2 // need to attachInterrupt to 5 in the code - -#elif TEST == 4 -#define PTEST 2 -#define PLOW 2 -#define PHIGH 5 - -#elif TEST == 5 -#define PTEST 3 -#define PLOW 2 -#define PHIGH 5 - -#elif TEST == 6 -#define PTEST 4 -#define PLOW 2 -#define PHIGH 5 - -#elif TEST == 7 -#define PTEST 5 -#define PLOW 2 -#define PHIGH 5 -#endif - -uint8_t qf0; - -#ifdef INLINE_PCINTFUNC -#define INLINE_PCINTFUNC inline -#else -#define INLINE_PCINTFUNC -#endif -INLINE_PCINTFUNC void quicfunc(); -void quicfunc() { - qf0=TCNT0; -} - -#if defined(OOPCIVERSION) -class speedy : public CallBackInterface -{ - public: - uint8_t id; - static uint8_t var0; - speedy () { id=0; }; - speedy (uint8_t _i): id(_i) {}; - - void cbmethod() { - speedy::var0=TCNT0; - //Serial.print("Speedy method "); // debugging - //Serial.println(id, DEC); - }; -}; -uint8_t speedy::var0=0; -#endif - -volatile uint8_t *led_port; -volatile uint8_t *pinT_OP; -volatile uint8_t *pinT_IP; -uint8_t led_mask, not_led_mask; -uint8_t pinT_M, not_pinT_M; -volatile uint8_t pintest, pinIntLow, pinIntHigh; -uint8_t totalpins; -#if defined(OOPCIVERSION) -speedy speedster[8]={speedy(0), speedy(1), speedy(2), speedy(3), speedy(4), speedy(5), speedy(6), speedy(7) }; -#endif -#ifdef MEMTEST -int freemem; -#endif - -int i=0; - -#define PINLED 13 -void setup() -{ -#ifdef SERIALSTUFF - Serial.begin(115200); Serial.println("---------------------------------------"); -#endif // SERIALSTUFF - // set up ports for trigger - pinMode(0, OUTPUT); digitalWrite(0, HIGH); - pinMode(1, OUTPUT); digitalWrite(1, HIGH); - pinMode(2, OUTPUT); digitalWrite(2, HIGH); - pinMode(3, OUTPUT); digitalWrite(3, HIGH); - pinMode(4, OUTPUT); digitalWrite(4, HIGH); - pinMode(5, OUTPUT); digitalWrite(5, HIGH); - pinMode(6, OUTPUT); digitalWrite(6, HIGH); - pinMode(7, OUTPUT); digitalWrite(7, HIGH); -#ifdef FLASH - led_port=portOutputRegister(digitalPinToPort(PINLED)); - led_mask=digitalPinToBitMask(PINLED); - not_led_mask=led_mask^0xFF; - pinMode(PINLED, OUTPUT); digitalWrite(PINLED, LOW); -#endif - // ***************************************************************************** - // set up ports for output ************ PIN TO TEST IS GIVEN HERE ************** - // ***************************************************************************** - pintest=PTEST; - pinIntLow=PLOW; pinIntHigh=PHIGH; // Interrupts are attached to these pins - // ***************************************************************************** - // ***************************************************************************** - pinT_OP=portOutputRegister(digitalPinToPort(pintest)); // output port - pinT_IP=portInputRegister(digitalPinToPort(pintest)); // input port - pinT_M=digitalPinToBitMask(pintest); // mask - not_pinT_M=pinT_M^0xFF; // not-mask - *pinT_OP|=pinT_M; - for (i=pinIntLow; i <= pinIntHigh; i++) { -#if defined(OOPCIVERSION) - PCintPort::attachInterrupt(i, &speedster[i], CHANGE); // C++ technique; v1.3 or better -#endif -#if defined(PCIVERSION) - PCintPort::attachInterrupt((uint8_t) i, &quicfunc, CHANGE); // C technique; v1.2 or earlier -#endif - } -#if TEST == 2 || TEST == 3 - i=5; totalpins=2; -#if defined(OOPCIVERSION) - PCintPort::attachInterrupt(i, &speedster[i], CHANGE); // C++ technique; v1.3 or better -#endif -#if defined(PCIVERSION) - PCintPort::attachInterrupt(i, &quicfunc, CHANGE); // C technique; v1.2 or earlier -#endif -#else - totalpins=pinIntHigh - pinIntLow + 1; -#endif - i=0; -} // end setup() - -uint8_t k=0; -unsigned long milliStart, milliEnd, elapsed; -void loop() { - k=0; - *pinT_OP|=pinT_M; // pintest to 1 -#ifdef SERIALSTUFF - Serial.print(LIBRARYUNDERTEST); Serial.print(" "); - Serial.print("TEST: "); Serial.print(TEST, DEC); Serial.print(" "); -#ifndef MEMTEST - Serial.print("test pin mask: "); Serial.print(pinT_M, HEX); - Serial.print(". Total of "); Serial.print(totalpins, DEC); Serial.println(" pins enabled."); -#endif -#ifdef MEMTEST - freemem=freeMemory(); Serial.print("Free memory: "); Serial.println(freemem, DEC); -#endif -#endif - delay(1000); - Serial.print("Start.."); - delay(1000); Serial.print("*"); - #ifdef FLASH - *led_port|=led_mask; - #endif - milliStart=millis(); - while (k < 10) { - i=0; - while (i < 10000) { - *pinT_OP&=not_pinT_M; // pintest to 0 ****************************** 16.8 us - *pinT_OP|=pinT_M; // pintest to 1 ****************************** ...to get here - i++; - } - k++; - } - milliEnd=millis(); - #ifdef FLASH - *led_port&=not_led_mask; - #endif - elapsed=milliEnd-milliStart; - #ifndef MEMTEST - Serial.print(" Elapsed: "); - Serial.println(elapsed, DEC); - #endif - #ifdef SERIALSTUFF - Serial.print("Interrupted pin: "); - #if defined(OOPCIVERSION) - Serial.println(speedster[pintest].id, DEC); - #else - Serial.println(PCintPort::arduinoPin, DEC); - #endif -#ifdef MEMTEST - freemem=freeMemory(); Serial.print("END-Free memory: "); Serial.println(freemem, DEC); -#endif -#endif - delay(500); -} - diff --git a/libraries/PinChangeInt/Examples/PinChangeIntTest/PinChangeIntTest.pde b/libraries/PinChangeInt/Examples/PinChangeIntTest/PinChangeIntTest.pde deleted file mode 100644 index c8df127..0000000 --- a/libraries/PinChangeInt/Examples/PinChangeIntTest/PinChangeIntTest.pde +++ /dev/null @@ -1,344 +0,0 @@ -// PinChangeIntTest -// version 1.0 Wed Feb 15 07:25:09 CST 2012 -// Version 1.1 Fri Jun 22 19:10:50 CDT 2012 minor tweaks to eliminate compiler warnings. Also, there were bugfixes in ByteBuffer. -// I had some "cli()" without "sei()" in there. -// See the Wiki at http://code.google.com/p/arduino-pinchangeint/wiki for more information. -// This sketch requires the ByteBuffer library, which is found in the PinChangeInt zipfile. -//-------- define these in your sketch, if applicable ---------------------------------------------------------- -//-------- This must go ahead of the #include statement -------------------------------------------------------- -// You can reduce the memory footprint of this handler by declaring that there will be no pin change interrupts -// on any one or two of the three ports. If only a single port remains, the handler will be declared inline -// reducing the size and latency of the handler. -// #define NO_PORTB_PINCHANGES // to indicate that port b will not be used for pin change interrupts -// #define NO_PORTC_PINCHANGES // to indicate that port c will not be used for pin change interrupts -// #define NO_PORTD_PINCHANGES // to indicate that port d will not be used for pin change interrupts -// You can reduce the code size by 20-50 bytes, and you can speed up the interrupt routine -// slightly by declaring that you don't care if the static variables PCintPort::pinState and/or -// PCintPort::arduinoPin are set and made available to your interrupt routine. -// #define NO_PIN_STATE // to indicate that you don't need the pinState -// #define NO_PIN_NUMBER // to indicate that you don't need the arduinoPin -// if there is only one PCInt vector in use the code can be inlined -// reducing latency and code size -// define DISABLE_PCINT_MULTI_SERVICE below to limit the handler to servicing a single interrupt per invocation. -// #define DISABLE_PCINT_MULTI_SERVICE -// The following is intended for testing purposes. If defined, then a variable PCintPort::pinMode can be read -// in your interrupt subroutine. It is not defined by default: -// #define PINMODE -//-------- define the above in your sketch, if applicable ------------------------------------------------------ -#define PINMODE -#define FLASH -#include -#include - -// This example demonstrates a configuration of 6 interrupting pins and 3 interrupt functions. -// A variety of interrupting pins have been chosen, so as to test all PORTs on the Arduino. -// The pins are as follows: -#define tPIN1 2 // port D -#define tPIN2 3 -#define tPIN3 11 // Port B -#define tPIN4 12 -#define tPIN5 A3 // Port C, also can be given as "17" -#define tPIN6 A4 // starts and stops the count -// All pins send interrupts. Arduino pins 2 and A4 (tPIN1,6) interrupt on FALLING. -// Arduino pins 3 and 12 (tPIN2,4) interrupt on RISING. -// Arduino pins 11 and A3 (tPIN5) interrupts on CHANGE. -// quicfunc0 is attached to Arduino pins 2, 3, 11, and 12 (tPIN1-4) -// quicfunc1 is attached to Arduino pin A3 (tPIN5) -// quicfunc2 is attached to Arduino pin A4 (tPIN6). -// NOTE: -// For the Analog Input pins used as digital input pins, you can use numbers such as 14, 15, 16, etc. -// or you can use A0, A1, A2, etc. (the Arduino code comes with #define's for the Analog Input pin -// names and will properly recognize e.g., pinMode(A0, INPUT)); - -// HOW IT WORKS -// The interrupt on Arduino pin A4 (tPIN6) will, when triggered, start the counting of interrupts. -// The array interrupt_count0[20] is updated in the interrupts; each cell keeps track of the number -// of interrupts on one of the 20 available interrupt pins on the Arduino. Every second in the main -// loop the array is scanned and registered interrupts are reported for all pins interrupted since -// the previous second. If no interrupts, the output is quiet. - -// tPIN6 is special. Not only does it start the counting of the interrups, but it turns on and off -// interrupts on pins 2, 11, and A3/17 (tPIN1, tPIN3, tPIN5). All pins start by interrupting, but after -// the count is turned on and then turned off, the 3 pins are detached from interrupts. -// Everytime thereafter when the count is turned off the 3 pins are detached. They are reattached -// when turned on. - -// Output is copied to a buffer, because we can't do a Serial.print() statement in an interrupt -// routine. The main loop checks for entries in the buffer and prints them if found. -// Output looks like this: -// -F- - an interrupt triggered by a falling signal occurred. -// +R+ - an interrupt triggered by a rising signal occurred. -// *C* - an interrupt triggered by a change in signal occurred. -// f#p#-P# - f# shows the interrupt subroutine that was called: 0, 1, or 2 -// - p# shows the pin number that triggered the interrupt -// - P# shows the port that this pin number is attached to. 2 is PORTB, 3 is PORTC, 4 is PORTD - -// HOW TO CONNECT -// Each pin gets a momentary contact switch connected to it. One side of the switch should connect -// to ground. The other side of the switch connects to the Arduino pin. For my purposes, I am using -// two rotary encoders. Each encoder contains 3 switches. But 6 regular pushbuttons would work, too. - -/* WHAT TO LOOK FOR - Output is sent to the serial line, so the Arduino IDE's serial terminal should be opened. - Upon startup, press tPINS1-5. You will see output like this: --F-f0p2-P4 (counting off) -..*C*f0p11-P2 (counting off) -+R+f0p3-P4 (counting off) - This shows that - 1. an interrupt was triggered on a falling signal (*F*). It called (f0) function 0, which is quicfunc0. - The triggering pin was (p2) Arduuino pin 2, which is on (P4) Port 4 (PORTD). Counting of this interrupt is - off, so you will not see any output from the main loop. - 2. Two dots appeared. Dots came from iterations of loop(), so these 2 dots show that the two interrupts happened 2 seconds apart. - 3. an interrupt was triggered on a change in signal (*C*). It called quicfunc0, from Arduino pin 11, on Port 2 (PORTB). - The interrupt was not counted. - 4. an interrupt was triggered on a rising signal (+R+). It called quicfunc0, from Arduino pin 3, on Purt 4 (PORTD). - The pin should have started out at the high level, so likely the signal fell during onother interrupt, and now - the rise has been caught. - - Now press the button attached to tPIN6 (in our case, A4 or D18). You will see something like this: --F-START! f2p18-P3 -.Count for pin A4 is 1 - This shows that - 1. The counting machanism (START!) was triggered by a folling signal (-F-) on pin 18 (p18) which is in Port 3 (P3) (which == PORTC) and - function f2 was called (f2). - 2. A dot appeared, which came from loop() because a second passed. - 3. The count for p18 or A4 was displayed. - - Now you will see messages for all the pins that you manipulate, for example: -*C*f0p11-P2 -+R+f0p3-P4 -*C*f0p11-P2 -+R+f0p3-P4 -*C*f0p11-P2 -.Count for pin D3 is 6 -Count for pin D11 is 9 -.+R+f0p3-P4 --F-f0p2-P4 -.Count for pin D2 is 1 -Count for pin D3 is 1 - These codes reflect the interrupts, as described above. This output will take place until you press tPIN6: --F-f2: STOP! Counting off. -Interrupt OFF on tPIN1 (2) tPIN3 (11) tPIN5 (17) - Then you will see output like this: -.....................+R+f0p12-P2 (counting off) -.+R+f0p12-P2 (counting off) -+R+f0p12-P2 (counting off) -+R+f0p12-P2 (counting off) - and tPIN1, tPIN3, and tPIN5 will not trigger interrupts. -*/ -// NOTES -// Output overwrites: -// It's possible during moderately fast interrupts to see your print output get garbled; eg, -// +R+f0p12-P2 (+R+f0p12-P2 (counting +R+f0p12-P2 (cou+R+f0p12-P+R+f0p12 -// This is because the print of the buffer takes place inside a while loop, and it can -// be interrupted and new data inserted into the buffer at a midpoint of the buffer's text. -// Just by spinning my rotary encoders I can readily generate over 200 interrupts per second -// on a pin, which is easily fast enough to overrun Serial output at 115,200 bps. -// The lesson here? ...Interrupts are tricky, and interrupt service routines should be fast. -// Just sayin'. - -// Pins: -// We want to use pins from each of ports B, C and D. So choose wisely. Ports are shown in -// this diagram of the ATmega328P chip. PD0 means "Port D, pin 0". PC3 means "Port C, Pin 3", -// PB2 means "Port B, pin 2" and so on. The corresponding Arduino pins are in parentheses. -// So PB2 is Arduino pin D 10, for example. -/* - +-\/-+ - PC6 1| |28 PC5 (AI 5) - (D 0) PD0 2| |27 PC4 (AI 4) - (D 1) PD1 3| |26 PC3 (AI 3) - (D 2) PD2 4| |25 PC2 (AI 2) - PWM+ (D 3) PD3 5| |24 PC1 (AI 1) - (D 4) PD4 6| |23 PC0 (AI 0) - VCC 7| |22 GND - GND 8| |21 AREF - PB6 9| |20 AVCC - PB7 10| |19 PB5 (D 13) - PWM+ (D 5) PD5 11| |18 PB4 (D 12) - PWM+ (D 6) PD6 12| |17 PB3 (D 11) PWM - (D 7) PD7 13| |16 PB2 (D 10) PWM - (D 8) PB0 14| |15 PB1 (D 9) PWM - +----+ -*/ - -uint8_t pins[6]={ tPIN1, tPIN2, tPIN3, tPIN4, tPIN5, tPIN6 }; -uint8_t ports[6]={ 0, 0, 0, 0, 0, 0 }; - -uint8_t latest_interrupted_pin; -uint8_t interrupt_count[20]={0}; // 20 possible arduino pins -uint8_t port; -uint8_t mode; - -ByteBuffer printBuffer(80); -char charArray[16]; -char numBuffer[4] = { 0, 0, 0, 0 }; -uint8_t printFull=0; - -volatile boolean start=0; -volatile boolean initial=true; -long begintime=0; -long now=0; - -void uint8ToString(char *outString, uint8_t number) { - uint8_t hundreds=0; - uint8_t tens=0; - uint8_t ones=0; - - while (number >= 100 ) { - hundreds++; - number-=100; - } - while (number >= 10 ) { - tens++; - number-=10; - } - ones=number; - ones+=48; - if (hundreds > 0) { hundreds+=48; tens+=48; outString[0]=hundreds; outString[1]=tens; outString[2]=ones; outString[3]=0; } - else if (tens > 0) { tens+=48; outString[0]=tens; outString[1]=ones; outString[2]=0; } - else { outString[0]=ones; outString[1]=0; }; -} - -void showMode() { - switch (mode) { - case FALLING: - printBuffer.putString((char *) "-F-"); - break; - case RISING: - printBuffer.putString((char *) "+R+"); - break; - case CHANGE: - printBuffer.putString((char *) "*C*"); - break; - } -} - -void quicfunc0() { - latest_interrupted_pin=PCintPort::arduinoPin; - mode=PCintPort::pinmode; - showMode(); - if (start==1) { - interrupt_count[latest_interrupted_pin]++; - } - uint8ToString(numBuffer, latest_interrupted_pin); - printBuffer.putString((char *) "f0p"); printBuffer.putString(numBuffer); printBuffer.putString((char *) "-P"); - uint8ToString(numBuffer, digitalPinToPort(latest_interrupted_pin)); - printBuffer.putString(numBuffer); - if (start !=1) printBuffer.putString((char *) " (counting off)"); - printBuffer.putString((char *) "\n"); -}; - -void quicfunc1() { - latest_interrupted_pin=PCintPort::arduinoPin; - mode=PCintPort::pinmode; - showMode(); - if (start==1) { - interrupt_count[latest_interrupted_pin]++; - } - uint8ToString(numBuffer, latest_interrupted_pin); - printBuffer.putString((char *) "f1p"); printBuffer.putString(numBuffer); printBuffer.putString((char *) "-P"); - uint8ToString(numBuffer, digitalPinToPort(latest_interrupted_pin)); - printBuffer.putString(numBuffer); - if (start !=1) printBuffer.putString((char *) " (counting off)"); - printBuffer.putString((char *) "\n"); -}; - -void quicfunc2() { - latest_interrupted_pin=PCintPort::arduinoPin; - mode=PCintPort::pinmode; - showMode(); - if (start == 1) { - printBuffer.putString((char *) "f2: STOP! Counting off.\n"); - printBuffer.putString((char *) "Interrupt OFF on tPIN1 ("); uint8ToString(numBuffer, tPIN1), printBuffer.putString(numBuffer); - printBuffer.putString((char *) ") tPIN3 (");uint8ToString(numBuffer, tPIN3), printBuffer.putString(numBuffer); - printBuffer.putString((char *) ") tPIN5 (");uint8ToString(numBuffer, tPIN5), printBuffer.putString(numBuffer); - printBuffer.putString((char *) ")\n"); - PCintPort::detachInterrupt(tPIN1); PCintPort::detachInterrupt(tPIN3); PCintPort::detachInterrupt(tPIN5); - start=0; - } else { - start=1; - interrupt_count[latest_interrupted_pin]++; - printBuffer.putString((char *) "START! f2p"); - uint8ToString(numBuffer, latest_interrupted_pin); - printBuffer.putString(numBuffer); printBuffer.putString((char *) "-P"); - uint8ToString(numBuffer, digitalPinToPort(latest_interrupted_pin)); - printBuffer.putString(numBuffer); printBuffer.putString((char *) "\n"); - if (! initial) { - PCintPort::attachInterrupt(tPIN1, &quicfunc0, FALLING); - PCintPort::attachInterrupt(tPIN3, &quicfunc0, CHANGE); - PCintPort::attachInterrupt(tPIN5, &quicfunc1, CHANGE); - } else { - initial=false; - } - } -}; - -uint8_t i; -void setup() { - Serial.begin(115200); - delay(250); - Serial.println("Test"); - delay(500); - for (i=0; i < 7; i++) { - pinMode(pins[i], INPUT); digitalWrite(pins[i], HIGH); - ports[i]=digitalPinToPort(pins[i]); - switch (pins[i]) { - case tPIN1: - PCintPort::attachInterrupt(pins[i], &quicfunc0, FALLING); - break; - case tPIN3: - PCintPort::attachInterrupt(pins[i], &quicfunc0, CHANGE); - break; - case tPIN2: - case tPIN4: - PCintPort::attachInterrupt(pins[i], &quicfunc0, RISING); - break; - case tPIN5: - PCintPort::attachInterrupt(pins[i], &quicfunc1, CHANGE); - break; - case tPIN6: - PCintPort::attachInterrupt(pins[i], &quicfunc2, FALLING); - break; - } - } - //Serial.println(printBuffer.getCapacity(), DEC); - //Serial.println("*---------------------------------------*"); - Serial.print("*---*"); - delay(250); - begintime=millis(); -} - -void loop() { - now=millis(); - uint8_t count; - char outChar; - // uint8_t bufsize; - //if (printBuffer.getSize() != 0) { Serial.print("SZ:"); Serial.println (printBuffer.getSize(), DEC); }; - //bufsize=printBuffer.getSize(); - //if (bufsize > 0) { Serial.print("S:"); Serial.println(bufsize); } - while ((outChar=(char)printBuffer.get()) != 0) Serial.print(outChar); - if ((now - begintime) > 1000) { - Serial.print("."); - if (printBuffer.checkError()) { - Serial.println("NOTICE: Some output lost due to filled buffer."); - } - for (i=0; i < 20; i++) { - if (interrupt_count[i] != 0) { - count=interrupt_count[i]; - interrupt_count[i]=0; - Serial.print("Count for pin "); - if (i < 14) { - Serial.print("D"); - Serial.print(i, DEC); - } else { - Serial.print("A"); - Serial.print(i-14, DEC); - } - Serial.print(" is "); - Serial.println(count, DEC); - } - } - begintime=millis(); - } -} - diff --git a/libraries/PinChangeInt/Examples/PinChangeIntTest2/PinChangeIntTest2.ino b/libraries/PinChangeInt/Examples/PinChangeIntTest2/PinChangeIntTest2.ino deleted file mode 100644 index aec34c4..0000000 --- a/libraries/PinChangeInt/Examples/PinChangeIntTest2/PinChangeIntTest2.ino +++ /dev/null @@ -1,275 +0,0 @@ -//#define DISABLE_PCINT_MULTI_SERVICE -#define PINMODE -#define FLASH -#include -#include -#include - -// This example demonstrates a configuration of 6 interrupting pins and 3 interrupt functions. -// A variety of interrupting pins have been chosen, so as to test all PORTs on the Arduino. -// The pins are as follows: -#define tPIN1 2 // port D -#define tPIN2 3 -#define tPIN3 11 // Port B -#define tPIN4 12 -#define tPIN5 A3 // Port C, also can be given as "17" -#define tPIN6 A4 // starts and stops the count - -uint8_t pins[6]={ tPIN1, tPIN2, tPIN3, tPIN4, tPIN5, tPIN6 }; -uint8_t ports[6]={ 0, 0, 0, 0, 0, 0 }; - -uint8_t latest_interrupted_pin; -uint8_t interrupt_count[20]={0}; // 20 possible arduino pins -uint8_t port; -uint8_t mode; - -ByteBuffer printBuffer(200); -char charArray[16]; -char numBuffer[5] = { 0, 0, 0, 0, 0 }; -uint8_t printFull=0; - -volatile boolean start=0; -volatile boolean initial=true; -long begintime=0; -long now=0; - -void uint8ToHexString(char *outString, uint8_t theByte) { - outString[0]='0'; outString[1]='x'; - uint8_t hinybble=theByte>>4; - uint8_t lonybble=theByte & 0x0F; - if (hinybble < 0x0a) outString[2]=hinybble+48; - else outString[2]=hinybble+55; - if (lonybble < 0x0a) outString[3]=lonybble+48; - else outString[3]=lonybble+55; - outString[4]=0; -} - -void uint8ToString(char *outString, uint8_t number) { - uint8_t hundreds=0; - uint8_t tens=0; - uint8_t ones=0; - - while (number >= 100 ) { - hundreds++; - number-=100; - } - while (number >= 10 ) { - tens++; - number-=10; - } - ones=number; - ones+=48; - if (hundreds > 0) { hundreds+=48; tens+=48; outString[0]=hundreds; outString[1]=tens; outString[2]=ones; outString[3]=0; } - else if (tens > 0) { tens+=48; outString[0]=tens; outString[1]=ones; outString[2]=0; } - else { outString[0]=ones; outString[1]=0; }; -} - -void showMode() { - switch (mode) { - case FALLING: - printBuffer.putString(getPSTR("-F-")); - break; - case RISING: - printBuffer.putString(getPSTR("+R+")); - break; - case CHANGE: - printBuffer.putString(getPSTR("*C*")); - break; - } -} - -/* -void quicfunc0() { - latest_interrupted_pin=PCintPort::arduinoPin; - mode=PCintPort::pinmode; - showMode(); - if (start==1) { - interrupt_count[latest_interrupted_pin]++; - } - uint8ToString(numBuffer, latest_interrupted_pin); - printBuffer.putString((char *) "f0p"); printBuffer.putString(numBuffer); printBuffer.putString((char *) "-P"); - uint8ToString(numBuffer, digitalPinToPort(latest_interrupted_pin)); - printBuffer.putString(numBuffer); - if (start !=1) printBuffer.putString(getPSTR(" no count")); - printBuffer.putString((char *) "\n"); -}; - -void quicfunc1() { - latest_interrupted_pin=PCintPort::arduinoPin; - mode=PCintPort::pinmode; - showMode(); - if (start==1) { - interrupt_count[latest_interrupted_pin]++; - } - uint8ToString(numBuffer, latest_interrupted_pin); - printBuffer.putString(getPSTR("f1p")); printBuffer.putString(numBuffer); printBuffer.putString((char *) "-P"); - uint8ToString(numBuffer, digitalPinToPort(latest_interrupted_pin)); - printBuffer.putString(numBuffer); - if (start !=1) printBuffer.putString(getPSTR(" (counting off)")); - printBuffer.putString((char *) "\n"); -}; -*/ - -void quicfunc2() { - //*led_port|=led_mask; - //*led_port&=not_led_mask; // 2 micros to here (ie, 2 micros used to push registers and call subroutine) - latest_interrupted_pin=PCintPort::arduinoPin; - mode=PCintPort::pinmode; - showMode(); - *led_port|=led_mask; // 73 micros to get here from above. Used in "Rigol Timing Example" - *led_port&=not_led_mask; - //uint8ToString(numBuffer, PCintPort::s_count); printBuffer.putString(numBuffer); - *led_port|=led_mask; // 73 micros to get here from above. Second pulse in "Rigol Timing Example" - *led_port&=not_led_mask; - printBuffer.putString(getPSTR(" f2: P"));/* - uint8ToHexString(numBuffer, *portInputRegister(3)); printBuffer.putString(numBuffer);// C port - printBuffer.putString(getPSTR(" pin:")); uint8ToString(numBuffer, latest_interrupted_pin); printBuffer.putString(numBuffer); - printBuffer.putString(getPSTR(" c")); uint8ToHexString(numBuffer, PCintPort::curr); printBuffer.putString(numBuffer); - printBuffer.putString(getPSTR(" l")); uint8ToHexString(numBuffer, PCintPort::s_lastPinView); printBuffer.putString(numBuffer); - printBuffer.putString(getPSTR(" r")); uint8ToHexString(numBuffer, PCintPort::s_portRisingPins); printBuffer.putString(numBuffer); - printBuffer.putString(getPSTR(" f")); uint8ToHexString(numBuffer, PCintPort::s_portFallingPins); printBuffer.putString(numBuffer); - printBuffer.putString(getPSTR(" m")); uint8ToHexString(numBuffer, PCintPort::s_pmask); printBuffer.putString(numBuffer); - printBuffer.putString(getPSTR(" P")); printBuffer.put(PCintPort::s_PORT); printBuffer.putString("\r\n"); - printBuffer.putString(getPSTR("cp")); uint8ToHexString(numBuffer, PCintPort::s_changedPins); printBuffer.putString(numBuffer); - printBuffer.putString(getPSTR(" cXORlpv")); uint8ToHexString(numBuffer, PCintPort::s_currXORlastPinView); printBuffer.putString(numBuffer); - printBuffer.putString(getPSTR(" rp_nCurr")); uint8ToHexString(numBuffer, PCintPort::s_portRisingPins_nCurr); printBuffer.putString(numBuffer); - printBuffer.putString(getPSTR(" fp_nNCurr")); uint8ToHexString(numBuffer, PCintPort::s_portFallingPins_nNCurr); printBuffer.putString(numBuffer); - */printBuffer.putString("\r\n"); - if (PCintPort::pcint_multi > 0) { - printBuffer.putString("MULTI!\n"); PCintPort::pcint_multi=0; - } - if (PCintPort::PCIFRbug > 0) { printBuffer.putString("ERROR: BUG- PCIFR should be reset!"); PCintPort::PCIFRbug=0; } - //s_registers, if it existed, could be used to keep a running queue of the latest interrupts that have - //been serviced by the PCint(). But generally I don't think it's necessary for debugging at this point (famous last words?) - /*if (PCintPort::s_count > 2) { - for (uint8_t i=0; i < PCintPort::s_count; i++) { - uint8ToHexString(numBuffer, PCintPort::s_registers[i]); printBuffer.putString(numBuffer); printBuffer.putString(" "); - } - } - PCintPort::s_count=0;*/ - /* - if (start == 1) { - printBuffer.putString(getPSTR("STOP Count off\n")); - printBuffer.putString(getPSTR("Intr OFF: (")); uint8ToString(numBuffer, tPIN1), printBuffer.putString(numBuffer); - printBuffer.putString((char *) " "); uint8ToString(numBuffer, tPIN3), printBuffer.putString(numBuffer); - printBuffer.putString((char *) " "); uint8ToString(numBuffer, tPIN5), printBuffer.putString(numBuffer); - printBuffer.putString((char *) ")\n"); - PCintPort::detachInterrupt(tPIN1); PCintPort::detachInterrupt(tPIN3); PCintPort::detachInterrupt(tPIN5); - start=0; - } else { - start=1; - interrupt_count[latest_interrupted_pin]++; - printBuffer.putString(getPSTR("START! p")); - uint8ToString(numBuffer, latest_interrupted_pin); - printBuffer.putString(numBuffer); printBuffer.putString((char *) "-P"); - // MIKE put the REAL PORT HERE - uint8ToString(numBuffer, digitalPinToPort(latest_interrupted_pin)); - printBuffer.putString(numBuffer); printBuffer.putString((char *) "\n"); - if (! initial) { - PCintPort::attachInterrupt(tPIN1, &quicfunc0, FALLING); - PCintPort::attachInterrupt(tPIN3, &quicfunc0, CHANGE); - PCintPort::attachInterrupt(tPIN5, &quicfunc1, CHANGE); - } else { - initial=false; - } - }*/ -}; - -uint8_t i; -char hexBuffer[5]; -void setup() { - int8_t returncode=1; - Serial.begin(115200); - Serial.println("Test"); - delay(500); - for (i=5; i < 6; i++) { - pinMode(pins[i], INPUT); digitalWrite(pins[i], HIGH); - ports[i]=digitalPinToPort(pins[i]); - switch (pins[i]) { - /*case tPIN1: - #if PCINT_VERSION > 2100 - returncode=PCintPort::attachInterrupt(pins[i], &quicfunc0, FALLING); - #else - PCintPort::attachInterrupt(pins[i], &quicfunc0, FALLING); - #endif - Serial.println(getPSTR("FIRST FAILURE OK.")); - break; - case tPIN3: - #if PCINT_VERSION > 2100 - returncode=PCintPort::attachInterrupt(pins[i], &quicfunc0, CHANGE); - #else - PCintPort::attachInterrupt(pins[i], &quicfunc0, CHANGE); - #endif - break; - case tPIN2: - case tPIN4: - #if PCINT_VERSION > 2100 - returncode=PCintPort::attachInterrupt(pins[i], &quicfunc0, RISING); - #else - PCintPort::attachInterrupt(pins[i], &quicfunc0, RISING); - #endif - break; - case tPIN5: - #if PCINT_VERSION > 2100 - returncode=PCintPort::attachInterrupt(pins[i], &quicfunc1, CHANGE); - #else - PCintPort::attachInterrupt(pins[i], &quicfunc1, CHANGE); - #endif - break;*/ - case tPIN6: - #if PCINT_VERSION > 2100 - returncode=PCintPort::attachInterrupt(pins[i], &quicfunc2, FALLING); - #else - PCintPort::attachInterrupt(pins[i], &quicfunc2, FALLING); - #endif - break; - } - #if PCINT_VERSION > 2100 - Serial.print(getPSTR("setup(): Interrupt attach ")); - if (returncode != 1) Serial.print(getPSTR("unsuccessful ")); - else Serial.print(getPSTR("GOOD ")); - Serial.print(pins[i], DEC); - Serial.print(getPSTR(":pin, code: ")); Serial.println(returncode, DEC); - #endif - } - //Serial.println(printBuffer.getCapacity(), DEC); - //Serial.println("*---------------------------------------*"); - Serial.print("*---*"); - delay(250); - begintime=millis(); -} - -void loop() { - now=millis(); - uint8_t count; - char outChar; - // uint8_t bufsize; - //if (printBuffer.getSize() != 0) { Serial.print("SZ:"); Serial.println (printBuffer.getSize(), DEC); }; - //bufsize=printBuffer.getSize(); - //if (bufsize > 0) { Serial.print("S:"); Serial.println(bufsize); } - while ((outChar=(char)printBuffer.get()) != 0) Serial.print(outChar); - if ((now - begintime) > 1000) { - Serial.print("."); - if (printBuffer.checkError()) { - Serial.println(getPSTR("!Some output lost due to full buffer!")); - } - for (i=0; i < 20; i++) { - if (interrupt_count[i] != 0) { - count=interrupt_count[i]; - interrupt_count[i]=0; - Serial.print(getPSTR("Count for pin ")); - if (i < 14) { - Serial.print("D"); - Serial.print(i, DEC); - } else { - Serial.print("A"); - Serial.print(i-14, DEC); - } - Serial.print(" is "); - Serial.println(count, DEC); - } - } - begintime=millis(); - } -} - diff --git a/libraries/PinChangeInt/PinChangeInt.h b/libraries/PinChangeInt/PinChangeInt.h deleted file mode 100644 index 6ccd1df..0000000 --- a/libraries/PinChangeInt/PinChangeInt.h +++ /dev/null @@ -1,624 +0,0 @@ -// We use 4-character tabstops, so IN VIM: :set ts=4 and :set sw=4 -// ...that's: ESCAPE key, colon key, then "s-e-t SPACE key t-s-=-4" -// -/* - * This is the PinChangeInt library for the Arduino. - - See google code project for latest, bugs and info http://code.google.com/p/arduino-pinchangeint/ - For more information Refer to avr-gcc header files, arduino source and atmega datasheet. - - This library was inspired by and derived from "johnboiles" (it seems) - PCInt Arduino Playground example here: http://www.arduino.cc/playground/Main/PcInt - If you are the original author, please let us know at the google code page - - It provides an extension to the interrupt support for arduino by - adding pin change interrupts, giving a way for users to have - interrupts drive off of any pin. - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - (the file gpl.txt is included with the library's zip package) -*/ -//-------- define these in your sketch, if applicable ---------------------------------------------------------- -//-------- These must go in your sketch ahead of the #include statement ----------------------- -// You can reduce the memory footprint of this handler by declaring that there will be no pin change interrupts -// on any one or two of the three ports. If only a single port remains, the handler will be declared inline -// reducing the size and latency of the handler. -// #define NO_PORTB_PINCHANGES // to indicate that port b will not be used for pin change interrupts -// #define NO_PORTC_PINCHANGES // to indicate that port c will not be used for pin change interrupts -// #define NO_PORTD_PINCHANGES // to indicate that port d will not be used for pin change interrupts -// --- Mega support --- -// #define NO_PORTB_PINCHANGES // to indicate that port b will not be used for pin change interrupts -// #define NO_PORTJ_PINCHANGES // to indicate that port c will not be used for pin change interrupts -// #define NO_PORTK_PINCHANGES // to indicate that port d will not be used for pin change interrupts -// In the Mega, there is no Port C, no Port D. Instead, you get Port J and Port K. Port B remains. -// Port J, however, is practically useless because there is only 1 pin available for interrupts. Most -// of the Port J pins are not even connected to a header connection. // "Mega Support" notes -// --- Sanguino, Mioduino support --- -// #define NO_PORTA_PINCHANGES // to indicate that port a will not be used for pin change interrupts -// -------------------- -// -// Other preprocessor directives... -// You can reduce the code size by 20-50 bytes, and you can speed up the interrupt routine -// slightly by declaring that you don't care if the static variables PCintPort::pinState and/or -// PCintPort::arduinoPin are set and made available to your interrupt routine. -// #define NO_PIN_STATE // to indicate that you don't need the pinState -// #define NO_PIN_NUMBER // to indicate that you don't need the arduinoPin -// #define DISABLE_PCINT_MULTI_SERVICE // to limit the handler to servicing a single interrupt per invocation. -// #define GET_PCINT_VERSION // to enable the uint16_t getPCIintVersion () function. -// The following is intended for testing purposes. If defined, then a whole host of static variables can be read -// in your interrupt subroutine. It is not defined by default, and you DO NOT want to define this in -// Production code!: -// #define PINMODE -//-------- define the above in your sketch, if applicable ------------------------------------------------------ - -/* - PinChangeInt.h - ---- VERSIONS --- (NOTE TO SELF: Update the PCINT_VERSION define, below) ----------------- -Version 2.19 (beta) Tue Nov 20 07:33:37 CST 2012 -Version 2.17 (beta) Sat Nov 17 09:46:50 CST 2012 -Version 2.11 (beta) Mon Nov 12 09:33:06 CST 2012 - - Version 2.01 (beta) Thu Jun 28 12:35:48 CDT 2012 - - Version 1.72 Wed Mar 14 18:57:55 CDT 2012 - - Version 1.71beta Sat Mar 10 12:57:05 CST 2012 - - Version 1.6beta Fri Feb 10 08:48:35 CST 2012 - - Version 1.51 Sun Feb 5 23:28:02 CST 2012 - - Version 1.5 Thu Feb 2 18:09:49 CST 2012 - - Version 1.4 Tue Jan 10 09:41:14 CST 2012 - - Version 1.3 Sat Dec 3 22:56:20 CST 2011 - - Version 1.2 Sat Dec 3 Sat Dec 3 09:15:52 CST 2011 - - Version 1.1 Sat Dec 3 00:06:03 CST 2011 - */ - -#ifndef PinChangeInt_h -#define PinChangeInt_h - -#define PCINT_VERSION 2190 // This number MUST agree with the version number, above. - -#include "stddef.h" - -// Thanks to Maurice Beelen, nms277, Akesson Karlpetter, and Orly Andico for these fixes. -#if defined(ARDUINO) && ARDUINO >= 100 - #include - #include - #include // cby and sbi defined here -#else - #include - #include - #ifndef LIBCALL_PINCHANGEINT - #include "../cppfix/cppfix.h" - #endif -#endif - - -#undef DEBUG - -/* -* Theory: all IO pins on Atmega168 are covered by Pin Change Interrupts. -* The PCINT corresponding to the pin must be enabled and masked, and -* an ISR routine provided. Since PCINTs are per port, not per pin, the ISR -* must use some logic to actually implement a per-pin interrupt service. -*/ - -/* Pin to interrupt map: -* D0-D7 = PCINT 16-23 = PCIR2 = PD = PCIE2 = pcmsk2 -* D8-D13 = PCINT 0-5 = PCIR0 = PB = PCIE0 = pcmsk0 -* A0-A5 (D14-D19) = PCINT 8-13 = PCIR1 = PC = PCIE1 = pcmsk1 -*/ - -#undef INLINE_PCINT -#define INLINE_PCINT -// Thanks to cserveny...@gmail.com for MEGA support! -#if defined __AVR_ATmega2560__ || defined __AVR_ATmega1280__ || defined __AVR_ATmega1281__ || defined __AVR_ATmega2561__ || defined __AVR_ATmega640__ - #define __USE_PORT_JK - // Mega does not have PORTA, C or D - #define NO_PORTA_PINCHANGES - #define NO_PORTC_PINCHANGES - #define NO_PORTD_PINCHANGES - #if ((defined(NO_PORTB_PINCHANGES) && defined(NO_PORTJ_PINCHANGES)) || \ - (defined(NO_PORTJ_PINCHANGES) && defined(NO_PORTK_PINCHANGES)) || \ - (defined(NO_PORTK_PINCHANGES) && defined(NO_PORTB_PINCHANGES))) - #define INLINE_PCINT inline - #endif -#else - #if defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) - #ifndef NO_PORTA_PINCHANGES - #define __USE_PORT_A - #endif - #else - #define NO_PORTA_PINCHANGES - #endif - // if defined only D .OR. only C .OR. only B .OR. only A, then inline it - #if ( (defined(NO_PORTA_PINCHANGES) && defined(NO_PORTB_PINCHANGES) && defined(NO_PORTC_PINCHANGES)) || \ - (defined(NO_PORTA_PINCHANGES) && defined(NO_PORTB_PINCHANGES) && defined(NO_PORTD_PINCHANGES)) || \ - (defined(NO_PORTA_PINCHANGES) && defined(NO_PORTC_PINCHANGES) && defined(NO_PORTD_PINCHANGES)) || \ - (defined(NO_PORTB_PINCHANGES) && defined(NO_PORTC_PINCHANGES) && defined(NO_PORTD_PINCHANGES)) ) - #define INLINE_PCINT inline - #endif -#endif - -// Provide drop in compatibility with johnboiles PCInt project at -// http://www.arduino.cc/playground/Main/PcInt -#define PCdetachInterrupt(pin) PCintPort::detachInterrupt(pin) -#define PCattachInterrupt(pin,userFunc,mode) PCintPort::attachInterrupt(pin, userFunc,mode) -#define PCgetArduinoPin() PCintPort::getArduinoPin() - - -typedef void (*PCIntvoidFuncPtr)(void); - -class PCintPort { -public: - PCintPort(int index,int pcindex, volatile uint8_t& maskReg) : - portInputReg(*portInputRegister(index)), - portPCMask(maskReg), - PCICRbit(1 << pcindex), - portRisingPins(0), - portFallingPins(0), - firstPin(NULL) -#ifdef PINMODE - ,intrCount(0) -#endif - { - #ifdef FLASH - ledsetup(); - #endif - } - volatile uint8_t& portInputReg; - static int8_t attachInterrupt(uint8_t pin, PCIntvoidFuncPtr userFunc, int mode); - static void detachInterrupt(uint8_t pin); - INLINE_PCINT void PCint(); - static volatile uint8_t curr; - #ifndef NO_PIN_NUMBER - static volatile uint8_t arduinoPin; - #endif - #ifndef NO_PIN_STATE - static volatile uint8_t pinState; - #endif - #ifdef PINMODE - static volatile uint8_t pinmode; - static volatile uint8_t s_portRisingPins; - static volatile uint8_t s_portFallingPins; - static volatile uint8_t s_lastPinView; - static volatile uint8_t s_pmask; - static volatile char s_PORT; - static volatile uint8_t s_changedPins; - static volatile uint8_t s_portRisingPins_nCurr; - static volatile uint8_t s_portFallingPins_nNCurr; - static volatile uint8_t s_currXORlastPinView; - volatile uint8_t intrCount; - static volatile uint8_t s_count; - static volatile uint8_t pcint_multi; - static volatile uint8_t PCIFRbug; - #endif - #ifdef FLASH - static void ledsetup(void); - #endif - -protected: - class PCintPin { - public: - PCintPin() : - PCintFunc((PCIntvoidFuncPtr)NULL), - mode(0) {} - PCIntvoidFuncPtr PCintFunc; - uint8_t mode; - uint8_t mask; - uint8_t arduinoPin; - PCintPin* next; - }; - void enable(PCintPin* pin, PCIntvoidFuncPtr userFunc, uint8_t mode); - int8_t addPin(uint8_t arduinoPin,PCIntvoidFuncPtr userFunc, uint8_t mode); - volatile uint8_t& portPCMask; - const uint8_t PCICRbit; - volatile uint8_t portRisingPins; - volatile uint8_t portFallingPins; - volatile uint8_t lastPinView; - PCintPin* firstPin; - PCintPin thePin; -}; - -#ifndef LIBCALL_PINCHANGEINT // LIBCALL_PINCHANGEINT *********************************************** -volatile uint8_t PCintPort::curr=0; -#ifndef NO_PIN_NUMBER -volatile uint8_t PCintPort::arduinoPin=0; -#endif -#ifndef NO_PIN_STATE -volatile uint8_t PCintPort::pinState=0; -#endif -#ifdef PINMODE -volatile uint8_t PCintPort::pinmode=0; -volatile uint8_t PCintPort::s_portRisingPins=0; -volatile uint8_t PCintPort::s_portFallingPins=0; -volatile uint8_t PCintPort::s_lastPinView=0; -volatile uint8_t PCintPort::s_pmask=0; -volatile char PCintPort::s_PORT='x'; -volatile uint8_t PCintPort::s_changedPins=0; -volatile uint8_t PCintPort::s_portRisingPins_nCurr=0; -volatile uint8_t PCintPort::s_portFallingPins_nNCurr=0; -volatile uint8_t PCintPort::s_currXORlastPinView=0; -volatile uint8_t PCintPort::s_count=0; -volatile uint8_t PCintPort::pcint_multi=0; -volatile uint8_t PCintPort::PCIFRbug=0; -#endif - -#ifdef FLASH -#define PINLED 13 -volatile uint8_t *led_port; -uint8_t led_mask; -uint8_t not_led_mask; -boolean ledsetup_run=false; -void PCintPort::ledsetup(void) { - if (! ledsetup_run) { - led_port=portOutputRegister(digitalPinToPort(PINLED)); - led_mask=digitalPinToBitMask(PINLED); - not_led_mask=led_mask^0xFF; - pinMode(PINLED, OUTPUT); digitalWrite(PINLED, LOW); - ledsetup_run=true; - } -}; -#endif - - -// ATMEGA 644 -// -#if defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) // Sanguino, Mosquino uino bobino bonanafannafofino, me my momino... - -#ifndef NO_PORTA_PINCHANGES -PCintPort portA=PCintPort(1, 0,PCMSK0); // port PB==2 (from Arduino.h, Arduino version 1.0) -#endif -#ifndef NO_PORTB_PINCHANGES -PCintPort portB=PCintPort(2, 1,PCMSK1); // port PB==2 (from Arduino.h, Arduino version 1.0) -#endif -#ifndef NO_PORTC_PINCHANGES -PCintPort portC=PCintPort(3, 2,PCMSK2); // port PC==3 (also in pins_arduino.c, Arduino version 022) -#endif -#ifndef NO_PORTD_PINCHANGES -PCintPort portD=PCintPort(4, 3,PCMSK3); // port PD==4 -#endif - -#else // others - -#ifndef NO_PORTB_PINCHANGES -PCintPort portB=PCintPort(2, 0,PCMSK0); // port PB==2 (from Arduino.h, Arduino version 1.0) -#endif -#ifndef NO_PORTC_PINCHANGES // note: no PORTC on MEGA -PCintPort portC=PCintPort(3, 1,PCMSK1); // port PC==3 (also in pins_arduino.c, Arduino version 022) -#endif -#ifndef NO_PORTD_PINCHANGES // note: no PORTD on MEGA -PCintPort portD=PCintPort(4, 2,PCMSK2); // port PD==4 -#endif - -#endif // defined __AVR_ATmega644__ - -#ifdef __USE_PORT_JK -#ifndef NO_PORTJ_PINCHANGES -PCintPort portJ=PCintPort(10,1,PCMSK1); // port PJ==10 -#endif -#ifndef NO_PORTK_PINCHANGES -PCintPort portK=PCintPort(11,2,PCMSK2); // port PK==11 -#endif -#endif // USE_PORT_JK - -static PCintPort *lookupPortNumToPort( int portNum ) { - PCintPort *port = NULL; - - switch (portNum) { -#ifndef NO_PORTA_PINCHANGES - case 1: - port=&portA; - break; -#endif -#ifndef NO_PORTB_PINCHANGES - case 2: - port=&portB; - break; -#endif -#ifndef NO_PORTC_PINCHANGES - case 3: - port=&portC; - break; -#endif -#ifndef NO_PORTD_PINCHANGES - case 4: - port=&portD; - break; -#endif -#ifdef __USE_PORT_JK - -#ifndef NO_PORTJ_PINCHANGES - case 10: - port=&portJ; - break; -#endif - -#ifndef NO_PORTK_PINCHANGES - case 11: - port=&portK; - break; -#endif - -#endif - } - - return port; -} - - -void PCintPort::enable(PCintPin* p, PCIntvoidFuncPtr userFunc, uint8_t mode) { - // Enable the pin for interrupts by adding to the PCMSKx register. - // ...The final steps; at this point the interrupt is enabled on this pin. - p->mode=mode; - p->PCintFunc=userFunc; - portPCMask |= p->mask; - if ((p->mode == RISING) || (p->mode == CHANGE)) portRisingPins |= p->mask; - if ((p->mode == FALLING) || (p->mode == CHANGE)) portFallingPins |= p->mask; - PCICR |= PCICRbit; -} - -int8_t PCintPort::addPin(uint8_t arduinoPin, PCIntvoidFuncPtr userFunc, uint8_t mode) -{ - PCintPin* tmp; - - // Add to linked list, starting with firstPin. If pin already exists, just enable. - if (firstPin != NULL) { - tmp=firstPin; - do { - if (tmp->arduinoPin == arduinoPin) { enable(tmp, userFunc, mode); return(0); } - if (tmp->next == NULL) break; - tmp=tmp->next; - } while (true); - } - - // Create pin p: fill in the data. NOTE: MySensors only support one pin due to size constraints - // TODO: Figure out a better way to solve this without relying on new/malloc - PCintPin* p = &thePin; //PCintPin* p=new PCintPin; - if (p == NULL) return(-1); - p->arduinoPin=arduinoPin; - p->mode = mode; - p->next=NULL; - p->mask = digitalPinToBitMask(arduinoPin); // the mask - - if (firstPin == NULL) firstPin=p; - else tmp->next=p; - -#ifdef DEBUG - Serial.print("addPin. pin given: "); Serial.print(arduinoPin, DEC); - int addr = (int) p; - Serial.print(" instance addr: "); Serial.println(addr, HEX); - Serial.print("userFunc addr: "); Serial.println((int)p->PCintFunc, HEX); -#endif - - enable(p, userFunc, mode); -#ifdef DEBUG - Serial.print("addPin. pin given: "); Serial.print(arduinoPin, DEC), Serial.print (" pin stored: "); - int addr = (int) p; - Serial.print(" instance addr: "); Serial.println(addr, HEX); -#endif - return(1); -} - -/* - * attach an interrupt to a specific pin using pin change interrupts. - */ -int8_t PCintPort::attachInterrupt(uint8_t arduinoPin, PCIntvoidFuncPtr userFunc, int mode) -{ - PCintPort *port; - uint8_t portNum = digitalPinToPort(arduinoPin); - if ((portNum == NOT_A_PORT) || (userFunc == NULL)) return(-1); - - port=lookupPortNumToPort(portNum); - // Added by GreyGnome... must set the initial value of lastPinView for it to be correct on the 1st interrupt. - // ...but even then, how do you define "correct"? Ultimately, the user must specify (not provisioned for yet). - port->lastPinView=port->portInputReg; - -#ifdef DEBUG - Serial.print("attachInterrupt FUNC: "); Serial.println(arduinoPin, DEC); -#endif - // map pin to PCIR register - return(port->addPin(arduinoPin,userFunc,mode)); -} - -void PCintPort::detachInterrupt(uint8_t arduinoPin) -{ - PCintPort *port; - PCintPin* current; - uint8_t mask; -#ifdef DEBUG - Serial.print("detachInterrupt: "); Serial.println(arduinoPin, DEC); -#endif - uint8_t portNum = digitalPinToPort(arduinoPin); - if (portNum == NOT_A_PORT) return; - port=lookupPortNumToPort(portNum); - mask=digitalPinToBitMask(arduinoPin); - current=port->firstPin; - //PCintPin* prev=NULL; - while (current) { - if (current->mask == mask) { // found the target - uint8_t oldSREG = SREG; - cli(); // disable interrupts - port->portPCMask &= ~mask; // disable the mask entry. - if (port->portPCMask == 0) PCICR &= ~(port->PCICRbit); - port->portRisingPins &= ~current->mask; port->portFallingPins &= ~current->mask; - // Link the previous' next to the found next. Then remove the found. - //if (prev != NULL) prev->next=current->next; // linked list skips over current. - //else firstPin=current->next; // at the first pin; save the new first pin - SREG = oldSREG; // Restore register; reenables interrupts - return; - } - //prev=current; - current=current->next; - } -} - -// common code for isr handler. "port" is the PCINT number. -// there isn't really a good way to back-map ports and masks to pins. -void PCintPort::PCint() { - uint8_t thisChangedPin; //MIKE - (void)thisChangedPin; - - #ifdef FLASH - if (*led_port & led_mask) *led_port&=not_led_mask; - else *led_port|=led_mask; - #endif - #ifndef DISABLE_PCINT_MULTI_SERVICE - uint8_t pcifr; - while (true) { - #endif - // get the pin states for the indicated port. - #ifdef PINMODE - PCintPort::s_lastPinView=lastPinView; - intrCount++; - PCintPort::s_count=intrCount; - #endif - // OLD v. 2.01 technique: Test 1: 3163; Test 7: 3993 - // From robtillaart online: ------------ (starting v. 2.11beta) - // uint8_t changedPins = PCintPort::curr ^ lastPinView; - // lastPinView = PCintPort::curr; - // uint8_t fastMask = changedPins & ((portRisingPins & PCintPort::curr ) | ( portFallingPins & ~PCintPort::curr )); - // NEW v. 2.11 technique: Test 1: 3270 Test 7: 3987 - // ------------------------------------- - // was: uint8_t changedPins = PCintPort::curr ^ lastPinView; - // makes test 6 of the PinChangeIntSpeedTest go from 3867 to 3923. Not good. - uint8_t changedPins = (PCintPort::curr ^ lastPinView) & - ((portRisingPins & PCintPort::curr ) | ( portFallingPins & ~PCintPort::curr )); - - #ifdef PINMODE - PCintPort::s_currXORlastPinView=PCintPort::curr ^ lastPinView; - PCintPort::s_portRisingPins_nCurr=portRisingPins & PCintPort::curr; - PCintPort::s_portFallingPins_nNCurr=portFallingPins & ~PCintPort::curr; - #endif - lastPinView = PCintPort::curr; - - PCintPin* p = firstPin; - while (p) { - // Trigger interrupt if the bit is high and it's set to trigger on mode RISING or CHANGE - // Trigger interrupt if the bit is low and it's set to trigger on mode FALLING or CHANGE - thisChangedPin=p->mask & changedPins; // PinChangeIntSpeedTest makes this 3673... weird. But GOOD!!! - if (p->mask & changedPins) { - #ifndef NO_PIN_STATE - PCintPort::pinState=PCintPort::curr & p->mask ? HIGH : LOW; - #endif - #ifndef NO_PIN_NUMBER - PCintPort::arduinoPin=p->arduinoPin; - #endif - #ifdef PINMODE - PCintPort::pinmode=p->mode; - PCintPort::s_portRisingPins=portRisingPins; - PCintPort::s_portFallingPins=portFallingPins; - PCintPort::s_pmask=p->mask; - PCintPort::s_changedPins=changedPins; - #endif - p->PCintFunc(); - } - p=p->next; - } - #ifndef DISABLE_PCINT_MULTI_SERVICE - pcifr = PCIFR & PCICRbit; - if (pcifr == 0) break; - PCIFR |= PCICRbit; - #ifdef PINMODE - PCintPort::pcint_multi++; - if (PCIFR & PCICRbit) PCintPort::PCIFRbug=1; // PCIFR & PCICRbit should ALWAYS be 0 here! - #endif - PCintPort::curr=portInputReg; - } - #endif -} - -#ifndef NO_PORTA_PINCHANGES -ISR(PCINT0_vect) { - #ifdef PINMODE - PCintPort::s_PORT='A'; - #endif - PCintPort::curr = portA.portInputReg; - portA.PCint(); -} -#define PORTBVECT PCINT1_vect -#define PORTCVECT PCINT2_vect -#define PORTDVECT PCINT3_vect -#else -#define PORTBVECT PCINT0_vect -#define PORTCVECT PCINT1_vect -#define PORTDVECT PCINT2_vect -#endif - -#ifndef NO_PORTB_PINCHANGES -ISR(PORTBVECT) { - #ifdef PINMODE - PCintPort::s_PORT='B'; - #endif - PCintPort::curr = portB.portInputReg; - portB.PCint(); -} -#endif - -#ifndef NO_PORTC_PINCHANGES -ISR(PORTCVECT) { - #ifdef PINMODE - PCintPort::s_PORT='C'; - #endif - PCintPort::curr = portC.portInputReg; - portC.PCint(); -} -#endif - -#ifndef NO_PORTD_PINCHANGES -ISR(PORTDVECT){ - #ifdef PINMODE - PCintPort::s_PORT='D'; - #endif - PCintPort::curr = portD.portInputReg; - portD.PCint(); -} -#endif - -#ifdef __USE_PORT_JK -#ifndef NO_PORTJ_PINCHANGES -ISR(PCINT1_vect) { - #ifdef PINMODE - PCintPort::s_PORT='J'; - #endif - PCintPort::curr = portJ.portInputReg; - portJ.PCint(); -} -#endif - -#ifndef NO_PORTK_PINCHANGES -ISR(PCINT2_vect){ - #ifdef PINMODE - PCintPort::s_PORT='K'; - #endif - PCintPort::curr = portK.portInputReg; - portK.PCint(); -} -#endif - -#endif // __USE_PORT_JK - -#ifdef GET_PCINT_VERSION -uint16_t getPCIntVersion () { - return ((uint16_t) PCINT_VERSION); -} -#endif // GET_PCINT_VERSION -#endif // #ifndef LIBCALL_PINCHANGEINT ************************************************************* -#endif // #ifndef PinChangeInt_h ******************************************************************* diff --git a/libraries/PinChangeInt/RELEASE_NOTES b/libraries/PinChangeInt/RELEASE_NOTES deleted file mode 100644 index 635e4f8..0000000 --- a/libraries/PinChangeInt/RELEASE_NOTES +++ /dev/null @@ -1,332 +0,0 @@ -****************************************************************************** - - PinChangeInt - ---- RELEASE NOTES --- - -Version 2.19 (beta) Tue Nov 20 07:33:37 CST 2012 -SANGUINO SUPPORT! ...And Mioduino! -...The ATmega644 chip is so cool, how can I not? 4 full ports of Pin Change Interrupt bliss! 32 i/o pins! 64k Flash! 4k RAM! Well I wish I had one. That said, Sanguino users, PLEASE send in your bug or bliss reports! Your interrupt-loving brethren and sistren are depending on you, so I can assure everyone that my changes work on that platform. Thanks. - -Modified the addPin() method to save 12 bytes; thanks again robtilllart! - if (firstPin != NULL) { - tmp=firstPin; - do { - if (tmp->arduinoPin == arduinoPin) { enable(tmp, userFunc, mode); return(0); } - if (tmp->next == NULL) break; - tmp=tmp->next; - } while (true); - } -Also changed the goto in the PCint() loop to be a while/break combination. No change to -code speed, but it looks better and passes the cleanliness "gut check". - -Includes PinChangeIntTest 1.5 sketch. - -...Ooops! Forgot the GetPSTR library, needed for the PinChangeIntTest code! Now it's included. -****************************************************************************** -Version 2.17 (beta) Sat Nov 17 09:46:50 CST 2012 -Another bugfix in the PCINT_MULTI_SERVICE section. I was using sbi(PCIFR, PCICRbit); -I didn't realize that was for I/O ports, and not ATmega registers. -But according to "deprecated.h", -"These macros became obsolete, as reading and writing IO ports can - be done by simply using the IO port name in an expression, and all - bit manipulation (including those on IO ports) can be done using - generic C bit manipulation operators." -So now I do: - PCIFR |= PCICRbit; -****************************************************************************** -Version 2.15 (beta) Sat Nov 17 01:17:44 CST 2012 -Fixed it so that attachInterrupt() will now follow your changes to the user function, -as well as the mode. detachInterrupt() still does not delete the PCintPin object but -at least you can detach and reattach at will, using different modes (RISING, FALLING, -CHANGE) and different functions as you wish. - -****************************************************************************** -Version 2.13 (beta) Mon Nov 12 09:33:06 CST 2012 -SIGNIFICANT BUGFIX release! Significant changes: -1. PCintPort::curr bug. Interrupts that occur rapidly will likely not get serviced properly by PCint(). -2. PCint() interrupt handler optimization. -3. PCIFR port bit set bug fix. -4. Many static variables added for debugging; used only when #define PINMODE is on. -5. detachInterrupt() no longer does a delete(), since that wasn't working anyway. When you detachInterrupt(), the PORT just disables interrupts for that pin; the PCintPin object remains in memory and in the linked list of pins (possibly slowing down your interrupts a couple of micros). You can reenable a detached interrupt- but you must do it within the PinChangeInt library (would anyone ever enable an interrupt on a pin, then disable it, then have need to reenable it but not using the library?). -6. attachInterrupt() now returns a uint8_t value: 1 on successful attach, 0 on successful attach but using an already-enabled pin, and -1 if the new() operator failed to create a PCintPin object. -Also, modified these release notes. - -Details: - -Uncovered a nasty bug, thanks to robtillaart on the Arduino Forums and Andre' Franken who posted to the PinChangeInt groups. This bug was introduced by me when I assigned PCintPort::curr early in the interrupt handler: -ISR(PCINT0_vect) { - PCintPort::curr = portB.portInputReg; - portB.PCint(); -} -Later, in the interrupt handler PCint(), we loop as long as PCIFR indicates a new interrupt wants to be triggered, provided DISABLE_PCINT_MULTI_SERVICE is not defined (it is not by default): -#ifndef DISABLE_PCINT_MULTI_SERVICE - pcifr = PCIFR & PCICRbit; - PCIFR = pcifr; // clear the interrupt if we will process it (no effect if bit is zero) -} while(pcifr); -#endif -...Well. Problem is, if a pin pops up and causes the PCIFR to change, we have to reread the port and look at how it is now! I wasn't doing that before, so if a new interrupt appeared while I was still servicing the old one, odd behavior would take place. For example, an interrupt would register but then the userFunc would not be called upon to service it. The code needs to be: - pcifr = PCIFR & PCICRbit; - PCIFR = pcifr; // clear the interrupt if we will process it (no effect if bit is zero) - PCintPort::curr=portInputReg; // ...Fixed in 2.11beta. -} while(pcifr); - -Also, made the interrupt handler even faster with an optimization from robtillaart to take out the checks for changed pins from the while() loop that steps through the pins: -uint8_t changedPins = (PCintPort::curr ^ lastPinView) & - ((portRisingPins & PCintPort::curr ) | ( portFallingPins & ~PCintPort::curr )); - -...This speedup is offset by more changes in the PCint() handler, which now looks like the following; there are two bug fixes: ----------------------------- -FIX 1: sbi(PCIFR, PCICRbit); -FIX 2: ...the aforementioned PCintPort::curr=portInputReg; -Here's the new code: ----------------------------- - #ifndef DISABLE_PCINT_MULTI_SERVICE - pcifr = PCIFR & PCICRbit; - if (pcifr) { - //if (PCIFR & PCICRbit) { // believe it or not, this adds .6 micros - sbi(PCIFR, PCICRbit); // This was a BUG: PCIFR = pcifr ...And here is the fix. - #ifdef PINMODE - PCintPort::pcint_multi++; - if (PCIFR & PCICRbit) PCintPort::PCIFRbug=1; // PCIFR & PCICRbit should ALWAYS be 0 here! - #endif - PCintPort::curr=portInputReg; // ...Fixed in 2.11beta. - goto loop; // A goto!!! Don't want to look at the portInputReg gratuitously, so the while() will not do. - } - #endif - -Also I added a lot of variables for debugging when PINMODE is defined, for routing out nasty bugs. I may need them in the future... :-( - -Finally, I am not putting newlines in this commentary so I can make it easier to paste online. - -****************************************************************************** -Version 2.11 (beta) Mon Nov 12 09:33:06 CST 2012 -See version 2.13 (beta) above. No change other than tidying up the release notes. -****************************************************************************** - Version 2.01 (beta) Thu Jun 28 12:35:48 CDT 2012 - ...Wow, Version 2! What? Why? - Modified the way that the pin is tested inside the interrupt subroutine (ISR) PCintPort::PCint(), - to make the interrupt quicker and slightly reduce the memory footprint. The interrupt's time is - reduced by 2 microseconds or about 7%. Instead of using the mode variable, two bitmasks are maintained - for each port. One bitmask contains all the pins that are configured to work on RISING signals, the other - on FALLING signals. A pin configured to work on CHANGE signals will appear in both bitmasks of the port. - Then, the test for a change goes like this: - if (thisChangedPin) { - if ((thisChangedPin & portRisingPins & PCintPort::curr ) || - (thisChangedPin & portFallingPins & ~PCintPort::curr )) { - where portRisingPins is the bitmask for the pins configured to interrupt on RISING signals, and - portFallingPins is the bitmask for the pins configured to interrupt on FALLING signals. Each port includes - these two bitmask variables. - - This is a significant change to some core functionality to the library, and it saves an appreciable amount - of time (2 out of 36 or so micros). Hence, the 2.00 designation. - - Tue Jun 26 12:42:20 CDT 2012 - I was officially given permission to use the PCint library: - - Re: PCint library - « Sent to: GreyGnome on: Today at 08:10:33 AM » - « You have forwarded or responded to this message. » - Quote Reply Remove - HI, - Yeah, I wrote the original PCint library. It was a bit of a hack and the new one has better features. - I intended the code to be freely usable. Didn't really think about a license. Feel free to use it in - your code: I hereby grant you permission. - - I'll investigate the MIT license, and see if it is appropriate. - Chris J. Kiick - Robot builder and all around geek. - - Version 1.81 (beta) Tue Jun 19 07:29:08 CDT 2012 - Created the getPCIntVersion function, and its associated GET_PCINT_VERSION preprocessor macro. The version - is a 16-bit int, therefore versions are represented as a 4-digit integer. 1810, then, is the first beta - release of 1.81x series. 1811 would be a bugfix of 1.810. 1820 would be the production release. - - Reversed the order of this list, so the most recent notes come first. - - Made some variables "volatile", because they are changed in the interrupt code. Thanks, Tony Cappellini! - - Added support for the Arduino Mega! Thanks to cserveny...@gmail.com! - NOTE: I don't have a Mega, so I rely on you to give me error (or working) reports! - To sum it up for the Mega: No Port C, no Port D. Instead, you get Port J and Port K. Port B remains. - Port J, however, is practically useless because there is only 1 pin available for interrupts. - Most of the Port J pins are not even connected to a header connector. Caveat Programmer. - - Created a function to report the version of this code. Put this #define ahead of the #include of this file, - in your sketch: - #define GET_PCINT_VERSION - Then you can call - uint16_t getPCIntVersion (); - and it will return a 16-bit integer representation of the version of this library. That is, version 1.73beta - will be reported as "1730". 1.74, then, will return "1740". And so on, for whatever version of the library - this happens to be. The odd number in the 10's position will indicate a beta version, as per usual, and the - number in the 1s place will indicate the beta revision (bugs may necessitate a 1.731, 1.732, etc.). - - Here are some of his notes based on his changes: - Mega and friends are using port B, J and K for interrupts. B is working without any modifications. - - J is mostly useless, because of the hardware UART. I was not able to get pin change notifications from - the TX pin (14), so only 15 left. All other (PORT J) pins are not connected on the Arduino boards. - - K controls Arduino pin A8-A15, working fine. - - 328/168 boards use C and D. So in case the lib is compiled with Mega target, the C and D will be - disabled. Also you cannot see port J/K with other targets. For J and K new flags introduced: - NO_PORTJ_PINCHANGES and NO_PORTK_PINCHANGES. - Maybe we should have PORTJ_PINCHANGES to enable PJ, because they will be most likely unused. - - Enjoy! - - Note: To remain consistent, I have not included PORTJ_PINCHANGES. All ports behave the same, - no matter how trivial those ports may seem... no surprises... - - Version 1.72 Wed Mar 14 18:57:55 CDT 2012 - Release. - - Version 1.71beta Sat Mar 10 12:57:05 CST 2012 - Code reordering: Starting in version 1.3 of this library, I put the code that enables - interrupts for the given pin, and the code that enables Pin Change Interrupts, ahead of actually - setting the user's function for the pin. Thus in the small interval between turning on the - interrupts and actually creating a valid link to an interrupt handler, it is possible to get an - interrupt. At that point the value of the pointer is 0, so this means that the Arduino - will start over again from memory location 0- just as if you'd pressed the reset button. Oops! - - I corrected it so the code now operates in the proper order. - (EDITORIAL NOTE: If you want to really learn something, teach it!) - - Minor code clean-up: All references to PCintPort::curr are now explicit. This changes the compiled - hex code not one whit. I just sleep better at night. - - Numbering: Changed the numbering scheme. Beta versions will end with an odd number in the hundredths - place- because they may be odd- and continue to be marked "beta". I'll just sleep better at night. :-) - - Version 1.70beta Mon Feb 27 07:20:42 CST 2012 - Happy Birthday to me! Happy Birthday tooooo meee! Happy Birthday, Dear Meeeeee-eeeee! - Happy Birthday to me! - - Yes, it is on this auspicious occasion of mine (and Elizabeth Taylor's [R.I.P.]) birthday that I - humbly submit to you, gracious Arduino PinChangeInt user, version 1.70beta of the PinChangeInt - library. I hope you enjoy it. - - New in this release: - The PinChangeIntTest sketch was created, which can be found in the Examples directory. It exercises: - * Two interrupting pins, one on each of the Arduino's PORTs. - * detachInterrupt() (and subsequent attachInterrupt()s). - Hopefully this will help avoid the embarrassing bugs that I have heretofore missed. - - As well, it has come to this author's (GreyGnome) attention that the Serial class in Arduino 1.0 - uses an interrupt that, if you attempt to print from an interrupt (which is what I was doing in my - tests) can easily lock up the Arduino. So I have taken SigurðurOrn's excellent ByteBuffer library - and modified it for my own nefarious purposes. (see http://siggiorn.com/?p=460). The zipfile - comes complete with the ByteBuffer library; see the ByteBuffer/ByteBuffer.h file for a list of - changes, and see the PinChangeIntTest sketch for a usage scenario. Now the (interrupt-less and) - relatively fast operation of filling a circular buffer is used in the interrupt routines. The buffer - is then printed from loop(). - - The library has been modified so it can be used in other libraries, such as my AdaEncoder library - (http://code.google.com/p/adaencoder/). When #include'd by another library you should #define - the LIBCALL_PINCHANGEINT macro. For example: - #ifndef PinChangeInt_h - #define LIBCALL_PINCHANGEINT - #include "../PinChangeInt/PinChangeInt.h" - #endif - This is necessary because the IDE compiles both your sketch and the .cpp file of your library, and - the .h file is included in both places. But since the .h file actually contains the code, any variable - or function definitions would occur twice and cause compilation errors- unless #ifdef'ed out. - - Version 1.6beta Fri Feb 10 08:48:35 CST 2012 - Set the value of the current register settings, first thing in each ISR; e.g., - ISR(PCINT0_vect) { - PCintPort::curr = portB.portInputReg; // version 1.6 - ... - ...instead of at the beginning of the PCintPort::PCint() static method. This means that the port is read - closer to the beginning of the interrupt, and may be slightly more accurate- only by a couple of microseconds, - really, but it's a cheap win. - - Fixed a bug- a BUG!- in the attachInterrupt() and detachInterrupt() methods. I didn't have breaks in my - switch statements! Augh! What am I, a (UNIX) shell programmer? ...Uh, generally, yes... - - Added the PINMODE define and the PCintPort::pinmode variable. - - Version 1.51 Sun Feb 5 23:28:02 CST 2012 - Crap, a bug! Changed line 392 from this: - PCintPort::pinState=curr & changedPins ? HIGH : LOW; - to this: - PCintPort::pinState=curr & p->mask ? HIGH : LOW; - Also added a few lines of (commented-out) debug code. - - Version 1.5 Thu Feb 2 18:09:49 CST 2012 - Added the PCintPort::pinState static variable to allow the programmer to query the state of the pin - at the time of interrupt. - Added two new #defines, NO_PIN_STATE and NO_PIN_NUMBER so as to reduce the code size by 20-50 bytes, - and to speed up the interrupt routine slightly by declaring that you don't care if the static variables - PCintPort::pinState and/or PCintPort::arduinoPin are set and made available to your interrupt routine. - // #define NO_PIN_STATE // to indicate that you don't need the pinState - // #define NO_PIN_NUMBER // to indicate that you don't need the arduinoPin - - Version 1.4 Tue Jan 10 09:41:14 CST 2012 - All the code has been moved into this .h file, so as to allow #define's to work from the user's - sketch. Thanks to Paul Stoffregen from pjrc.com for the inspiration! (Check out his website for - some nice [lots more memory] Arduino-like boards at really good prices. ...This has been an unsolicited - plug. Now back to our regular programming. ...Hehe, "programming", get it?) - - As a result, we no longer use the PinChangeIntConfig.h file. The user must #define things in his/her - sketch. Which is better anyway. - - Removed the pcIntPorts[] array, which created all the ports by default no matter what. Now, only - those ports (PCintPort objects) that you need will get created if you use the NO_PORTx_PINCHANGES #defines. - This saves flash memory, and actually we get a bit of a memory savings anyway even if all the ports are - left enabled. - - The attachInterrupt and detachInterrupt routines were modified to handle the new PCintPort objects. - - Version 1.3 Sat Dec 3 22:56:20 CST 2011 - Significant internal changes: - Tested and modified to work with Arduino 1.0. - - Modified to use the new() operator and symbolic links instead of creating a pre-populated - PCintPins[]. Renamed some variables to simplify or make their meaning more obvious (IMHO anyway). - Modified the PCintPort::PCint() code (ie, the interrupt code) to loop over a linked-list. For - those who love arrays, I have left some code in there that should work to loop over an array - instead. But it is commented out in the release version. - - For Arduino versions prior to 1.0: The new() operator requires the cppfix.h library, which is - included with this package. For Arduino 1.0 and above: new.h comes with the distribution, and - that is #included. - - Version 1.2 Sat Dec 3 Sat Dec 3 09:15:52 CST 2011 - Modified Thu Sep 8 07:33:17 CDT 2011 by GreyGnome. Fixes a bug with the initial port - value. Now it sets the initial value to be the state of the port at the time of - attachInterrupt(). The line is port.PCintLast=port.portInputReg; in attachInterrupt(). - See GreyGnome comment, below. - - Added the "arduinoPin" variable, so the user's function will know exactly which pin on - the Arduino was triggered. - - Version 1.1 Sat Dec 3 00:06:03 CST 2011 - ...updated to fix the "delPin" function as per "pekka"'s bug report. Thanks! - - ---- ^^^ VERSIONS ^^^ (NOTE TO SELF: Update the PCINT_VERSION define, below) ------------- - - See google code project for latest, bugs and info http://code.google.com/p/arduino-pinchangeint/ - For more information Refer to avr-gcc header files, arduino source and atmega datasheet. - - This library was inspired by and derived from "johnboiles" (it seems) - PCInt Arduino Playground example here: http://www.arduino.cc/playground/Main/PcInt - If you are the original author, please let us know at the google code page - - It provides an extension to the interrupt support for arduino by - adding pin change interrupts, giving a way for users to have - interrupts drive off of any pin. - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . diff --git a/libraries/PinChangeInt/gpl.txt b/libraries/PinChangeInt/gpl.txt deleted file mode 100644 index 94a9ed0..0000000 --- a/libraries/PinChangeInt/gpl.txt +++ /dev/null @@ -1,674 +0,0 @@ - GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The GNU General Public License is a free, copyleft license for -software and other kinds of works. - - The licenses for most software and other practical works are designed -to take away your freedom to share and change the works. By contrast, -the GNU General Public License is intended to guarantee your freedom to -share and change all versions of a program--to make sure it remains free -software for all its users. We, the Free Software Foundation, use the -GNU General Public License for most of our software; it applies also to -any other work released this way by its authors. You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -them if you wish), that you receive source code or can get it if you -want it, that you can change the software or use pieces of it in new -free programs, and that you know you can do these things. - - To protect your rights, we need to prevent others from denying you -these rights or asking you to surrender the rights. Therefore, you have -certain responsibilities if you distribute copies of the software, or if -you modify it: responsibilities to respect the freedom of others. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must pass on to the recipients the same -freedoms that you received. You must make sure that they, too, receive -or can get the source code. And you must show them these terms so they -know their rights. - - Developers that use the GNU GPL protect your rights with two steps: -(1) assert copyright on the software, and (2) offer you this License -giving you legal permission to copy, distribute and/or modify it. - - For the developers' and authors' protection, the GPL clearly explains -that there is no warranty for this free software. For both users' and -authors' sake, the GPL requires that modified versions be marked as -changed, so that their problems will not be attributed erroneously to -authors of previous versions. - - Some devices are designed to deny users access to install or run -modified versions of the software inside them, although the manufacturer -can do so. This is fundamentally incompatible with the aim of -protecting users' freedom to change the software. The systematic -pattern of such abuse occurs in the area of products for individuals to -use, which is precisely where it is most unacceptable. Therefore, we -have designed this version of the GPL to prohibit the practice for those -products. If such problems arise substantially in other domains, we -stand ready to extend this provision to those domains in future versions -of the GPL, as needed to protect the freedom of users. - - Finally, every program is threatened constantly by software patents. -States should not allow patents to restrict development and use of -software on general-purpose computers, but in those that do, we wish to -avoid the special danger that patents applied to a free program could -make it effectively proprietary. To prevent this, the GPL assures that -patents cannot be used to render the program non-free. - - The precise terms and conditions for copying, distribution and -modification follow. - - TERMS AND CONDITIONS - - 0. Definitions. - - "This License" refers to version 3 of the GNU General Public License. - - "Copyright" also means copyright-like laws that apply to other kinds of -works, such as semiconductor masks. - - "The Program" refers to any copyrightable work licensed under this -License. Each licensee is addressed as "you". "Licensees" and -"recipients" may be individuals or organizations. - - To "modify" a work means to copy from or adapt all or part of the work -in a fashion requiring copyright permission, other than the making of an -exact copy. The resulting work is called a "modified version" of the -earlier work or a work "based on" the earlier work. - - A "covered work" means either the unmodified Program or a work based -on the Program. - - To "propagate" a work means to do anything with it that, without -permission, would make you directly or secondarily liable for -infringement under applicable copyright law, except executing it on a -computer or modifying a private copy. Propagation includes copying, -distribution (with or without modification), making available to the -public, and in some countries other activities as well. - - To "convey" a work means any kind of propagation that enables other -parties to make or receive copies. Mere interaction with a user through -a computer network, with no transfer of a copy, is not conveying. - - An interactive user interface displays "Appropriate Legal Notices" -to the extent that it includes a convenient and prominently visible -feature that (1) displays an appropriate copyright notice, and (2) -tells the user that there is no warranty for the work (except to the -extent that warranties are provided), that licensees may convey the -work under this License, and how to view a copy of this License. If -the interface presents a list of user commands or options, such as a -menu, a prominent item in the list meets this criterion. - - 1. Source Code. - - The "source code" for a work means the preferred form of the work -for making modifications to it. "Object code" means any non-source -form of a work. - - A "Standard Interface" means an interface that either is an official -standard defined by a recognized standards body, or, in the case of -interfaces specified for a particular programming language, one that -is widely used among developers working in that language. - - The "System Libraries" of an executable work include anything, other -than the work as a whole, that (a) is included in the normal form of -packaging a Major Component, but which is not part of that Major -Component, and (b) serves only to enable use of the work with that -Major Component, or to implement a Standard Interface for which an -implementation is available to the public in source code form. A -"Major Component", in this context, means a major essential component -(kernel, window system, and so on) of the specific operating system -(if any) on which the executable work runs, or a compiler used to -produce the work, or an object code interpreter used to run it. - - The "Corresponding Source" for a work in object code form means all -the source code needed to generate, install, and (for an executable -work) run the object code and to modify the work, including scripts to -control those activities. However, it does not include the work's -System Libraries, or general-purpose tools or generally available free -programs which are used unmodified in performing those activities but -which are not part of the work. For example, Corresponding Source -includes interface definition files associated with source files for -the work, and the source code for shared libraries and dynamically -linked subprograms that the work is specifically designed to require, -such as by intimate data communication or control flow between those -subprograms and other parts of the work. - - The Corresponding Source need not include anything that users -can regenerate automatically from other parts of the Corresponding -Source. - - The Corresponding Source for a work in source code form is that -same work. - - 2. Basic Permissions. - - All rights granted under this License are granted for the term of -copyright on the Program, and are irrevocable provided the stated -conditions are met. This License explicitly affirms your unlimited -permission to run the unmodified Program. The output from running a -covered work is covered by this License only if the output, given its -content, constitutes a covered work. This License acknowledges your -rights of fair use or other equivalent, as provided by copyright law. - - You may make, run and propagate covered works that you do not -convey, without conditions so long as your license otherwise remains -in force. You may convey covered works to others for the sole purpose -of having them make modifications exclusively for you, or provide you -with facilities for running those works, provided that you comply with -the terms of this License in conveying all material for which you do -not control copyright. Those thus making or running the covered works -for you must do so exclusively on your behalf, under your direction -and control, on terms that prohibit them from making any copies of -your copyrighted material outside their relationship with you. - - Conveying under any other circumstances is permitted solely under -the conditions stated below. Sublicensing is not allowed; section 10 -makes it unnecessary. - - 3. Protecting Users' Legal Rights From Anti-Circumvention Law. - - No covered work shall be deemed part of an effective technological -measure under any applicable law fulfilling obligations under article -11 of the WIPO copyright treaty adopted on 20 December 1996, or -similar laws prohibiting or restricting circumvention of such -measures. - - When you convey a covered work, you waive any legal power to forbid -circumvention of technological measures to the extent such circumvention -is effected by exercising rights under this License with respect to -the covered work, and you disclaim any intention to limit operation or -modification of the work as a means of enforcing, against the work's -users, your or third parties' legal rights to forbid circumvention of -technological measures. - - 4. Conveying Verbatim Copies. - - You may convey verbatim copies of the Program's source code as you -receive it, in any medium, provided that you conspicuously and -appropriately publish on each copy an appropriate copyright notice; -keep intact all notices stating that this License and any -non-permissive terms added in accord with section 7 apply to the code; -keep intact all notices of the absence of any warranty; and give all -recipients a copy of this License along with the Program. - - You may charge any price or no price for each copy that you convey, -and you may offer support or warranty protection for a fee. - - 5. Conveying Modified Source Versions. - - You may convey a work based on the Program, or the modifications to -produce it from the Program, in the form of source code under the -terms of section 4, provided that you also meet all of these conditions: - - a) The work must carry prominent notices stating that you modified - it, and giving a relevant date. - - b) The work must carry prominent notices stating that it is - released under this License and any conditions added under section - 7. This requirement modifies the requirement in section 4 to - "keep intact all notices". - - c) You must license the entire work, as a whole, under this - License to anyone who comes into possession of a copy. This - License will therefore apply, along with any applicable section 7 - additional terms, to the whole of the work, and all its parts, - regardless of how they are packaged. This License gives no - permission to license the work in any other way, but it does not - invalidate such permission if you have separately received it. - - d) If the work has interactive user interfaces, each must display - Appropriate Legal Notices; however, if the Program has interactive - interfaces that do not display Appropriate Legal Notices, your - work need not make them do so. - - A compilation of a covered work with other separate and independent -works, which are not by their nature extensions of the covered work, -and which are not combined with it such as to form a larger program, -in or on a volume of a storage or distribution medium, is called an -"aggregate" if the compilation and its resulting copyright are not -used to limit the access or legal rights of the compilation's users -beyond what the individual works permit. Inclusion of a covered work -in an aggregate does not cause this License to apply to the other -parts of the aggregate. - - 6. Conveying Non-Source Forms. - - You may convey a covered work in object code form under the terms -of sections 4 and 5, provided that you also convey the -machine-readable Corresponding Source under the terms of this License, -in one of these ways: - - a) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by the - Corresponding Source fixed on a durable physical medium - customarily used for software interchange. - - b) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by a - written offer, valid for at least three years and valid for as - long as you offer spare parts or customer support for that product - model, to give anyone who possesses the object code either (1) a - copy of the Corresponding Source for all the software in the - product that is covered by this License, on a durable physical - medium customarily used for software interchange, for a price no - more than your reasonable cost of physically performing this - conveying of source, or (2) access to copy the - Corresponding Source from a network server at no charge. - - c) Convey individual copies of the object code with a copy of the - written offer to provide the Corresponding Source. This - alternative is allowed only occasionally and noncommercially, and - only if you received the object code with such an offer, in accord - with subsection 6b. - - d) Convey the object code by offering access from a designated - place (gratis or for a charge), and offer equivalent access to the - Corresponding Source in the same way through the same place at no - further charge. You need not require recipients to copy the - Corresponding Source along with the object code. If the place to - copy the object code is a network server, the Corresponding Source - may be on a different server (operated by you or a third party) - that supports equivalent copying facilities, provided you maintain - clear directions next to the object code saying where to find the - Corresponding Source. Regardless of what server hosts the - Corresponding Source, you remain obligated to ensure that it is - available for as long as needed to satisfy these requirements. - - e) Convey the object code using peer-to-peer transmission, provided - you inform other peers where the object code and Corresponding - Source of the work are being offered to the general public at no - charge under subsection 6d. - - A separable portion of the object code, whose source code is excluded -from the Corresponding Source as a System Library, need not be -included in conveying the object code work. - - A "User Product" is either (1) a "consumer product", which means any -tangible personal property which is normally used for personal, family, -or household purposes, or (2) anything designed or sold for incorporation -into a dwelling. In determining whether a product is a consumer product, -doubtful cases shall be resolved in favor of coverage. For a particular -product received by a particular user, "normally used" refers to a -typical or common use of that class of product, regardless of the status -of the particular user or of the way in which the particular user -actually uses, or expects or is expected to use, the product. A product -is a consumer product regardless of whether the product has substantial -commercial, industrial or non-consumer uses, unless such uses represent -the only significant mode of use of the product. - - "Installation Information" for a User Product means any methods, -procedures, authorization keys, or other information required to install -and execute modified versions of a covered work in that User Product from -a modified version of its Corresponding Source. The information must -suffice to ensure that the continued functioning of the modified object -code is in no case prevented or interfered with solely because -modification has been made. - - If you convey an object code work under this section in, or with, or -specifically for use in, a User Product, and the conveying occurs as -part of a transaction in which the right of possession and use of the -User Product is transferred to the recipient in perpetuity or for a -fixed term (regardless of how the transaction is characterized), the -Corresponding Source conveyed under this section must be accompanied -by the Installation Information. But this requirement does not apply -if neither you nor any third party retains the ability to install -modified object code on the User Product (for example, the work has -been installed in ROM). - - The requirement to provide Installation Information does not include a -requirement to continue to provide support service, warranty, or updates -for a work that has been modified or installed by the recipient, or for -the User Product in which it has been modified or installed. Access to a -network may be denied when the modification itself materially and -adversely affects the operation of the network or violates the rules and -protocols for communication across the network. - - Corresponding Source conveyed, and Installation Information provided, -in accord with this section must be in a format that is publicly -documented (and with an implementation available to the public in -source code form), and must require no special password or key for -unpacking, reading or copying. - - 7. Additional Terms. - - "Additional permissions" are terms that supplement the terms of this -License by making exceptions from one or more of its conditions. -Additional permissions that are applicable to the entire Program shall -be treated as though they were included in this License, to the extent -that they are valid under applicable law. If additional permissions -apply only to part of the Program, that part may be used separately -under those permissions, but the entire Program remains governed by -this License without regard to the additional permissions. - - When you convey a copy of a covered work, you may at your option -remove any additional permissions from that copy, or from any part of -it. (Additional permissions may be written to require their own -removal in certain cases when you modify the work.) You may place -additional permissions on material, added by you to a covered work, -for which you have or can give appropriate copyright permission. - - Notwithstanding any other provision of this License, for material you -add to a covered work, you may (if authorized by the copyright holders of -that material) supplement the terms of this License with terms: - - a) Disclaiming warranty or limiting liability differently from the - terms of sections 15 and 16 of this License; or - - b) Requiring preservation of specified reasonable legal notices or - author attributions in that material or in the Appropriate Legal - Notices displayed by works containing it; or - - c) Prohibiting misrepresentation of the origin of that material, or - requiring that modified versions of such material be marked in - reasonable ways as different from the original version; or - - d) Limiting the use for publicity purposes of names of licensors or - authors of the material; or - - e) Declining to grant rights under trademark law for use of some - trade names, trademarks, or service marks; or - - f) Requiring indemnification of licensors and authors of that - material by anyone who conveys the material (or modified versions of - it) with contractual assumptions of liability to the recipient, for - any liability that these contractual assumptions directly impose on - those licensors and authors. - - All other non-permissive additional terms are considered "further -restrictions" within the meaning of section 10. If the Program as you -received it, or any part of it, contains a notice stating that it is -governed by this License along with a term that is a further -restriction, you may remove that term. If a license document contains -a further restriction but permits relicensing or conveying under this -License, you may add to a covered work material governed by the terms -of that license document, provided that the further restriction does -not survive such relicensing or conveying. - - If you add terms to a covered work in accord with this section, you -must place, in the relevant source files, a statement of the -additional terms that apply to those files, or a notice indicating -where to find the applicable terms. - - Additional terms, permissive or non-permissive, may be stated in the -form of a separately written license, or stated as exceptions; -the above requirements apply either way. - - 8. Termination. - - You may not propagate or modify a covered work except as expressly -provided under this License. Any attempt otherwise to propagate or -modify it is void, and will automatically terminate your rights under -this License (including any patent licenses granted under the third -paragraph of section 11). - - However, if you cease all violation of this License, then your -license from a particular copyright holder is reinstated (a) -provisionally, unless and until the copyright holder explicitly and -finally terminates your license, and (b) permanently, if the copyright -holder fails to notify you of the violation by some reasonable means -prior to 60 days after the cessation. - - Moreover, your license from a particular copyright holder is -reinstated permanently if the copyright holder notifies you of the -violation by some reasonable means, this is the first time you have -received notice of violation of this License (for any work) from that -copyright holder, and you cure the violation prior to 30 days after -your receipt of the notice. - - Termination of your rights under this section does not terminate the -licenses of parties who have received copies or rights from you under -this License. If your rights have been terminated and not permanently -reinstated, you do not qualify to receive new licenses for the same -material under section 10. - - 9. Acceptance Not Required for Having Copies. - - You are not required to accept this License in order to receive or -run a copy of the Program. Ancillary propagation of a covered work -occurring solely as a consequence of using peer-to-peer transmission -to receive a copy likewise does not require acceptance. However, -nothing other than this License grants you permission to propagate or -modify any covered work. These actions infringe copyright if you do -not accept this License. Therefore, by modifying or propagating a -covered work, you indicate your acceptance of this License to do so. - - 10. Automatic Licensing of Downstream Recipients. - - Each time you convey a covered work, the recipient automatically -receives a license from the original licensors, to run, modify and -propagate that work, subject to this License. You are not responsible -for enforcing compliance by third parties with this License. - - An "entity transaction" is a transaction transferring control of an -organization, or substantially all assets of one, or subdividing an -organization, or merging organizations. If propagation of a covered -work results from an entity transaction, each party to that -transaction who receives a copy of the work also receives whatever -licenses to the work the party's predecessor in interest had or could -give under the previous paragraph, plus a right to possession of the -Corresponding Source of the work from the predecessor in interest, if -the predecessor has it or can get it with reasonable efforts. - - You may not impose any further restrictions on the exercise of the -rights granted or affirmed under this License. For example, you may -not impose a license fee, royalty, or other charge for exercise of -rights granted under this License, and you may not initiate litigation -(including a cross-claim or counterclaim in a lawsuit) alleging that -any patent claim is infringed by making, using, selling, offering for -sale, or importing the Program or any portion of it. - - 11. Patents. - - A "contributor" is a copyright holder who authorizes use under this -License of the Program or a work on which the Program is based. The -work thus licensed is called the contributor's "contributor version". - - A contributor's "essential patent claims" are all patent claims -owned or controlled by the contributor, whether already acquired or -hereafter acquired, that would be infringed by some manner, permitted -by this License, of making, using, or selling its contributor version, -but do not include claims that would be infringed only as a -consequence of further modification of the contributor version. For -purposes of this definition, "control" includes the right to grant -patent sublicenses in a manner consistent with the requirements of -this License. - - Each contributor grants you a non-exclusive, worldwide, royalty-free -patent license under the contributor's essential patent claims, to -make, use, sell, offer for sale, import and otherwise run, modify and -propagate the contents of its contributor version. - - In the following three paragraphs, a "patent license" is any express -agreement or commitment, however denominated, not to enforce a patent -(such as an express permission to practice a patent or covenant not to -sue for patent infringement). To "grant" such a patent license to a -party means to make such an agreement or commitment not to enforce a -patent against the party. - - If you convey a covered work, knowingly relying on a patent license, -and the Corresponding Source of the work is not available for anyone -to copy, free of charge and under the terms of this License, through a -publicly available network server or other readily accessible means, -then you must either (1) cause the Corresponding Source to be so -available, or (2) arrange to deprive yourself of the benefit of the -patent license for this particular work, or (3) arrange, in a manner -consistent with the requirements of this License, to extend the patent -license to downstream recipients. "Knowingly relying" means you have -actual knowledge that, but for the patent license, your conveying the -covered work in a country, or your recipient's use of the covered work -in a country, would infringe one or more identifiable patents in that -country that you have reason to believe are valid. - - If, pursuant to or in connection with a single transaction or -arrangement, you convey, or propagate by procuring conveyance of, a -covered work, and grant a patent license to some of the parties -receiving the covered work authorizing them to use, propagate, modify -or convey a specific copy of the covered work, then the patent license -you grant is automatically extended to all recipients of the covered -work and works based on it. - - A patent license is "discriminatory" if it does not include within -the scope of its coverage, prohibits the exercise of, or is -conditioned on the non-exercise of one or more of the rights that are -specifically granted under this License. You may not convey a covered -work if you are a party to an arrangement with a third party that is -in the business of distributing software, under which you make payment -to the third party based on the extent of your activity of conveying -the work, and under which the third party grants, to any of the -parties who would receive the covered work from you, a discriminatory -patent license (a) in connection with copies of the covered work -conveyed by you (or copies made from those copies), or (b) primarily -for and in connection with specific products or compilations that -contain the covered work, unless you entered into that arrangement, -or that patent license was granted, prior to 28 March 2007. - - Nothing in this License shall be construed as excluding or limiting -any implied license or other defenses to infringement that may -otherwise be available to you under applicable patent law. - - 12. No Surrender of Others' Freedom. - - If conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot convey a -covered work so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you may -not convey it at all. For example, if you agree to terms that obligate you -to collect a royalty for further conveying from those to whom you convey -the Program, the only way you could satisfy both those terms and this -License would be to refrain entirely from conveying the Program. - - 13. Use with the GNU Affero General Public License. - - Notwithstanding any other provision of this License, you have -permission to link or combine any covered work with a work licensed -under version 3 of the GNU Affero General Public License into a single -combined work, and to convey the resulting work. The terms of this -License will continue to apply to the part which is the covered work, -but the special requirements of the GNU Affero General Public License, -section 13, concerning interaction through a network will apply to the -combination as such. - - 14. Revised Versions of this License. - - The Free Software Foundation may publish revised and/or new versions of -the GNU General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - - Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU General -Public License "or any later version" applies to it, you have the -option of following the terms and conditions either of that numbered -version or of any later version published by the Free Software -Foundation. If the Program does not specify a version number of the -GNU General Public License, you may choose any version ever published -by the Free Software Foundation. - - If the Program specifies that a proxy can decide which future -versions of the GNU General Public License can be used, that proxy's -public statement of acceptance of a version permanently authorizes you -to choose that version for the Program. - - Later license versions may give you additional or different -permissions. However, no additional obligations are imposed on any -author or copyright holder as a result of your choosing to follow a -later version. - - 15. Disclaimer of Warranty. - - THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY -APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT -HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY -OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, -THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM -IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF -ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. Limitation of Liability. - - IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS -THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY -GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE -USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF -DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD -PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), -EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF -SUCH DAMAGES. - - 17. Interpretation of Sections 15 and 16. - - If the disclaimer of warranty and limitation of liability provided -above cannot be given local legal effect according to their terms, -reviewing courts shall apply local law that most closely approximates -an absolute waiver of all civil liability in connection with the -Program, unless a warranty or assumption of liability accompanies a -copy of the Program in return for a fee. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -state the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - -Also add information on how to contact you by electronic and paper mail. - - If the program does terminal interaction, make it output a short -notice like this when it starts in an interactive mode: - - Copyright (C) - This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, your program's commands -might be different; for a GUI interface, you would use an "about box". - - You should also get your employer (if you work as a programmer) or school, -if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU GPL, see -. - - The GNU General Public License does not permit incorporating your program -into proprietary programs. If your program is a subroutine library, you -may consider it more useful to permit linking proprietary applications with -the library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. But first, please read -. diff --git a/libraries/PinChangeInt/keywords.txt b/libraries/PinChangeInt/keywords.txt deleted file mode 100644 index 24e1cec..0000000 --- a/libraries/PinChangeInt/keywords.txt +++ /dev/null @@ -1,10 +0,0 @@ -# LITERAL1 specifies constants - -# KEYWORD1 specifies datatypes and C/C++ keywords -pinState KEYWORD1 PinState -arduinoPin KEYWORD1 ArduinoPin -PCintPort KEYWORD1 PCInterruptPort - -# KEYWORD2 specifies methods and functions -attachInterrupt KEYWORD2 AttachInterrupt -detachInterrupt KEYWORD2 DetachInterrupt diff --git a/libraries/PubSubClient/.gitignore b/libraries/PubSubClient/.gitignore deleted file mode 100644 index 1c3ba18..0000000 --- a/libraries/PubSubClient/.gitignore +++ /dev/null @@ -1 +0,0 @@ -tests/bin diff --git a/libraries/PubSubClient/.travis.yml b/libraries/PubSubClient/.travis.yml deleted file mode 100644 index e7b28cb..0000000 --- a/libraries/PubSubClient/.travis.yml +++ /dev/null @@ -1,7 +0,0 @@ -sudo: false -language: cpp -compiler: - - g++ -script: cd tests && make && make test -os: - - linux diff --git a/libraries/PubSubClient/CHANGES.txt b/libraries/PubSubClient/CHANGES.txt deleted file mode 100755 index 8c8bef6..0000000 --- a/libraries/PubSubClient/CHANGES.txt +++ /dev/null @@ -1,68 +0,0 @@ -2.4 - * Add MQTT_SOCKET_TIMEOUT to prevent it blocking indefinitely - whilst waiting for inbound data - * Fixed return code when publishing >256 bytes - -2.3 - * Add publish(topic,payload,retained) function - -2.2 - * Change code layout to match Arduino Library reqs - -2.1 - * Add MAX_TRANSFER_SIZE def to chunk messages if needed - * Reject topic/payloads that exceed MQTT_MAX_PACKET_SIZE - -2.0 - * Add (and default to) MQTT 3.1.1 support - * Fix PROGMEM handling for Intel Galileo/ESP8266 - * Add overloaded constructors for convenience - * Add chainable setters for server/callback/client/stream - * Add state function to return connack return code - -1.9 - * Do not split MQTT packets over multiple calls to _client->write() - * API change: All constructors now require an instance of Client - to be passed in. - * Fixed example to match 1.8 api changes - dpslwk - * Added username/password support - WilHall - * Added publish_P - publishes messages from PROGMEM - jobytaffey - -1.8 - * KeepAlive interval is configurable in PubSubClient.h - * Maximum packet size is configurable in PubSubClient.h - * API change: Return boolean rather than int from various functions - * API change: Length parameter in message callback changed - from int to unsigned int - * Various internal tidy-ups around types -1.7 - * Improved keepalive handling - * Updated to the Arduino-1.0 API -1.6 - * Added the ability to publish a retained message - -1.5 - * Added default constructor - * Fixed compile error when used with arduino-0021 or later - -1.4 - * Fixed connection lost handling - -1.3 - * Fixed packet reading bug in PubSubClient.readPacket - -1.2 - * Fixed compile error when used with arduino-0016 or later - - -1.1 - * Reduced size of library - * Added support for Will messages - * Clarified licensing - see LICENSE.txt - - -1.0 - * Only Quality of Service (QOS) 0 messaging is supported - * The maximum message size, including header, is 128 bytes - * The keepalive interval is set to 30 seconds - * No support for Will messages diff --git a/libraries/PubSubClient/LICENSE.txt b/libraries/PubSubClient/LICENSE.txt deleted file mode 100755 index 217df35..0000000 --- a/libraries/PubSubClient/LICENSE.txt +++ /dev/null @@ -1,20 +0,0 @@ -Copyright (c) 2008-2015 Nicholas O'Leary - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/libraries/PubSubClient/README.md b/libraries/PubSubClient/README.md deleted file mode 100644 index 8317691..0000000 --- a/libraries/PubSubClient/README.md +++ /dev/null @@ -1,47 +0,0 @@ -# Arduino Client for MQTT - -This library provides a client for doing simple publish/subscribe messaging with -a server that supports MQTT. - -## Examples - -The library comes with a number of example sketches. See File > Examples > PubSubClient -within the Arduino application. - -Full API documentation is available here: http://pubsubclient.knolleary.net - -## Limitations - - - It can only publish QoS 0 messages. It can subscribe at QoS 0 or QoS 1. - - The maximum message size, including header, is **128 bytes** by default. This - is configurable via `MQTT_MAX_PACKET_SIZE` in `PubSubClient.h`. - - The keepalive interval is set to 15 seconds by default. This is configurable - via `MQTT_KEEPALIVE` in `PubSubClient.h`. - - The client uses MQTT 3.1.1 by default. It can be changed to use MQTT 3.1 by - changing value of `MQTT_VERSION` in `PubSubClient.h`. - - -## Compatible Hardware - -The library uses the Arduino Ethernet Client api for interacting with the -underlying network hardware. This means it Just Works with a growing number of -boards and shields, including: - - - Arduino Ethernet - - Arduino Ethernet Shield - - Arduino YUN – use the included `YunClient` in place of `EthernetClient`, and - be sure to do a `Bridge.begin()` first - - Arduino WiFi Shield - if you want to send packets > 90 bytes with this shield, - enable the `MQTT_MAX_TRANSFER_SIZE` define in `PubSubClient.h`. - - Sparkfun WiFly Shield – [library](https://github.com/dpslwk/WiFly) - - TI CC3000 WiFi - [library](https://github.com/sparkfun/SFE_CC3000_Library) - - Intel Galileo/Edison - - ESP8266 - -The library cannot currently be used with hardware based on the ENC28J60 chip – -such as the Nanode or the Nuelectronics Ethernet Shield. For those, there is an -[alternative library](https://github.com/njh/NanodeMQTT) available. - -## License - -This code is released under the MIT License. diff --git a/libraries/PubSubClient/examples/mqtt_auth/mqtt_auth.ino b/libraries/PubSubClient/examples/mqtt_auth/mqtt_auth.ino deleted file mode 100755 index e9f7b18..0000000 --- a/libraries/PubSubClient/examples/mqtt_auth/mqtt_auth.ino +++ /dev/null @@ -1,43 +0,0 @@ -/* - Basic MQTT example with Authentication - - - connects to an MQTT server, providing username - and password - - publishes "hello world" to the topic "outTopic" - - subscribes to the topic "inTopic" -*/ - -#include -#include -#include - -// Update these with values suitable for your network. -byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; -IPAddress ip(172, 16, 0, 100); -IPAddress server(172, 16, 0, 2); - -void callback(char* topic, byte* payload, unsigned int length) { - // handle message arrived -} - -EthernetClient ethClient; -PubSubClient client(server, 1883, callback, ethClient); - -void setup() -{ - Ethernet.begin(mac, ip); - // Note - the default maximum packet size is 128 bytes. If the - // combined length of clientId, username and password exceed this, - // you will need to increase the value of MQTT_MAX_PACKET_SIZE in - // PubSubClient.h - - if (client.connect("arduinoClient", "testuser", "testpass")) { - client.publish("outTopic","hello world"); - client.subscribe("inTopic"); - } -} - -void loop() -{ - client.loop(); -} diff --git a/libraries/PubSubClient/examples/mqtt_basic/mqtt_basic.ino b/libraries/PubSubClient/examples/mqtt_basic/mqtt_basic.ino deleted file mode 100755 index f545ade..0000000 --- a/libraries/PubSubClient/examples/mqtt_basic/mqtt_basic.ino +++ /dev/null @@ -1,77 +0,0 @@ -/* - Basic MQTT example - - This sketch demonstrates the basic capabilities of the library. - It connects to an MQTT server then: - - publishes "hello world" to the topic "outTopic" - - subscribes to the topic "inTopic", printing out any messages - it receives. NB - it assumes the received payloads are strings not binary - - It will reconnect to the server if the connection is lost using a blocking - reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to - achieve the same result without blocking the main loop. - -*/ - -#include -#include -#include - -// Update these with values suitable for your network. -byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; -IPAddress ip(172, 16, 0, 100); -IPAddress server(172, 16, 0, 2); - -void callback(char* topic, byte* payload, unsigned int length) { - Serial.print("Message arrived ["); - Serial.print(topic); - Serial.print("] "); - for (int i=0;i Preferences -> Additional Boards Manager URLs": - http://arduino.esp8266.com/stable/package_esp8266com_index.json - - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266" - - Select your ESP8266 in "Tools -> Board" - -*/ - -#include -#include - -// Update these with values suitable for your network. - -const char* ssid = "........"; -const char* password = "........"; -const char* mqtt_server = "broker.mqtt-dashboard.com"; - -WiFiClient espClient; -PubSubClient client(espClient); -long lastMsg = 0; -char msg[50]; -int value = 0; - -void setup_wifi() { - - delay(10); - // We start by connecting to a WiFi network - Serial.println(); - Serial.print("Connecting to "); - Serial.println(ssid); - - WiFi.begin(ssid, password); - - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } - - Serial.println(""); - Serial.println("WiFi connected"); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); -} - -void callback(char* topic, byte* payload, unsigned int length) { - Serial.print("Message arrived ["); - Serial.print(topic); - Serial.print("] "); - for (int i = 0; i < length; i++) { - Serial.print((char)payload[i]); - } - Serial.println(); - - // Switch on the LED if an 1 was received as first character - if ((char)payload[0] == '1') { - digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level - // but actually the LED is on; this is because - // it is acive low on the ESP-01) - } else { - digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH - } - -} - -void reconnect() { - // Loop until we're reconnected - while (!client.connected()) { - Serial.print("Attempting MQTT connection..."); - // Attempt to connect - if (client.connect("ESP8266Client")) { - Serial.println("connected"); - // Once connected, publish an announcement... - client.publish("outTopic", "hello world"); - // ... and resubscribe - client.subscribe("inTopic"); - } else { - Serial.print("failed, rc="); - Serial.print(client.state()); - Serial.println(" try again in 5 seconds"); - // Wait 5 seconds before retrying - delay(5000); - } - } -} - -void setup() { - pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output - Serial.begin(115200); - setup_wifi(); - client.setServer(mqtt_server, 1883); - client.setCallback(callback); -} - -void loop() { - - if (!client.connected()) { - reconnect(); - } - client.loop(); - - long now = millis(); - if (now - lastMsg > 2000) { - lastMsg = now; - ++value; - snprintf (msg, 75, "hello world #%ld", value); - Serial.print("Publish message: "); - Serial.println(msg); - client.publish("outTopic", msg); - } -} diff --git a/libraries/PubSubClient/examples/mqtt_publish_in_callback/mqtt_publish_in_callback.ino b/libraries/PubSubClient/examples/mqtt_publish_in_callback/mqtt_publish_in_callback.ino deleted file mode 100755 index 42afb2a..0000000 --- a/libraries/PubSubClient/examples/mqtt_publish_in_callback/mqtt_publish_in_callback.ino +++ /dev/null @@ -1,60 +0,0 @@ -/* - Publishing in the callback - - - connects to an MQTT server - - subscribes to the topic "inTopic" - - when a message is received, republishes it to "outTopic" - - This example shows how to publish messages within the - callback function. The callback function header needs to - be declared before the PubSubClient constructor and the - actual callback defined afterwards. - This ensures the client reference in the callback function - is valid. - -*/ - -#include -#include -#include - -// Update these with values suitable for your network. -byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; -IPAddress ip(172, 16, 0, 100); -IPAddress server(172, 16, 0, 2); - -// Callback function header -void callback(char* topic, byte* payload, unsigned int length); - -EthernetClient ethClient; -PubSubClient client(server, 1883, callback, ethClient); - -// Callback function -void callback(char* topic, byte* payload, unsigned int length) { - // In order to republish this payload, a copy must be made - // as the orignal payload buffer will be overwritten whilst - // constructing the PUBLISH packet. - - // Allocate the correct amount of memory for the payload copy - byte* p = (byte*)malloc(length); - // Copy the payload to the new buffer - memcpy(p,payload,length); - client.publish("outTopic", p, length); - // Free the memory - free(p); -} - -void setup() -{ - - Ethernet.begin(mac, ip); - if (client.connect("arduinoClient")) { - client.publish("outTopic","hello world"); - client.subscribe("inTopic"); - } -} - -void loop() -{ - client.loop(); -} diff --git a/libraries/PubSubClient/examples/mqtt_reconnect_nonblocking/mqtt_reconnect_nonblocking.ino b/libraries/PubSubClient/examples/mqtt_reconnect_nonblocking/mqtt_reconnect_nonblocking.ino deleted file mode 100644 index 080b739..0000000 --- a/libraries/PubSubClient/examples/mqtt_reconnect_nonblocking/mqtt_reconnect_nonblocking.ino +++ /dev/null @@ -1,67 +0,0 @@ -/* - Reconnecting MQTT example - non-blocking - - This sketch demonstrates how to keep the client connected - using a non-blocking reconnect function. If the client loses - its connection, it attempts to reconnect every 5 seconds - without blocking the main loop. - -*/ - -#include -#include -#include - -// Update these with values suitable for your hardware/network. -byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; -IPAddress ip(172, 16, 0, 100); -IPAddress server(172, 16, 0, 2); - -void callback(char* topic, byte* payload, unsigned int length) { - // handle message arrived -} - -EthernetClient ethClient; -PubSubClient client(ethClient); - -long lastReconnectAttempt = 0; - -boolean reconnect() { - if (client.connect("arduinoClient")) { - // Once connected, publish an announcement... - client.publish("outTopic","hello world"); - // ... and resubscribe - client.subscribe("inTopic"); - } - return client.connected(); -} - -void setup() -{ - client.setServer(server, 1883); - client.setCallback(callback); - - Ethernet.begin(mac, ip); - delay(1500); - lastReconnectAttempt = 0; -} - - -void loop() -{ - if (!client.connected()) { - long now = millis(); - if (now - lastReconnectAttempt > 5000) { - lastReconnectAttempt = now; - // Attempt to reconnect - if (reconnect()) { - lastReconnectAttempt = 0; - } - } - } else { - // Client connected - - client.loop(); - } - -} diff --git a/libraries/PubSubClient/examples/mqtt_stream/mqtt_stream.ino b/libraries/PubSubClient/examples/mqtt_stream/mqtt_stream.ino deleted file mode 100755 index 67c2287..0000000 --- a/libraries/PubSubClient/examples/mqtt_stream/mqtt_stream.ino +++ /dev/null @@ -1,57 +0,0 @@ -/* - Example of using a Stream object to store the message payload - - Uses SRAM library: https://github.com/ennui2342/arduino-sram - but could use any Stream based class such as SD - - - connects to an MQTT server - - publishes "hello world" to the topic "outTopic" - - subscribes to the topic "inTopic" -*/ - -#include -#include -#include -#include - -// Update these with values suitable for your network. -byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; -IPAddress ip(172, 16, 0, 100); -IPAddress server(172, 16, 0, 2); - -SRAM sram(4, SRAM_1024); - -void callback(char* topic, byte* payload, unsigned int length) { - sram.seek(1); - - // do something with the message - for(uint8_t i=0; i -maintainer=Nick O'Leary -sentence=A client library for MQTT messaging. -paragraph=MQTT is a lightweight messaging protocol ideal for small devices. This library allows you to send and receive MQTT messages. It supports the latest MQTT 3.1.1 protocol and can be configured to use the older MQTT 3.1 if needed. It supports all Arduino Ethernet Client compatible hardware, including the Intel Galileo/Edison, ESP8266 and TI CC3000. -category=Communication -url=http://pubsubclient.knolleary.net -architectures=* diff --git a/libraries/PubSubClient/src/PubSubClient.cpp b/libraries/PubSubClient/src/PubSubClient.cpp deleted file mode 100644 index e66c9e0..0000000 --- a/libraries/PubSubClient/src/PubSubClient.cpp +++ /dev/null @@ -1,591 +0,0 @@ -/* - PubSubClient.cpp - A simple client for MQTT. - Nick O'Leary - http://knolleary.net -*/ - -#include "PubSubClient.h" -#include "Arduino.h" - -PubSubClient::PubSubClient() { - this->_state = MQTT_DISCONNECTED; - this->_client = NULL; - this->stream = NULL; - setCallback(NULL); -} - -PubSubClient::PubSubClient(Client& client) { - this->_state = MQTT_DISCONNECTED; - setClient(client); - this->stream = NULL; -} - -PubSubClient::PubSubClient(IPAddress addr, uint16_t port, Client& client) { - this->_state = MQTT_DISCONNECTED; - setServer(addr, port); - setClient(client); - this->stream = NULL; -} -PubSubClient::PubSubClient(IPAddress addr, uint16_t port, Client& client, Stream& stream) { - this->_state = MQTT_DISCONNECTED; - setServer(addr,port); - setClient(client); - setStream(stream); -} -PubSubClient::PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) { - this->_state = MQTT_DISCONNECTED; - setServer(addr, port); - setCallback(callback); - setClient(client); - this->stream = NULL; -} -PubSubClient::PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) { - this->_state = MQTT_DISCONNECTED; - setServer(addr,port); - setCallback(callback); - setClient(client); - setStream(stream); -} - -PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, Client& client) { - this->_state = MQTT_DISCONNECTED; - setServer(ip, port); - setClient(client); - this->stream = NULL; -} -PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, Client& client, Stream& stream) { - this->_state = MQTT_DISCONNECTED; - setServer(ip,port); - setClient(client); - setStream(stream); -} -PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) { - this->_state = MQTT_DISCONNECTED; - setServer(ip, port); - setCallback(callback); - setClient(client); - this->stream = NULL; -} -PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) { - this->_state = MQTT_DISCONNECTED; - setServer(ip,port); - setCallback(callback); - setClient(client); - setStream(stream); -} - -PubSubClient::PubSubClient(const char* domain, uint16_t port, Client& client) { - this->_state = MQTT_DISCONNECTED; - setServer(domain,port); - setClient(client); - this->stream = NULL; -} -PubSubClient::PubSubClient(const char* domain, uint16_t port, Client& client, Stream& stream) { - this->_state = MQTT_DISCONNECTED; - setServer(domain,port); - setClient(client); - setStream(stream); -} -PubSubClient::PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) { - this->_state = MQTT_DISCONNECTED; - setServer(domain,port); - setCallback(callback); - setClient(client); - this->stream = NULL; -} -PubSubClient::PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) { - this->_state = MQTT_DISCONNECTED; - setServer(domain,port); - setCallback(callback); - setClient(client); - setStream(stream); -} - -boolean PubSubClient::connect(const char *id) { - return connect(id,NULL,NULL,0,0,0,0); -} - -boolean PubSubClient::connect(const char *id, const char *user, const char *pass) { - return connect(id,user,pass,0,0,0,0); -} - -boolean PubSubClient::connect(const char *id, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage) { - return connect(id,NULL,NULL,willTopic,willQos,willRetain,willMessage); -} - -boolean PubSubClient::connect(const char *id, const char *user, const char *pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage) { - if (!connected()) { - int result = 0; - - if (domain != NULL) { - result = _client->connect(this->domain, this->port); - } else { - result = _client->connect(this->ip, this->port); - } - if (result == 1) { - nextMsgId = 1; - // Leave room in the buffer for header and variable length field - uint16_t length = 5; - unsigned int j; - -#if MQTT_VERSION == MQTT_VERSION_3_1 - uint8_t d[9] = {0x00,0x06,'M','Q','I','s','d','p', MQTT_VERSION}; -#define MQTT_HEADER_VERSION_LENGTH 9 -#elif MQTT_VERSION == MQTT_VERSION_3_1_1 - uint8_t d[7] = {0x00,0x04,'M','Q','T','T',MQTT_VERSION}; -#define MQTT_HEADER_VERSION_LENGTH 7 -#endif - for (j = 0;j>1); - } - } - - buffer[length++] = v; - - buffer[length++] = ((MQTT_KEEPALIVE) >> 8); - buffer[length++] = ((MQTT_KEEPALIVE) & 0xFF); - length = writeString(id,buffer,length); - if (willTopic) { - length = writeString(willTopic,buffer,length); - length = writeString(willMessage,buffer,length); - } - - if(user != NULL) { - length = writeString(user,buffer,length); - if(pass != NULL) { - length = writeString(pass,buffer,length); - } - } - - write(MQTTCONNECT,buffer,length-5); - - lastInActivity = lastOutActivity = millis(); - - while (!_client->available()) { - unsigned long t = millis(); - if (t-lastInActivity >= ((int32_t) MQTT_SOCKET_TIMEOUT*1000UL)) { - _state = MQTT_CONNECTION_TIMEOUT; - _client->stop(); - return false; - } - } - uint8_t llen; - uint16_t len = readPacket(&llen); - - if (len == 4) { - if (buffer[3] == 0) { - lastInActivity = millis(); - pingOutstanding = false; - _state = MQTT_CONNECTED; - return true; - } else { - _state = buffer[3]; - } - } - _client->stop(); - } else { - _state = MQTT_CONNECT_FAILED; - } - return false; - } - return true; -} - -// reads a byte into result -boolean PubSubClient::readByte(uint8_t * result) { - uint32_t previousMillis = millis(); - while(!_client->available()) { - uint32_t currentMillis = millis(); - if(currentMillis - previousMillis >= ((int32_t) MQTT_SOCKET_TIMEOUT * 1000)){ - return false; - } - } - *result = _client->read(); - return true; -} - -// reads a byte into result[*index] and increments index -boolean PubSubClient::readByte(uint8_t * result, uint16_t * index){ - uint16_t current_index = *index; - uint8_t * write_address = &(result[current_index]); - if(readByte(write_address)){ - *index = current_index + 1; - return true; - } - return false; -} - -uint16_t PubSubClient::readPacket(uint8_t* lengthLength) { - uint16_t len = 0; - if(!readByte(buffer, &len)) return 0; - bool isPublish = (buffer[0]&0xF0) == MQTTPUBLISH; - uint32_t multiplier = 1; - uint16_t length = 0; - uint8_t digit = 0; - uint16_t skip = 0; - uint8_t start = 0; - - do { - if(!readByte(&digit)) return 0; - buffer[len++] = digit; - length += (digit & 127) * multiplier; - multiplier *= 128; - } while ((digit & 128) != 0); - *lengthLength = len-1; - - if (isPublish) { - // Read in topic length to calculate bytes to skip over for Stream writing - if(!readByte(buffer, &len)) return 0; - if(!readByte(buffer, &len)) return 0; - skip = (buffer[*lengthLength+1]<<8)+buffer[*lengthLength+2]; - start = 2; - if (buffer[0]&MQTTQOS1) { - // skip message id - skip += 2; - } - } - - for (uint16_t i = start;istream) { - if (isPublish && len-*lengthLength-2>skip) { - this->stream->write(digit); - } - } - if (len < MQTT_MAX_PACKET_SIZE) { - buffer[len] = digit; - } - len++; - } - - if (!this->stream && len > MQTT_MAX_PACKET_SIZE) { - len = 0; // This will cause the packet to be ignored. - } - - return len; -} - -boolean PubSubClient::loop() { - if (connected()) { - unsigned long t = millis(); - if ((t - lastInActivity > MQTT_KEEPALIVE*1000UL) || (t - lastOutActivity > MQTT_KEEPALIVE*1000UL)) { - if (pingOutstanding) { - this->_state = MQTT_CONNECTION_TIMEOUT; - _client->stop(); - return false; - } else { - buffer[0] = MQTTPINGREQ; - buffer[1] = 0; - _client->write(buffer,2); - lastOutActivity = t; - lastInActivity = t; - pingOutstanding = true; - } - } - if (_client->available()) { - uint8_t llen; - uint16_t len = readPacket(&llen); - uint16_t msgId = 0; - uint8_t *payload; - if (len > 0) { - lastInActivity = t; - uint8_t type = buffer[0]&0xF0; - if (type == MQTTPUBLISH) { - if (callback) { - uint16_t tl = (buffer[llen+1]<<8)+buffer[llen+2]; - char topic[tl+1]; - for (uint16_t i=0;i0 - if ((buffer[0]&0x06) == MQTTQOS1) { - msgId = (buffer[llen+3+tl]<<8)+buffer[llen+3+tl+1]; - payload = buffer+llen+3+tl+2; - callback(topic,payload,len-llen-3-tl-2); - - buffer[0] = MQTTPUBACK; - buffer[1] = 2; - buffer[2] = (msgId >> 8); - buffer[3] = (msgId & 0xFF); - _client->write(buffer,4); - lastOutActivity = t; - - } else { - payload = buffer+llen+3+tl; - callback(topic,payload,len-llen-3-tl); - } - } - } else if (type == MQTTPINGREQ) { - buffer[0] = MQTTPINGRESP; - buffer[1] = 0; - _client->write(buffer,2); - } else if (type == MQTTPINGRESP) { - pingOutstanding = false; - } - } - } - return true; - } - return false; -} - -boolean PubSubClient::publish(const char* topic, const char* payload) { - return publish(topic,(const uint8_t*)payload,strlen(payload),false); -} - -boolean PubSubClient::publish(const char* topic, const char* payload, boolean retained) { - return publish(topic,(const uint8_t*)payload,strlen(payload),retained); -} - -boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength) { - return publish(topic, payload, plength, false); -} - -boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength, boolean retained) { - if (connected()) { - if (MQTT_MAX_PACKET_SIZE < 5 + 2+strlen(topic) + plength) { - // Too long - return false; - } - // Leave room in the buffer for header and variable length field - uint16_t length = 5; - length = writeString(topic,buffer,length); - uint16_t i; - for (i=0;i 0) { - digit |= 0x80; - } - buffer[pos++] = digit; - llen++; - } while(len>0); - - pos = writeString(topic,buffer,pos); - - rc += _client->write(buffer,pos); - - for (i=0;iwrite((char)pgm_read_byte_near(payload + i)); - } - - lastOutActivity = millis(); - - return rc == tlen + 4 + plength; -} - -boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) { - uint8_t lenBuf[4]; - uint8_t llen = 0; - uint8_t digit; - uint8_t pos = 0; - uint16_t rc; - uint16_t len = length; - do { - digit = len % 128; - len = len / 128; - if (len > 0) { - digit |= 0x80; - } - lenBuf[pos++] = digit; - llen++; - } while(len>0); - - buf[4-llen] = header; - for (int i=0;i 0) && result) { - bytesToWrite = (bytesRemaining > MQTT_MAX_TRANSFER_SIZE)?MQTT_MAX_TRANSFER_SIZE:bytesRemaining; - rc = _client->write(writeBuf,bytesToWrite); - result = (rc == bytesToWrite); - bytesRemaining -= rc; - writeBuf += rc; - } - return result; -#else - rc = _client->write(buf+(4-llen),length+1+llen); - lastOutActivity = millis(); - return (rc == 1+llen+length); -#endif -} - -boolean PubSubClient::subscribe(const char* topic) { - return subscribe(topic, 0); -} - -boolean PubSubClient::subscribe(const char* topic, uint8_t qos) { - // original: if (qos < 0 || qos > 1) { (qos is uint8_t, hence qos < 0 impossible, tekka) - if (qos > 1) { - return false; - } - if (MQTT_MAX_PACKET_SIZE < 9 + strlen(topic)) { - // Too long - return false; - } - if (connected()) { - // Leave room in the buffer for header and variable length field - uint16_t length = 5; - nextMsgId++; - if (nextMsgId == 0) { - nextMsgId = 1; - } - buffer[length++] = (nextMsgId >> 8); - buffer[length++] = (nextMsgId & 0xFF); - length = writeString((char*)topic, buffer,length); - buffer[length++] = qos; - return write(MQTTSUBSCRIBE|MQTTQOS1,buffer,length-5); - } - return false; -} - -boolean PubSubClient::unsubscribe(const char* topic) { - if (MQTT_MAX_PACKET_SIZE < 9 + strlen(topic)) { - // Too long - return false; - } - if (connected()) { - uint16_t length = 5; - nextMsgId++; - if (nextMsgId == 0) { - nextMsgId = 1; - } - buffer[length++] = (nextMsgId >> 8); - buffer[length++] = (nextMsgId & 0xFF); - length = writeString(topic, buffer,length); - return write(MQTTUNSUBSCRIBE|MQTTQOS1,buffer,length-5); - } - return false; -} - -void PubSubClient::disconnect() { - buffer[0] = MQTTDISCONNECT; - buffer[1] = 0; - _client->write(buffer,2); - _state = MQTT_DISCONNECTED; - _client->stop(); - lastInActivity = lastOutActivity = millis(); -} - -uint16_t PubSubClient::writeString(const char* string, uint8_t* buf, uint16_t pos) { - const char* idp = string; - uint16_t i = 0; - pos += 2; - while (*idp) { - buf[pos++] = *idp++; - i++; - } - buf[pos-i-2] = (i >> 8); - buf[pos-i-1] = (i & 0xFF); - return pos; -} - - -boolean PubSubClient::connected() { - boolean rc; - if (_client == NULL ) { - rc = false; - } else { - rc = (int)_client->connected(); - if (!rc) { - if (this->_state == MQTT_CONNECTED) { - this->_state = MQTT_CONNECTION_LOST; - _client->flush(); - _client->stop(); - } - } - } - return rc; -} - -PubSubClient& PubSubClient::setServer(uint8_t * ip, uint16_t port) { - IPAddress addr(ip[0],ip[1],ip[2],ip[3]); - return setServer(addr,port); -} - -PubSubClient& PubSubClient::setServer(IPAddress ip, uint16_t port) { - this->ip = ip; - this->port = port; - this->domain = NULL; - return *this; -} - -PubSubClient& PubSubClient::setServer(const char * domain, uint16_t port) { - this->domain = domain; - this->port = port; - return *this; -} - -PubSubClient& PubSubClient::setCallback(MQTT_CALLBACK_SIGNATURE) { - this->callback = callback; - return *this; -} - -PubSubClient& PubSubClient::setClient(Client& client){ - this->_client = &client; - return *this; -} - -PubSubClient& PubSubClient::setStream(Stream& stream){ - this->stream = &stream; - return *this; -} - -int PubSubClient::state() { - return this->_state; -} diff --git a/libraries/PubSubClient/src/PubSubClient.h b/libraries/PubSubClient/src/PubSubClient.h deleted file mode 100644 index 90ad8ae..0000000 --- a/libraries/PubSubClient/src/PubSubClient.h +++ /dev/null @@ -1,145 +0,0 @@ -/* - PubSubClient.h - A simple client for MQTT. - Nick O'Leary - http://knolleary.net -*/ - -#ifndef PubSubClient_h -#define PubSubClient_h - -#include -#include "IPAddress.h" -#include "Client.h" -#include "Stream.h" - -#define MQTT_VERSION_3_1 3 -#define MQTT_VERSION_3_1_1 4 - -// MQTT_VERSION : Pick the version -//#define MQTT_VERSION MQTT_VERSION_3_1 -#ifndef MQTT_VERSION -#define MQTT_VERSION MQTT_VERSION_3_1_1 -#endif - -// MQTT_MAX_PACKET_SIZE : Maximum packet size -#ifndef MQTT_MAX_PACKET_SIZE -#define MQTT_MAX_PACKET_SIZE 128 -#endif - -// MQTT_KEEPALIVE : keepAlive interval in Seconds -#ifndef MQTT_KEEPALIVE -#define MQTT_KEEPALIVE 15 -#endif - -// MQTT_SOCKET_TIMEOUT: socket timeout interval in Seconds -#ifndef MQTT_SOCKET_TIMEOUT -#define MQTT_SOCKET_TIMEOUT 15 -#endif - -// MQTT_MAX_TRANSFER_SIZE : limit how much data is passed to the network client -// in each write call. Needed for the Arduino Wifi Shield. Leave undefined to -// pass the entire MQTT packet in each write call. -//#define MQTT_MAX_TRANSFER_SIZE 80 - -// Possible values for client.state() -#define MQTT_CONNECTION_TIMEOUT -4 -#define MQTT_CONNECTION_LOST -3 -#define MQTT_CONNECT_FAILED -2 -#define MQTT_DISCONNECTED -1 -#define MQTT_CONNECTED 0 -#define MQTT_CONNECT_BAD_PROTOCOL 1 -#define MQTT_CONNECT_BAD_CLIENT_ID 2 -#define MQTT_CONNECT_UNAVAILABLE 3 -#define MQTT_CONNECT_BAD_CREDENTIALS 4 -#define MQTT_CONNECT_UNAUTHORIZED 5 - -#define MQTTCONNECT 1 << 4 // Client request to connect to Server -#define MQTTCONNACK 2 << 4 // Connect Acknowledgment -#define MQTTPUBLISH 3 << 4 // Publish message -#define MQTTPUBACK 4 << 4 // Publish Acknowledgment -#define MQTTPUBREC 5 << 4 // Publish Received (assured delivery part 1) -#define MQTTPUBREL 6 << 4 // Publish Release (assured delivery part 2) -#define MQTTPUBCOMP 7 << 4 // Publish Complete (assured delivery part 3) -#define MQTTSUBSCRIBE 8 << 4 // Client Subscribe request -#define MQTTSUBACK 9 << 4 // Subscribe Acknowledgment -#define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request -#define MQTTUNSUBACK 11 << 4 // Unsubscribe Acknowledgment -#define MQTTPINGREQ 12 << 4 // PING Request -#define MQTTPINGRESP 13 << 4 // PING Response -#define MQTTDISCONNECT 14 << 4 // Client is Disconnecting -#define MQTTReserved 15 << 4 // Reserved - -#define MQTTQOS0 (0 << 1) -#define MQTTQOS1 (1 << 1) -#define MQTTQOS2 (2 << 1) - -#ifdef ESP8266 -#include -#define MQTT_CALLBACK_SIGNATURE std::function callback -#else -#define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int) -#endif - -/** PubSubClient class */ -class PubSubClient { -private: - Client* _client; - uint8_t buffer[MQTT_MAX_PACKET_SIZE]; - uint16_t nextMsgId; - unsigned long lastOutActivity; - unsigned long lastInActivity; - bool pingOutstanding; - MQTT_CALLBACK_SIGNATURE; - uint16_t readPacket(uint8_t*); - boolean readByte(uint8_t * result); - boolean readByte(uint8_t * result, uint16_t * index); - boolean write(uint8_t header, uint8_t* buf, uint16_t length); - uint16_t writeString(const char* string, uint8_t* buf, uint16_t pos); - IPAddress ip; - const char* domain; - uint16_t port; - Stream* stream; - int _state; -public: - PubSubClient(); //!< PubSubClient - PubSubClient(Client& client); //!< PubSubClient - PubSubClient(IPAddress, uint16_t, Client& client); //!< PubSubClient - PubSubClient(IPAddress, uint16_t, Client& client, Stream&); //!< PubSubClient - PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client); //!< PubSubClient - PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&); //!< PubSubClient - PubSubClient(uint8_t *, uint16_t, Client& client); //!< PubSubClient - PubSubClient(uint8_t *, uint16_t, Client& client, Stream&); //!< PubSubClient - PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client); //!< PubSubClient - PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&); //!< PubSubClient - PubSubClient(const char*, uint16_t, Client& client); //!< PubSubClient - PubSubClient(const char*, uint16_t, Client& client, Stream&); //!< PubSubClient - PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client); //!< PubSubClient - PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&); //!< PubSubClient - - PubSubClient& setServer(IPAddress ip, uint16_t port); //!< setServer - PubSubClient& setServer(uint8_t * ip, uint16_t port); //!< setServer - PubSubClient& setServer(const char * domain, uint16_t port); //!< setServer - PubSubClient& setCallback(MQTT_CALLBACK_SIGNATURE); //!< setCallback - PubSubClient& setClient(Client& client); //!< setClient - PubSubClient& setStream(Stream& stream); //!< setStream - - boolean connect(const char* id); //!< connect - boolean connect(const char* id, const char* user, const char* pass); //!< connect - boolean connect(const char* id, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage); //!< connect - boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage); //!< connect - void disconnect(); //!< disconnect - boolean publish(const char* topic, const char* payload); //!< publish - boolean publish(const char* topic, const char* payload, boolean retained); //!< publish - boolean publish(const char* topic, const uint8_t * payload, unsigned int plength); //!< publish - boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained); //!< publish - boolean publish_P(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained); //!< publish_P - boolean subscribe(const char* topic); //!< subscribe - boolean subscribe(const char* topic, uint8_t qos); //!< subscribe - boolean unsubscribe(const char* topic); //!< unsubscribe - boolean loop(); //!< loop - boolean connected(); //!< connected - int state(); //!< state -}; - - -#endif diff --git a/libraries/PubSubClient/tests/.gitignore b/libraries/PubSubClient/tests/.gitignore deleted file mode 100644 index 215de78..0000000 --- a/libraries/PubSubClient/tests/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -.build -tmpbin -logs -*.pyc diff --git a/libraries/PubSubClient/tests/Makefile b/libraries/PubSubClient/tests/Makefile deleted file mode 100644 index 1f71636..0000000 --- a/libraries/PubSubClient/tests/Makefile +++ /dev/null @@ -1,25 +0,0 @@ -SRC_PATH=./src -OUT_PATH=./bin -TEST_SRC=$(wildcard ${SRC_PATH}/*_spec.cpp) -TEST_BIN= $(TEST_SRC:${SRC_PATH}/%.cpp=${OUT_PATH}/%) -VPATH=${SRC_PATH} -SHIM_FILES=${SRC_PATH}/lib/*.cpp -PSC_FILE=../src/PubSubClient.cpp -CC=g++ -CFLAGS=-I${SRC_PATH}/lib -I../src - -all: $(TEST_BIN) - -${OUT_PATH}/%: ${SRC_PATH}/%.cpp ${PSC_FILE} ${SHIM_FILES} - mkdir -p ${OUT_PATH} - ${CC} ${CFLAGS} $^ -o $@ - -clean: - @rm -rf ${OUT_PATH} - -test: - @bin/connect_spec - @bin/publish_spec - @bin/receive_spec - @bin/subscribe_spec - @bin/keepalive_spec diff --git a/libraries/PubSubClient/tests/README.md b/libraries/PubSubClient/tests/README.md deleted file mode 100644 index e5700a6..0000000 --- a/libraries/PubSubClient/tests/README.md +++ /dev/null @@ -1,93 +0,0 @@ -# Arduino Client for MQTT Test Suite - -This is a regression test suite for the `PubSubClient` library. - -There are two parts: - - - Tests that can be compiled and run on any machine - - Tests that build the example sketches using the Arduino IDE - - -It is a work-in-progress and is subject to complete refactoring as the whim takes -me. - - -## Local tests - -These are a set of executables that can be run to test specific areas of functionality. -They do not require a real Arduino to be attached, nor the use of the Arduino IDE. - -The tests include a set of mock files to stub out the parts of the Arduino environment the library -depends on. - -### Dependencies - - - g++ - -### Running - -Build the tests using the provided `Makefile`: - - $ make - -This will create a set of executables in `./bin/`. Run each of these executables to test the corresponding functionality. - -*Note:* the `connect_spec` and `keepalive_spec` tests involve testing keepalive timers so naturally take a few minutes to run through. - -## Arduino tests - -*Note:* INO Tool doesn't currently play nicely with Arduino 1.5. This has broken this test suite. - -Without a suitable arduino plugged in, the test suite will only check the -example sketches compile cleanly against the library. - -With an arduino plugged in, each sketch that has a corresponding python -test case is built, uploaded and then the tests run. - -### Dependencies - - - Python 2.7+ - - [INO Tool](http://inotool.org/) - this provides command-line build/upload of Arduino sketches - -### Running - -The test suite _does not_ run an MQTT server - it is assumed to be running already. - - $ python testsuite.py - -A summary of activity is printed to the console. More comprehensive logs are written -to the `logs` directory. - -### What it does - -For each sketch in the library's `examples` directory, e.g. `mqtt_basic.ino`, the suite looks for a matching test case -`testcases/mqtt_basic.py`. - -The test case must follow these conventions: - - sub-class `unittest.TestCase` - - provide the class methods `setUpClass` and `tearDownClass` (TODO: make this optional) - - all test method names begin with `test_` - -The suite will call the `setUpClass` method _before_ uploading the sketch. This -allows any test setup to be performed before the sketch runs - such as connecting -a client and subscribing to topics. - - -### Settings - -The file `testcases/settings.py` is used to config the test environment. - - - `server_ip` - the IP address of the broker the client should connect to (the broker port is assumed to be 1883). - - `arduino_ip` - the IP address the arduino should use (when not testing DHCP). - -Before each sketch is compiled, these values are automatically substituted in. To -do this, the suite looks for lines that _start_ with the following: - - byte server[] = { - byte ip[] = { - -and replaces them with the appropriate values. - - - - diff --git a/libraries/PubSubClient/tests/src/connect_spec.cpp b/libraries/PubSubClient/tests/src/connect_spec.cpp deleted file mode 100644 index 69f1864..0000000 --- a/libraries/PubSubClient/tests/src/connect_spec.cpp +++ /dev/null @@ -1,256 +0,0 @@ -#include "PubSubClient.h" -#include "ShimClient.h" -#include "Buffer.h" -#include "BDDTest.h" -#include "trace.h" - - -byte server[] = { 172, 16, 0, 2 }; - -void callback(char* topic, byte* payload, unsigned int length) { - // handle message arrived -} - - -int test_connect_fails_no_network() { - IT("fails to connect if underlying client doesn't connect"); - ShimClient shimClient; - shimClient.setAllowConnect(false); - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_FALSE(rc); - int state = client.state(); - IS_TRUE(state == MQTT_CONNECT_FAILED); - END_IT -} - -int test_connect_fails_on_no_response() { - IT("fails to connect if no response received after 15 seconds"); - ShimClient shimClient; - shimClient.setAllowConnect(true); - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_FALSE(rc); - int state = client.state(); - IS_TRUE(state == MQTT_CONNECTION_TIMEOUT); - END_IT -} - -int test_connect_properly_formatted() { - IT("sends a properly formatted connect packet and succeeds"); - ShimClient shimClient; - - shimClient.setAllowConnect(true); - byte expectServer[] = { 172, 16, 0, 2 }; - shimClient.expectConnect(expectServer,1883); - byte connect[] = {0x10,0x18,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31}; - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - - shimClient.expect(connect,26); - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int state = client.state(); - IS_TRUE(state == MQTT_DISCONNECTED); - - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - IS_FALSE(shimClient.error()); - - state = client.state(); - IS_TRUE(state == MQTT_CONNECTED); - - END_IT -} - -int test_connect_properly_formatted_hostname() { - IT("accepts a hostname"); - ShimClient shimClient; - - shimClient.setAllowConnect(true); - shimClient.expectConnect((char* const)"localhost",1883); - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client((char* const)"localhost", 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - IS_FALSE(shimClient.error()); - - END_IT -} - - -int test_connect_fails_on_bad_rc() { - IT("fails to connect if a bad return code is received"); - ShimClient shimClient; - shimClient.setAllowConnect(true); - byte connack[] = { 0x20, 0x02, 0x00, 0x01 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_FALSE(rc); - - int state = client.state(); - IS_TRUE(state == 0x01); - - END_IT -} - -int test_connect_accepts_username_password() { - IT("accepts a username and password"); - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connect[] = { 0x10,0x24,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0xc2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x4,0x75,0x73,0x65,0x72,0x0,0x4,0x70,0x61,0x73,0x73}; - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.expect(connect,0x26); - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1",(char*)"user",(char*)"pass"); - IS_TRUE(rc); - IS_FALSE(shimClient.error()); - - END_IT -} - -int test_connect_accepts_username_no_password() { - IT("accepts a username but no password"); - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connect[] = { 0x10,0x1e,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x82,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x4,0x75,0x73,0x65,0x72}; - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.expect(connect,0x20); - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1",(char*)"user",0); - IS_TRUE(rc); - IS_FALSE(shimClient.error()); - - END_IT -} - -int test_connect_ignores_password_no_username() { - IT("ignores a password but no username"); - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connect[] = {0x10,0x18,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31}; - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.expect(connect,26); - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1",0,(char*)"pass"); - IS_TRUE(rc); - IS_FALSE(shimClient.error()); - - END_IT -} - -int test_connect_with_will() { - IT("accepts a will"); - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connect[] = {0x10,0x30,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0xe,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x9,0x77,0x69,0x6c,0x6c,0x54,0x6f,0x70,0x69,0x63,0x0,0xb,0x77,0x69,0x6c,0x6c,0x4d,0x65,0x73,0x73,0x61,0x67,0x65}; - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.expect(connect,0x32); - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1",(char*)"willTopic",1,0,(char*)"willMessage"); - IS_TRUE(rc); - IS_FALSE(shimClient.error()); - - END_IT -} - -int test_connect_with_will_username_password() { - IT("accepts a will, username and password"); - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connect[] = {0x10,0x40,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0xce,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x9,0x77,0x69,0x6c,0x6c,0x54,0x6f,0x70,0x69,0x63,0x0,0xb,0x77,0x69,0x6c,0x6c,0x4d,0x65,0x73,0x73,0x61,0x67,0x65,0x0,0x4,0x75,0x73,0x65,0x72,0x0,0x8,0x70,0x61,0x73,0x73,0x77,0x6f,0x72,0x64}; - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.expect(connect,0x42); - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1",(char*)"user",(char*)"password",(char*)"willTopic",1,0,(char*)"willMessage"); - IS_TRUE(rc); - IS_FALSE(shimClient.error()); - - END_IT -} - -int test_connect_disconnect_connect() { - IT("connects, disconnects and connects again"); - ShimClient shimClient; - - shimClient.setAllowConnect(true); - byte expectServer[] = { 172, 16, 0, 2 }; - shimClient.expectConnect(expectServer,1883); - byte connect[] = {0x10,0x18,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31}; - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - - shimClient.expect(connect,26); - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - - int state = client.state(); - IS_TRUE(state == MQTT_DISCONNECTED); - - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - IS_FALSE(shimClient.error()); - - state = client.state(); - IS_TRUE(state == MQTT_CONNECTED); - - byte disconnect[] = {0xE0,0x00}; - shimClient.expect(disconnect,2); - - client.disconnect(); - - IS_FALSE(client.connected()); - IS_FALSE(shimClient.connected()); - IS_FALSE(shimClient.error()); - - state = client.state(); - IS_TRUE(state == MQTT_DISCONNECTED); - - shimClient.expect(connect,28); - shimClient.respond(connack,4); - rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - IS_FALSE(shimClient.error()); - state = client.state(); - IS_TRUE(state == MQTT_CONNECTED); - - END_IT -} - -int main() -{ - SUITE("Connect"); - test_connect_fails_no_network(); - test_connect_fails_on_no_response(); - - test_connect_properly_formatted(); - test_connect_accepts_username_password(); - test_connect_fails_on_bad_rc(); - test_connect_properly_formatted_hostname(); - - test_connect_accepts_username_no_password(); - test_connect_ignores_password_no_username(); - test_connect_with_will(); - test_connect_with_will_username_password(); - test_connect_disconnect_connect(); - FINISH -} diff --git a/libraries/PubSubClient/tests/src/keepalive_spec.cpp b/libraries/PubSubClient/tests/src/keepalive_spec.cpp deleted file mode 100644 index ea643cf..0000000 --- a/libraries/PubSubClient/tests/src/keepalive_spec.cpp +++ /dev/null @@ -1,185 +0,0 @@ -#include "PubSubClient.h" -#include "ShimClient.h" -#include "Buffer.h" -#include "BDDTest.h" -#include "trace.h" -#include - -byte server[] = { 172, 16, 0, 2 }; - -void callback(char* topic, byte* payload, unsigned int length) { - // handle message arrived -} - - -int test_keepalive_pings_idle() { - IT("keeps an idle connection alive (takes 1 minute)"); - - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - byte pingreq[] = { 0xC0,0x0 }; - shimClient.expect(pingreq,2); - byte pingresp[] = { 0xD0,0x0 }; - shimClient.respond(pingresp,2); - - for (int i = 0; i < 50; i++) { - sleep(1); - if ( i == 15 || i == 31 || i == 47) { - shimClient.expect(pingreq,2); - shimClient.respond(pingresp,2); - } - rc = client.loop(); - IS_TRUE(rc); - } - - IS_FALSE(shimClient.error()); - - END_IT -} - -int test_keepalive_pings_with_outbound_qos0() { - IT("keeps a connection alive that only sends qos0 (takes 1 minute)"); - - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; - - for (int i = 0; i < 50; i++) { - TRACE(i<<":"); - shimClient.expect(publish,16); - rc = client.publish((char*)"topic",(char*)"payload"); - IS_TRUE(rc); - IS_FALSE(shimClient.error()); - sleep(1); - if ( i == 15 || i == 31 || i == 47) { - byte pingreq[] = { 0xC0,0x0 }; - shimClient.expect(pingreq,2); - byte pingresp[] = { 0xD0,0x0 }; - shimClient.respond(pingresp,2); - } - rc = client.loop(); - IS_TRUE(rc); - IS_FALSE(shimClient.error()); - } - - END_IT -} - -int test_keepalive_pings_with_inbound_qos0() { - IT("keeps a connection alive that only receives qos0 (takes 1 minute)"); - - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; - - for (int i = 0; i < 50; i++) { - TRACE(i<<":"); - sleep(1); - if ( i == 15 || i == 31 || i == 47) { - byte pingreq[] = { 0xC0,0x0 }; - shimClient.expect(pingreq,2); - byte pingresp[] = { 0xD0,0x0 }; - shimClient.respond(pingresp,2); - } - shimClient.respond(publish,16); - rc = client.loop(); - IS_TRUE(rc); - IS_FALSE(shimClient.error()); - } - - END_IT -} - -int test_keepalive_no_pings_inbound_qos1() { - IT("does not send pings for connections with inbound qos1 (takes 1 minute)"); - - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - byte publish[] = {0x32,0x10,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x12,0x34,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; - byte puback[] = {0x40,0x2,0x12,0x34}; - - for (int i = 0; i < 50; i++) { - shimClient.respond(publish,18); - shimClient.expect(puback,4); - sleep(1); - rc = client.loop(); - IS_TRUE(rc); - IS_FALSE(shimClient.error()); - } - - END_IT -} - -int test_keepalive_disconnects_hung() { - IT("disconnects a hung connection (takes 30 seconds)"); - - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - byte pingreq[] = { 0xC0,0x0 }; - shimClient.expect(pingreq,2); - - for (int i = 0; i < 32; i++) { - sleep(1); - rc = client.loop(); - } - IS_FALSE(rc); - - int state = client.state(); - IS_TRUE(state == MQTT_CONNECTION_TIMEOUT); - - IS_FALSE(shimClient.error()); - - END_IT -} - -int main() -{ - SUITE("Keep-alive"); - test_keepalive_pings_idle(); - test_keepalive_pings_with_outbound_qos0(); - test_keepalive_pings_with_inbound_qos0(); - test_keepalive_no_pings_inbound_qos1(); - test_keepalive_disconnects_hung(); - - FINISH -} diff --git a/libraries/PubSubClient/tests/src/lib/Arduino.h b/libraries/PubSubClient/tests/src/lib/Arduino.h deleted file mode 100644 index c675280..0000000 --- a/libraries/PubSubClient/tests/src/lib/Arduino.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef Arduino_h -#define Arduino_h - -#include -#include -#include -#include - - -extern "C"{ - typedef uint8_t byte ; - typedef uint8_t boolean ; - - /* sketch */ - extern void setup( void ) ; - extern void loop( void ) ; - uint32_t millis( void ); -} - -#define PROGMEM -#define pgm_read_byte_near(x) *(x) - -#endif // Arduino_h diff --git a/libraries/PubSubClient/tests/src/lib/BDDTest.cpp b/libraries/PubSubClient/tests/src/lib/BDDTest.cpp deleted file mode 100644 index a72bf65..0000000 --- a/libraries/PubSubClient/tests/src/lib/BDDTest.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include "BDDTest.h" -#include "trace.h" -#include -#include -#include -#include - -int testCount = 0; -int testPasses = 0; -const char* testDescription; - -std::list failureList; - -void bddtest_suite(const char* name) { - LOG(name << "\n"); -} - -int bddtest_test(const char* file, int line, const char* assertion, int result) { - if (!result) { - LOG("✗\n"); - std::ostringstream os; - os << " ! "<::iterator it = failureList.begin(); it != failureList.end(); it++) { - LOG("\n"); - LOG(*it); - LOG("\n"); - } - - LOG(std::dec << testPasses << "/" << testCount << " tests passed\n\n"); - if (testPasses == testCount) { - return 0; - } - return 1; -} diff --git a/libraries/PubSubClient/tests/src/lib/BDDTest.h b/libraries/PubSubClient/tests/src/lib/BDDTest.h deleted file mode 100644 index 1197fdd..0000000 --- a/libraries/PubSubClient/tests/src/lib/BDDTest.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef bddtest_h -#define bddtest_h - -void bddtest_suite(const char* name); -int bddtest_test(const char*, int, const char*, int); -void bddtest_start(const char*); -void bddtest_end(); -int bddtest_summary(); - -#define SUITE(x) { bddtest_suite(x); } -#define TEST(x) { if (!bddtest_test(__FILE__, __LINE__, #x, (x))) return false; } - -#define IT(x) { bddtest_start(x); } -#define END_IT { bddtest_end();return true;} - -#define FINISH { return bddtest_summary(); } - -#define IS_TRUE(x) TEST(x) -#define IS_FALSE(x) TEST(!(x)) -#define IS_EQUAL(x,y) TEST(x==y) -#define IS_NOT_EQUAL(x,y) TEST(x!=y) - -#endif diff --git a/libraries/PubSubClient/tests/src/lib/Buffer.cpp b/libraries/PubSubClient/tests/src/lib/Buffer.cpp deleted file mode 100644 index 59a2fbb..0000000 --- a/libraries/PubSubClient/tests/src/lib/Buffer.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include "Buffer.h" -#include "Arduino.h" - -Buffer::Buffer() { -} - -Buffer::Buffer(uint8_t* buf, size_t size) { - this->add(buf,size); -} -bool Buffer::available() { - return this->pos < this->length; -} - -uint8_t Buffer::next() { - if (this->available()) { - return this->buffer[this->pos++]; - } - return 0; -} - -void Buffer::reset() { - this->pos = 0; -} - -void Buffer::add(uint8_t* buf, size_t size) { - uint16_t i = 0; - for (;ibuffer[this->length++] = buf[i]; - } -} diff --git a/libraries/PubSubClient/tests/src/lib/Buffer.h b/libraries/PubSubClient/tests/src/lib/Buffer.h deleted file mode 100644 index e05d38e..0000000 --- a/libraries/PubSubClient/tests/src/lib/Buffer.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef buffer_h -#define buffer_h - -#include "Arduino.h" - -/** Buffer class */ -class Buffer { -private: - uint8_t buffer[1024]; - uint16_t pos; - uint16_t length; - -public: - Buffer(); //!< Buffer - Buffer(uint8_t* buf, size_t size); //!< Buffer - - virtual bool available(); //!< available - virtual uint8_t next(); //!< next - virtual void reset(); //!< reset - - virtual void add(uint8_t* buf, size_t size); //!< add -}; - -#endif diff --git a/libraries/PubSubClient/tests/src/lib/Client.h b/libraries/PubSubClient/tests/src/lib/Client.h deleted file mode 100644 index 29d53b8..0000000 --- a/libraries/PubSubClient/tests/src/lib/Client.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef client_h -#define client_h -#include "IPAddress.h" - -/** Client class */ -class Client { -public: - virtual int connect(IPAddress ip, uint16_t port) =0; //!< connect - virtual int connect(const char *host, uint16_t port) =0; //!< connect - virtual size_t write(uint8_t) =0; //!< write - virtual size_t write(const uint8_t *buf, size_t size) =0; //!< write - virtual int available() = 0; //!< available - virtual int read() = 0; //!< read - virtual int read(uint8_t *buf, size_t size) = 0; //!< read - virtual int peek() = 0; //!< peek - virtual void flush() = 0; //!< flush - virtual void stop() = 0; //!< stop - virtual uint8_t connected() = 0; //!< connected - virtual operator bool() = 0; //!< bool operator -}; - -#endif diff --git a/libraries/PubSubClient/tests/src/lib/IPAddress.cpp b/libraries/PubSubClient/tests/src/lib/IPAddress.cpp deleted file mode 100644 index 610ff4c..0000000 --- a/libraries/PubSubClient/tests/src/lib/IPAddress.cpp +++ /dev/null @@ -1,44 +0,0 @@ - -#include -#include - -IPAddress::IPAddress() -{ - memset(_address, 0, sizeof(_address)); -} - -IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) -{ - _address[0] = first_octet; - _address[1] = second_octet; - _address[2] = third_octet; - _address[3] = fourth_octet; -} - -IPAddress::IPAddress(uint32_t address) -{ - memcpy(_address, &address, sizeof(_address)); -} - -IPAddress::IPAddress(const uint8_t *address) -{ - memcpy(_address, address, sizeof(_address)); -} - -IPAddress& IPAddress::operator=(const uint8_t *address) -{ - memcpy(_address, address, sizeof(_address)); - return *this; -} - -IPAddress& IPAddress::operator=(uint32_t address) -{ - memcpy(_address, (const uint8_t *)&address, sizeof(_address)); - return *this; -} - -bool IPAddress::operator==(const uint8_t* addr) -{ - return memcmp(addr, _address, sizeof(_address)) == 0; -} - diff --git a/libraries/PubSubClient/tests/src/lib/IPAddress.h b/libraries/PubSubClient/tests/src/lib/IPAddress.h deleted file mode 100644 index 3871074..0000000 --- a/libraries/PubSubClient/tests/src/lib/IPAddress.h +++ /dev/null @@ -1,80 +0,0 @@ -/* - * - * MIT License: - * Copyright (c) 2011 Adrian McEwen - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * adrianm@mcqn.com 1/1/2011 - */ - -#ifndef IPAddress_h -#define IPAddress_h - - -// A class to make it easier to handle and pass around IP addresses - -/** IPAddress class */ -class IPAddress { -private: - uint8_t _address[4]; // IPv4 address - // Access the raw byte array containing the address. Because this returns a pointer - // to the internal structure rather than a copy of the address this function should only - // be used when you know that the usage of the returned uint8_t* will be transient and not - // stored. - uint8_t* raw_address() { return _address; }; - -public: - // Constructors - IPAddress(); //!< IPAddress - IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); //!< IPAddress - IPAddress(uint32_t address); //!< IPAddress - IPAddress(const uint8_t *address); //!< IPAddress - - // Overloaded cast operator to allow IPAddress objects to be used where a pointer - // to a four-byte uint8_t array is expected - /** @brief operator overload */ - operator uint32_t() { return *((uint32_t*)_address); }; - /** @brief operator overload */ - bool operator==(const IPAddress& addr) { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); }; - /** @brief operator overload */ - bool operator==(const uint8_t* addr); - - // Overloaded index operator to allow getting and setting individual octets of the address - /** @brief operator overload */ - uint8_t operator[](int index) const { return _address[index]; }; - /** @brief operator overload */ - uint8_t& operator[](int index) { return _address[index]; }; - - // Overloaded copy operators to allow initialisation of IPAddress objects from other types - /** @brief operator overload */ - IPAddress& operator=(const uint8_t *address); - /** @brief operator overload */ - IPAddress& operator=(uint32_t address); - - - friend class EthernetClass; - friend class UDP; - friend class Client; - friend class Server; - friend class DhcpClass; - friend class DNSClient; -}; - - -#endif diff --git a/libraries/PubSubClient/tests/src/lib/ShimClient.cpp b/libraries/PubSubClient/tests/src/lib/ShimClient.cpp deleted file mode 100644 index f70115f..0000000 --- a/libraries/PubSubClient/tests/src/lib/ShimClient.cpp +++ /dev/null @@ -1,153 +0,0 @@ -#include "ShimClient.h" -#include "trace.h" -#include -#include -#include - -extern "C" { - uint32_t millis(void) { - return time(0)*1000; - } -} - -ShimClient::ShimClient() { - this->responseBuffer = new Buffer(); - this->expectBuffer = new Buffer(); - this->_allowConnect = true; - this->_connected = false; - this->_error = false; - this->expectAnything = true; - this->_received = 0; - this->_expectedPort = 0; -} - -int ShimClient::connect(IPAddress ip, uint16_t port) { - if (this->_allowConnect) { - this->_connected = true; - } - if (this->_expectedPort !=0) { - // if (memcmp(ip,this->_expectedIP,4) != 0) { - // TRACE( "ip mismatch\n"); - // this->_error = true; - // } - if (port != this->_expectedPort) { - TRACE( "port mismatch\n"); - this->_error = true; - } - } - return this->_connected; -} -int ShimClient::connect(const char *host, uint16_t port) { - if (this->_allowConnect) { - this->_connected = true; - } - if (this->_expectedPort !=0) { - if (strcmp(host,this->_expectedHost) != 0) { - TRACE( "host mismatch\n"); - this->_error = true; - } - if (port != this->_expectedPort) { - TRACE( "port mismatch\n"); - this->_error = true; - } - - } - return this->_connected; -} -size_t ShimClient::write(uint8_t b) { - this->_received += 1; - TRACE(std::hex << (unsigned int)b); - if (!this->expectAnything) { - if (this->expectBuffer->available()) { - uint8_t expected = this->expectBuffer->next(); - if (expected != b) { - this->_error = true; - TRACE("!=" << (unsigned int)expected); - } - } else { - this->_error = true; - } - } - TRACE("\n"<< std::dec); - return 1; -} -size_t ShimClient::write(const uint8_t *buf, size_t size) { - this->_received += size; - TRACE( "[" << std::dec << (unsigned int)(size) << "] "); - uint16_t i=0; - for (;i0) { - TRACE(":"); - } - TRACE(std::hex << (unsigned int)(buf[i])); - - if (!this->expectAnything) { - if (this->expectBuffer->available()) { - uint8_t expected = this->expectBuffer->next(); - if (expected != buf[i]) { - this->_error = true; - TRACE("!=" << (unsigned int)expected); - } - } else { - this->_error = true; - } - } - } - TRACE("\n"<responseBuffer->available(); -} -int ShimClient::read() { return this->responseBuffer->next(); } -int ShimClient::read(uint8_t *buf, size_t size) { - uint16_t i = 0; - for (;iread(); - } - return size; -} -int ShimClient::peek() { return 0; } -void ShimClient::flush() {} -void ShimClient::stop() { - this->setConnected(false); -} -uint8_t ShimClient::connected() { return this->_connected; } -ShimClient::operator bool() { return true; } - - -ShimClient* ShimClient::respond(uint8_t *buf, size_t size) { - this->responseBuffer->add(buf,size); - return this; -} - -ShimClient* ShimClient::expect(uint8_t *buf, size_t size) { - this->expectAnything = false; - this->expectBuffer->add(buf,size); - return this; -} - -void ShimClient::setConnected(bool b) { - this->_connected = b; -} -void ShimClient::setAllowConnect(bool b) { - this->_allowConnect = b; -} - -bool ShimClient::error() { - return this->_error; -} - -uint16_t ShimClient::received() { - return this->_received; -} - -void ShimClient::expectConnect(IPAddress ip, uint16_t port) { - this->_expectedIP = ip; - this->_expectedPort = port; -} - -void ShimClient::expectConnect(const char *host, uint16_t port) { - this->_expectedHost = host; - this->_expectedPort = port; -} diff --git a/libraries/PubSubClient/tests/src/lib/ShimClient.h b/libraries/PubSubClient/tests/src/lib/ShimClient.h deleted file mode 100644 index ea87bae..0000000 --- a/libraries/PubSubClient/tests/src/lib/ShimClient.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef shimclient_h -#define shimclient_h - -#include "Arduino.h" -#include "Client.h" -#include "IPAddress.h" -#include "Buffer.h" - - -/** ShimClient class */ -class ShimClient : public Client { -private: - Buffer* responseBuffer; - Buffer* expectBuffer; - bool _allowConnect; - bool _connected; - bool expectAnything; - bool _error; - uint16_t _received; - IPAddress _expectedIP; - uint16_t _expectedPort; - const char* _expectedHost; - -public: - ShimClient(); - virtual int connect(IPAddress ip, uint16_t port); //!< connect - virtual int connect(const char *host, uint16_t port); //!< connect - virtual size_t write(uint8_t); //!< write - virtual size_t write(const uint8_t *buf, size_t size); //!< write - virtual int available(); //!< available - virtual int read(); //!< read - virtual int read(uint8_t *buf, size_t size); //!< read - virtual int peek(); //!< peek - virtual void flush(); //!< flush - virtual void stop(); //!< stop - virtual uint8_t connected(); //!< connected - virtual operator bool(); //!< bool operator - - virtual ShimClient* respond(uint8_t *buf, size_t size); //!< respond - virtual ShimClient* expect(uint8_t *buf, size_t size); //!< expect - - virtual void expectConnect(IPAddress ip, uint16_t port); //!< expectConnect - virtual void expectConnect(const char *host, uint16_t port); //!< expectConnect - - virtual uint16_t received(); //!< received - virtual bool error(); //!< error - - virtual void setAllowConnect(bool b); //!< setAllowConnect - virtual void setConnected(bool b); //!< setConnected -}; - -#endif diff --git a/libraries/PubSubClient/tests/src/lib/Stream.cpp b/libraries/PubSubClient/tests/src/lib/Stream.cpp deleted file mode 100644 index b0ecbb4..0000000 --- a/libraries/PubSubClient/tests/src/lib/Stream.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "Stream.h" -#include "trace.h" -#include -#include - -Stream::Stream() { - this->expectBuffer = new Buffer(); - this->_error = false; - this->_written = 0; -} - -size_t Stream::write(uint8_t b) { - this->_written++; - TRACE(std::hex << (unsigned int)b); - if (this->expectBuffer->available()) { - uint8_t expected = this->expectBuffer->next(); - if (expected != b) { - this->_error = true; - TRACE("!=" << (unsigned int)expected); - } - } else { - this->_error = true; - } - TRACE("\n"<< std::dec); - return 1; -} - - -bool Stream::error() { - return this->_error; -} - -void Stream::expect(uint8_t *buf, size_t size) { - this->expectBuffer->add(buf,size); -} - -uint16_t Stream::length() { - return this->_written; -} diff --git a/libraries/PubSubClient/tests/src/lib/Stream.h b/libraries/PubSubClient/tests/src/lib/Stream.h deleted file mode 100644 index ad57313..0000000 --- a/libraries/PubSubClient/tests/src/lib/Stream.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef Stream_h -#define Stream_h - -#include "Arduino.h" -#include "Buffer.h" - -/** Stream class */ -class Stream { -private: - Buffer* expectBuffer; - bool _error; - uint16_t _written; - -public: - Stream(); - virtual size_t write(uint8_t); //!< write - - virtual bool error(); //!< error - virtual void expect(uint8_t *buf, size_t size); //!< expect - virtual uint16_t length(); //!< length -}; - -#endif diff --git a/libraries/PubSubClient/tests/src/lib/trace.h b/libraries/PubSubClient/tests/src/lib/trace.h deleted file mode 100644 index 42eb991..0000000 --- a/libraries/PubSubClient/tests/src/lib/trace.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef trace_h -#define trace_h -#include - -#include - -#define LOG(x) {std::cout << x << std::flush; } -#define TRACE(x) {if (getenv("TRACE")) { std::cout << x << std::flush; }} - -#endif diff --git a/libraries/PubSubClient/tests/src/publish_spec.cpp b/libraries/PubSubClient/tests/src/publish_spec.cpp deleted file mode 100644 index 232df0d..0000000 --- a/libraries/PubSubClient/tests/src/publish_spec.cpp +++ /dev/null @@ -1,190 +0,0 @@ -#include "PubSubClient.h" -#include "ShimClient.h" -#include "Buffer.h" -#include "BDDTest.h" -#include "trace.h" - - -byte server[] = { 172, 16, 0, 2 }; - -void callback(char* topic, byte* payload, unsigned int length) { - // handle message arrived -} - -int test_publish() { - IT("publishes a null-terminated string"); - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; - shimClient.expect(publish,16); - - rc = client.publish((char*)"topic",(char*)"payload"); - IS_TRUE(rc); - - IS_FALSE(shimClient.error()); - - END_IT -} - - -int test_publish_bytes() { - IT("publishes a byte array"); - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte payload[] = { 0x01,0x02,0x03,0x0,0x05 }; - int length = 5; - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - byte publish[] = {0x30,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x1,0x2,0x3,0x0,0x5}; - shimClient.expect(publish,14); - - rc = client.publish((char*)"topic",payload,length); - IS_TRUE(rc); - - IS_FALSE(shimClient.error()); - - END_IT -} - - -int test_publish_retained() { - IT("publishes retained - 1"); - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte payload[] = { 0x01,0x02,0x03,0x0,0x05 }; - int length = 5; - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - byte publish[] = {0x31,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x1,0x2,0x3,0x0,0x5}; - shimClient.expect(publish,14); - - rc = client.publish((char*)"topic",payload,length,true); - IS_TRUE(rc); - - IS_FALSE(shimClient.error()); - - END_IT -} - -int test_publish_retained_2() { - IT("publishes retained - 2"); - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - byte publish[] = {0x31,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,'A','B','C','D','E'}; - shimClient.expect(publish,14); - - rc = client.publish((char*)"topic",(char*)"ABCDE",true); - IS_TRUE(rc); - - IS_FALSE(shimClient.error()); - - END_IT -} - -int test_publish_not_connected() { - IT("publish fails when not connected"); - ShimClient shimClient; - - PubSubClient client(server, 1883, callback, shimClient); - - int rc = client.publish((char*)"topic",(char*)"payload"); - IS_FALSE(rc); - - IS_FALSE(shimClient.error()); - - END_IT -} - -int test_publish_too_long() { - IT("publish fails when topic/payload are too long"); - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - // 0 1 2 3 4 5 6 7 8 9 0 1 2 - rc = client.publish((char*)"topic",(char*)"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"); - IS_FALSE(rc); - - IS_FALSE(shimClient.error()); - - END_IT -} - -int test_publish_P() { - IT("publishes using PROGMEM"); - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte payload[] = { 0x01,0x02,0x03,0x0,0x05 }; - int length = 5; - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - byte publish[] = {0x31,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x1,0x2,0x3,0x0,0x5}; - shimClient.expect(publish,14); - - rc = client.publish_P((char*)"topic",payload,length,true); - IS_TRUE(rc); - - IS_FALSE(shimClient.error()); - - END_IT -} - - - - -int main() -{ - SUITE("Publish"); - test_publish(); - test_publish_bytes(); - test_publish_retained(); - test_publish_retained_2(); - test_publish_not_connected(); - test_publish_too_long(); - test_publish_P(); - - FINISH -} diff --git a/libraries/PubSubClient/tests/src/receive_spec.cpp b/libraries/PubSubClient/tests/src/receive_spec.cpp deleted file mode 100644 index 54a62ee..0000000 --- a/libraries/PubSubClient/tests/src/receive_spec.cpp +++ /dev/null @@ -1,249 +0,0 @@ -#include "PubSubClient.h" -#include "ShimClient.h" -#include "Buffer.h" -#include "BDDTest.h" -#include "trace.h" - - -byte server[] = { 172, 16, 0, 2 }; - -bool callback_called = false; -char lastTopic[1024]; -char lastPayload[1024]; -unsigned int lastLength; - -void reset_callback() { - callback_called = false; - lastTopic[0] = '\0'; - lastPayload[0] = '\0'; - lastLength = 0; -} - -void callback(char* topic, byte* payload, unsigned int length) { - callback_called = true; - strcpy(lastTopic,topic); - memcpy(lastPayload,payload,length); - lastLength = length; -} - -int test_receive_callback() { - IT("receives a callback message"); - reset_callback(); - - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; - shimClient.respond(publish,16); - - rc = client.loop(); - - IS_TRUE(rc); - - IS_TRUE(callback_called); - IS_TRUE(strcmp(lastTopic,"topic")==0); - IS_TRUE(memcmp(lastPayload,"payload",7)==0); - IS_TRUE(lastLength == 7); - - IS_FALSE(shimClient.error()); - - END_IT -} - -int test_receive_stream() { - IT("receives a streamed callback message"); - reset_callback(); - - Stream stream; - stream.expect((uint8_t*)"payload",7); - - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient, stream); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; - shimClient.respond(publish,16); - - rc = client.loop(); - - IS_TRUE(rc); - - IS_TRUE(callback_called); - IS_TRUE(strcmp(lastTopic,"topic")==0); - IS_TRUE(lastLength == 7); - - IS_FALSE(stream.error()); - IS_FALSE(shimClient.error()); - - END_IT -} - -int test_receive_max_sized_message() { - IT("receives an max-sized message"); - reset_callback(); - - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - int length = MQTT_MAX_PACKET_SIZE; - byte publish[] = {0x30,length-2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; - byte bigPublish[length]; - memset(bigPublish,'A',length); - bigPublish[length] = 'B'; - memcpy(bigPublish,publish,16); - shimClient.respond(bigPublish,length); - - rc = client.loop(); - - IS_TRUE(rc); - - IS_TRUE(callback_called); - IS_TRUE(strcmp(lastTopic,"topic")==0); - IS_TRUE(lastLength == length-9); - IS_TRUE(memcmp(lastPayload,bigPublish+9,lastLength)==0); - - IS_FALSE(shimClient.error()); - - END_IT -} - -int test_receive_oversized_message() { - IT("drops an oversized message"); - reset_callback(); - - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - int length = MQTT_MAX_PACKET_SIZE+1; - byte publish[] = {0x30,length-2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; - byte bigPublish[length]; - memset(bigPublish,'A',length); - bigPublish[length] = 'B'; - memcpy(bigPublish,publish,16); - shimClient.respond(bigPublish,length); - - rc = client.loop(); - - IS_TRUE(rc); - - IS_FALSE(callback_called); - - IS_FALSE(shimClient.error()); - - END_IT -} - -int test_receive_oversized_stream_message() { - IT("drops an oversized message"); - reset_callback(); - - Stream stream; - - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient, stream); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - int length = MQTT_MAX_PACKET_SIZE+1; - byte publish[] = {0x30,length-2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; - - byte bigPublish[length]; - memset(bigPublish,'A',length); - bigPublish[length] = 'B'; - memcpy(bigPublish,publish,16); - - shimClient.respond(bigPublish,length); - stream.expect(bigPublish+9,length-9); - - rc = client.loop(); - - IS_TRUE(rc); - - IS_TRUE(callback_called); - IS_TRUE(strcmp(lastTopic,"topic")==0); - IS_TRUE(lastLength == length-9); - - IS_FALSE(stream.error()); - IS_FALSE(shimClient.error()); - - END_IT -} - -int test_receive_qos1() { - IT("receives a qos1 message"); - reset_callback(); - - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - byte publish[] = {0x32,0x10,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x12,0x34,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; - shimClient.respond(publish,18); - - byte puback[] = {0x40,0x2,0x12,0x34}; - shimClient.expect(puback,4); - - rc = client.loop(); - - IS_TRUE(rc); - - IS_TRUE(callback_called); - IS_TRUE(strcmp(lastTopic,"topic")==0); - IS_TRUE(memcmp(lastPayload,"payload",7)==0); - IS_TRUE(lastLength == 7); - - IS_FALSE(shimClient.error()); - - END_IT -} - -int main() -{ - SUITE("Receive"); - test_receive_callback(); - test_receive_stream(); - test_receive_max_sized_message(); - test_receive_oversized_message(); - test_receive_oversized_stream_message(); - test_receive_qos1(); - - FINISH -} diff --git a/libraries/PubSubClient/tests/src/subscribe_spec.cpp b/libraries/PubSubClient/tests/src/subscribe_spec.cpp deleted file mode 100644 index a419823..0000000 --- a/libraries/PubSubClient/tests/src/subscribe_spec.cpp +++ /dev/null @@ -1,177 +0,0 @@ -#include "PubSubClient.h" -#include "ShimClient.h" -#include "Buffer.h" -#include "BDDTest.h" -#include "trace.h" - - -byte server[] = { 172, 16, 0, 2 }; - -void callback(char* topic, byte* payload, unsigned int length) { - // handle message arrived -} - -int test_subscribe_no_qos() { - IT("subscribe without qos defaults to 0"); - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - byte subscribe[] = { 0x82,0xa,0x0,0x2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x0 }; - shimClient.expect(subscribe,12); - byte suback[] = { 0x90,0x3,0x0,0x2,0x0 }; - shimClient.respond(suback,5); - - rc = client.subscribe((char*)"topic"); - IS_TRUE(rc); - - IS_FALSE(shimClient.error()); - - END_IT -} - -int test_subscribe_qos_1() { - IT("subscribes qos 1"); - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - byte subscribe[] = { 0x82,0xa,0x0,0x2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x1 }; - shimClient.expect(subscribe,12); - byte suback[] = { 0x90,0x3,0x0,0x2,0x1 }; - shimClient.respond(suback,5); - - rc = client.subscribe((char*)"topic",1); - IS_TRUE(rc); - - IS_FALSE(shimClient.error()); - - END_IT -} - -int test_subscribe_not_connected() { - IT("subscribe fails when not connected"); - ShimClient shimClient; - - PubSubClient client(server, 1883, callback, shimClient); - - int rc = client.subscribe((char*)"topic"); - IS_FALSE(rc); - - IS_FALSE(shimClient.error()); - - END_IT -} - -int test_subscribe_invalid_qos() { - IT("subscribe fails with invalid qos values"); - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - rc = client.subscribe((char*)"topic",2); - IS_FALSE(rc); - rc = client.subscribe((char*)"topic",254); - IS_FALSE(rc); - - IS_FALSE(shimClient.error()); - - END_IT -} - -int test_subscribe_too_long() { - IT("subscribe fails with too long topic"); - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - // max length should be allowed - // 0 1 2 3 4 5 6 7 8 9 0 1 2 - rc = client.subscribe((char*)"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"); - IS_TRUE(rc); - - // 0 1 2 3 4 5 6 7 8 9 0 1 2 - rc = client.subscribe((char*)"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"); - IS_FALSE(rc); - - IS_FALSE(shimClient.error()); - - END_IT -} - - -int test_unsubscribe() { - IT("unsubscribes"); - ShimClient shimClient; - shimClient.setAllowConnect(true); - - byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; - shimClient.respond(connack,4); - - PubSubClient client(server, 1883, callback, shimClient); - int rc = client.connect((char*)"client_test1"); - IS_TRUE(rc); - - byte unsubscribe[] = { 0xA2,0x9,0x0,0x2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63 }; - shimClient.expect(unsubscribe,12); - byte unsuback[] = { 0xB0,0x2,0x0,0x2 }; - shimClient.respond(unsuback,4); - - rc = client.unsubscribe((char*)"topic"); - IS_TRUE(rc); - - IS_FALSE(shimClient.error()); - - END_IT -} - -int test_unsubscribe_not_connected() { - IT("unsubscribe fails when not connected"); - ShimClient shimClient; - - PubSubClient client(server, 1883, callback, shimClient); - - int rc = client.unsubscribe((char*)"topic"); - IS_FALSE(rc); - - IS_FALSE(shimClient.error()); - - END_IT -} - -int main() -{ - SUITE("Subscribe"); - test_subscribe_no_qos(); - test_subscribe_qos_1(); - test_subscribe_not_connected(); - test_subscribe_invalid_qos(); - test_subscribe_too_long(); - test_unsubscribe(); - test_unsubscribe_not_connected(); - FINISH -} diff --git a/libraries/PubSubClient/tests/testcases/__init__.py b/libraries/PubSubClient/tests/testcases/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/libraries/PubSubClient/tests/testcases/mqtt_basic.py b/libraries/PubSubClient/tests/testcases/mqtt_basic.py deleted file mode 100644 index 1b0cc65..0000000 --- a/libraries/PubSubClient/tests/testcases/mqtt_basic.py +++ /dev/null @@ -1,43 +0,0 @@ -import unittest -import settings - -import time -import mosquitto - -import serial - -def on_message(mosq, obj, msg): - obj.message_queue.append(msg) - -class mqtt_basic(unittest.TestCase): - - message_queue = [] - - @classmethod - def setUpClass(self): - self.client = mosquitto.Mosquitto("pubsubclient_ut", clean_session=True,obj=self) - self.client.connect(settings.server_ip) - self.client.on_message = on_message - self.client.subscribe("outTopic",0) - - @classmethod - def tearDownClass(self): - self.client.disconnect() - - def test_one(self): - i=30 - while len(self.message_queue) == 0 and i > 0: - self.client.loop() - time.sleep(0.5) - i -= 1 - self.assertTrue(i>0, "message receive timed-out") - self.assertEqual(len(self.message_queue), 1, "unexpected number of messages received") - msg = self.message_queue[0] - self.assertEqual(msg.mid,0,"message id not 0") - self.assertEqual(msg.topic,"outTopic","message topic incorrect") - self.assertEqual(msg.payload,"hello world") - self.assertEqual(msg.qos,0,"message qos not 0") - self.assertEqual(msg.retain,False,"message retain flag incorrect") - - - diff --git a/libraries/PubSubClient/tests/testcases/mqtt_publish_in_callback.py b/libraries/PubSubClient/tests/testcases/mqtt_publish_in_callback.py deleted file mode 100644 index 7989f7f..0000000 --- a/libraries/PubSubClient/tests/testcases/mqtt_publish_in_callback.py +++ /dev/null @@ -1,64 +0,0 @@ -import unittest -import settings - -import time -import mosquitto - -import serial - -def on_message(mosq, obj, msg): - obj.message_queue.append(msg) - -class mqtt_publish_in_callback(unittest.TestCase): - - message_queue = [] - - @classmethod - def setUpClass(self): - self.client = mosquitto.Mosquitto("pubsubclient_ut", clean_session=True,obj=self) - self.client.connect(settings.server_ip) - self.client.on_message = on_message - self.client.subscribe("outTopic",0) - - @classmethod - def tearDownClass(self): - self.client.disconnect() - - def test_connect(self): - i=30 - while len(self.message_queue) == 0 and i > 0: - self.client.loop() - time.sleep(0.5) - i -= 1 - self.assertTrue(i>0, "message receive timed-out") - self.assertEqual(len(self.message_queue), 1, "unexpected number of messages received") - msg = self.message_queue.pop(0) - self.assertEqual(msg.mid,0,"message id not 0") - self.assertEqual(msg.topic,"outTopic","message topic incorrect") - self.assertEqual(msg.payload,"hello world") - self.assertEqual(msg.qos,0,"message qos not 0") - self.assertEqual(msg.retain,False,"message retain flag incorrect") - - - def test_publish(self): - self.assertEqual(len(self.message_queue), 0, "message queue not empty") - payload = "abcdefghij" - self.client.publish("inTopic",payload) - - i=30 - while len(self.message_queue) == 0 and i > 0: - self.client.loop() - time.sleep(0.5) - i -= 1 - - self.assertTrue(i>0, "message receive timed-out") - self.assertEqual(len(self.message_queue), 1, "unexpected number of messages received") - msg = self.message_queue.pop(0) - self.assertEqual(msg.mid,0,"message id not 0") - self.assertEqual(msg.topic,"outTopic","message topic incorrect") - self.assertEqual(msg.payload,payload) - self.assertEqual(msg.qos,0,"message qos not 0") - self.assertEqual(msg.retain,False,"message retain flag incorrect") - - - diff --git a/libraries/PubSubClient/tests/testcases/settings.py b/libraries/PubSubClient/tests/testcases/settings.py deleted file mode 100644 index 4ad8719..0000000 --- a/libraries/PubSubClient/tests/testcases/settings.py +++ /dev/null @@ -1,2 +0,0 @@ -server_ip = "172.16.0.2" -arduino_ip = "172.16.0.100" diff --git a/libraries/PubSubClient/tests/testsuite.py b/libraries/PubSubClient/tests/testsuite.py deleted file mode 100644 index 0a8e70d..0000000 --- a/libraries/PubSubClient/tests/testsuite.py +++ /dev/null @@ -1,179 +0,0 @@ -#!/usr/bin/env python -import os -import os.path -import sys -import shutil -from subprocess import call -import importlib -import unittest -import re - -from testcases import settings - -class Workspace(object): - - def __init__(self): - self.root_dir = os.getcwd() - self.build_dir = os.path.join(self.root_dir,"tmpbin"); - self.log_dir = os.path.join(self.root_dir,"logs"); - self.tests_dir = os.path.join(self.root_dir,"testcases"); - self.examples_dir = os.path.join(self.root_dir,"../PubSubClient/examples") - self.examples = [] - self.tests = [] - if not os.path.isdir("../PubSubClient"): - raise Exception("Cannot find PubSubClient library") - try: - import ino - except: - raise Exception("ino tool not installed") - - def init(self): - if os.path.isdir(self.build_dir): - shutil.rmtree(self.build_dir) - os.mkdir(self.build_dir) - if os.path.isdir(self.log_dir): - shutil.rmtree(self.log_dir) - os.mkdir(self.log_dir) - - os.chdir(self.build_dir) - call(["ino","init"]) - - shutil.copytree("../../PubSubClient","lib/PubSubClient") - - filenames = [] - for root, dirs, files in os.walk(self.examples_dir): - filenames += [os.path.join(root,f) for f in files if f.endswith(".ino")] - filenames.sort() - for e in filenames: - self.examples.append(Sketch(self,e)) - - filenames = [] - for root, dirs, files in os.walk(self.tests_dir): - filenames += [os.path.join(root,f) for f in files if f.endswith(".ino")] - filenames.sort() - for e in filenames: - self.tests.append(Sketch(self,e)) - - def clean(self): - shutil.rmtree(self.build_dir) - -class Sketch(object): - def __init__(self,wksp,fn): - self.w = wksp - self.filename = fn - self.basename = os.path.basename(self.filename) - self.build_log = os.path.join(self.w.log_dir,"%s.log"%(os.path.basename(self.filename),)) - self.build_err_log = os.path.join(self.w.log_dir,"%s.err.log"%(os.path.basename(self.filename),)) - self.build_upload_log = os.path.join(self.w.log_dir,"%s.upload.log"%(os.path.basename(self.filename),)) - - def build(self): - sys.stdout.write(" Build: ") - sys.stdout.flush() - - # Copy sketch over, replacing IP addresses as necessary - fin = open(self.filename,"r") - lines = fin.readlines() - fin.close() - fout = open(os.path.join(self.w.build_dir,"src","sketch.ino"),"w") - for l in lines: - if re.match(r"^byte server\[\] = {",l): - fout.write("byte server[] = { %s };\n"%(settings.server_ip.replace(".",", "),)) - elif re.match(r"^byte ip\[\] = {",l): - fout.write("byte ip[] = { %s };\n"%(settings.arduino_ip.replace(".",", "),)) - else: - fout.write(l) - fout.flush() - fout.close() - - # Run build - fout = open(self.build_log, "w") - ferr = open(self.build_err_log, "w") - rc = call(["ino","build"],stdout=fout,stderr=ferr) - fout.close() - ferr.close() - if rc == 0: - sys.stdout.write("pass") - sys.stdout.write("\n") - return True - else: - sys.stdout.write("fail") - sys.stdout.write("\n") - with open(self.build_err_log) as f: - for line in f: - print " ",line, - return False - - def upload(self): - sys.stdout.write(" Upload: ") - sys.stdout.flush() - fout = open(self.build_upload_log, "w") - rc = call(["ino","upload"],stdout=fout,stderr=fout) - fout.close() - if rc == 0: - sys.stdout.write("pass") - sys.stdout.write("\n") - return True - else: - sys.stdout.write("fail") - sys.stdout.write("\n") - with open(self.build_upload_log) as f: - for line in f: - print " ",line, - return False - - - def test(self): - # import the matching test case, if it exists - try: - basename = os.path.basename(self.filename)[:-4] - i = importlib.import_module("testcases."+basename) - except: - sys.stdout.write(" Test: no tests found") - sys.stdout.write("\n") - return - c = getattr(i,basename) - - testmethods = [m for m in dir(c) if m.startswith("test_")] - testmethods.sort() - tests = [] - for m in testmethods: - tests.append(c(m)) - - result = unittest.TestResult() - c.setUpClass() - if self.upload(): - sys.stdout.write(" Test: ") - sys.stdout.flush() - for t in tests: - t.run(result) - print "%d/%d"%(result.testsRun-len(result.failures)-len(result.errors),result.testsRun) - if not result.wasSuccessful(): - if len(result.failures) > 0: - for f in result.failures: - print "-- %s"%(str(f[0]),) - print f[1] - if len(result.errors) > 0: - print " Errors:" - for f in result.errors: - print "-- %s"%(str(f[0]),) - print f[1] - c.tearDownClass() - -if __name__ == '__main__': - run_tests = True - - w = Workspace() - w.init() - - for e in w.examples: - print "--------------------------------------" - print "[%s]"%(e.basename,) - if e.build() and run_tests: - e.test() - for e in w.tests: - print "--------------------------------------" - print "[%s]"%(e.basename,) - if e.build() and run_tests: - e.test() - - w.clean() diff --git a/libraries/RunningAverage/RunningAverage.cpp b/libraries/RunningAverage/RunningAverage.cpp deleted file mode 100644 index ae26b8b..0000000 --- a/libraries/RunningAverage/RunningAverage.cpp +++ /dev/null @@ -1,101 +0,0 @@ -// -// FILE: RunningAverage.cpp -// AUTHOR: Rob Tillaart -// VERSION: 0.2.08 -// DATE: 2015-apr-10 -// PURPOSE: RunningAverage library for Arduino -// -// The library stores N individual values in a circular buffer, -// to calculate the running average. -// -// HISTORY: -// 0.1.00 - 2011-01-30 initial version -// 0.1.01 - 2011-02-28 fixed missing destructor in .h -// 0.2.00 - 2012-??-?? Yuval Naveh added trimValue (found on web) -// http://stromputer.googlecode.com/svn-history/r74/trunk/Arduino/Libraries/RunningAverage/RunningAverage.cpp -// 0.2.01 - 2012-11-21 refactored -// 0.2.02 - 2012-12-30 refactored trimValue -> fillValue -// 0.2.03 - 2013-11-31 getElement -// 0.2.04 - 2014-07-03 added memory protection -// 0.2.05 - 2014-12-16 changed float -> double -// 0.2.06 - 2015-03-07 all size uint8_t -// 0.2.07 - 2015-03-16 added getMin() and getMax() functions (Eric Mulder) -// 0.2.08 - 2015-04-10 refactored getMin() and getMax() implementation -// -// Released to the public domain -// - -#include "RunningAverage.h" -#include - -RunningAverage::RunningAverage(uint8_t size) -{ - _size = size; - _ar = (double*) malloc(_size * sizeof(double)); - if (_ar == NULL) _size = 0; - clear(); -} - -RunningAverage::~RunningAverage() -{ - if (_ar != NULL) free(_ar); -} - -// resets all counters -void RunningAverage::clear() -{ - _cnt = 0; - _idx = 0; - _sum = 0.0; - _min = NAN; - _max = NAN; - for (uint8_t i = 0; i < _size; i++) - { - _ar[i] = 0.0; // keeps addValue simple - } -} - -// adds a new value to the data-set -void RunningAverage::addValue(double value) -{ - if (_ar == NULL) return; // allocation error - _sum -= _ar[_idx]; - _ar[_idx] = value; - _sum += _ar[_idx]; - _idx++; - if (_idx == _size) _idx = 0; // faster than % - // handle min max - if (_cnt == 0) _min = _max = value; - else if (value < _min) _min = value; - else if (value > _max) _max = value; - // update count as last otherwise if( _cnt == 0) above will fail - if (_cnt < _size) _cnt++; -} - -// returns the average of the data-set added sofar -double RunningAverage::getAverage() -{ - if (_cnt == 0) return NAN; - return _sum / _cnt; -} - -// returns the value of an element if exist, NAN otherwise -double RunningAverage::getElement(uint8_t idx) -{ - if (idx >=_cnt ) return NAN; - return _ar[idx]; -} - -// fill the average with a value -// the param number determines how often value is added (weight) -// number should preferably be between 1 and size -void RunningAverage::fillValue(double value, uint8_t number) -{ - clear(); // TODO conditional? if (clr) clear(); - - for (uint8_t i = 0; i < number; i++) - { - addValue(value); - } -} -// END OF FILE \ No newline at end of file diff --git a/libraries/RunningAverage/RunningAverage.h b/libraries/RunningAverage/RunningAverage.h deleted file mode 100644 index c17240a..0000000 --- a/libraries/RunningAverage/RunningAverage.h +++ /dev/null @@ -1,56 +0,0 @@ -// -// FILE: RunningAverage.h -// AUTHOR: Rob dot Tillaart at gmail dot com -// VERSION: 0.2.08 -// DATE: 2015-apr-10 -// PURPOSE: RunningAverage library for Arduino -// URL: http://arduino.cc/playground/Main/RunningAverage -// HISTORY: See RunningAverage.cpp -// -// Released to the public domain -// -// backwards compatibility -// clr() clear() -// add(x) addValue(x) -// avg() getAverage() - -#ifndef RunningAverage_h -#define RunningAverage_h - -#define RUNNINGAVERAGE_LIB_VERSION "0.2.08" - -#include "Arduino.h" - -class RunningAverage -{ -public: - RunningAverage(void); - RunningAverage(uint8_t); - ~RunningAverage(); - - void clear(); - void addValue(double); - void fillValue(double, uint8_t); - - double getAverage(); - // returns lowest value added to the data-set since last clear - double getMin() { return _min; }; - // returns highest value added to the data-set since last clear - double getMax() { return _max; }; - - double getElement(uint8_t idx); - uint8_t getSize() { return _size; } - uint8_t getCount() { return _cnt; } - -protected: - uint8_t _size; - uint8_t _cnt; - uint8_t _idx; - double _sum; - double * _ar; - double _min; - double _max; -}; - -#endif -// END OF FILE \ No newline at end of file diff --git a/libraries/RunningAverage/examples/fillValue/fillValue.ino b/libraries/RunningAverage/examples/fillValue/fillValue.ino deleted file mode 100644 index aaf8900..0000000 --- a/libraries/RunningAverage/examples/fillValue/fillValue.ino +++ /dev/null @@ -1,38 +0,0 @@ -// -// FILE: fillValue.pde -// AUTHOR: Rob Tillaart -// DATE: 2012-12-30 -// -// PUPROSE: show working of fillValue -// - -#include "RunningAverage.h" - -RunningAverage myRA(10); -int samples = 0; - -void setup(void) -{ - Serial.begin(115200); - Serial.print("Demo RunningAverage lib - fillValue "); - Serial.print("Version: "); - Serial.println(RUNNINGAVERAGE_LIB_VERSION); - - myRA.fillValue(100,5); -} - -void loop(void) -{ - long rn = random(0, 100); - myRA.addValue(rn/100.0); - samples++; - Serial.print("Running Average: "); - Serial.println(myRA.getAverage(), 4); - - if (samples == 300) - { - samples = 0; - myRA.fillValue(100, 10); - } - delay(100); -} \ No newline at end of file diff --git a/libraries/RunningAverage/examples/ra_MinMaxTest/ra_MinMaxTest.ino b/libraries/RunningAverage/examples/ra_MinMaxTest/ra_MinMaxTest.ino deleted file mode 100644 index 437251a..0000000 --- a/libraries/RunningAverage/examples/ra_MinMaxTest/ra_MinMaxTest.ino +++ /dev/null @@ -1,46 +0,0 @@ -// -// FILE: runningAverageMinMaxTest.ino -// AUTHOR: Rob Tillaart -// VERSION: 0.1.00 -// DATE: 2015-apr-10 -// -// PUPROSE: demo -// - -#include "RunningAverage.h" - -RunningAverage myRA(10); -int samples = 0; - -void setup(void) -{ - Serial.begin(115200); - Serial.println("\nDemo runningAverageMinMaxTest"); - Serial.print("Version: "); - Serial.println(RUNNINGAVERAGE_LIB_VERSION); - myRA.clear(); // explicitly start clean - - Serial.println("\nCNT\tMIN\tAVG\tMAX"); -} - -void loop(void) -{ - long rn = random(0, 1000); - myRA.addValue(rn * 0.001); - samples++; - Serial.print(samples); - Serial.print("\t"); - Serial.print(myRA.getMin(), 3); - Serial.print("\t"); - Serial.print(myRA.getAverage(), 3); - Serial.print("\t"); - Serial.println(myRA.getMax(), 3); - - if (samples == 100) - { - samples = 0; - myRA.clear(); - Serial.println("\nCNT\tMIN\tAVG\tMAX"); - } - delay(10); -} diff --git a/libraries/RunningAverage/examples/ra_hour/ra_hour.ino b/libraries/RunningAverage/examples/ra_hour/ra_hour.ino deleted file mode 100644 index 09d9214..0000000 --- a/libraries/RunningAverage/examples/ra_hour/ra_hour.ino +++ /dev/null @@ -1,40 +0,0 @@ -// -// FILE: runningAverageHour.pde -// AUTHOR: Rob Tillaart -// DATE: 2012-12-30 -// -// PUPROSE: show working of runningAverage per hour -// in 2 steps - last minute + last hour -// 3 or more steps also possible -// - -#include "RunningAverage.h" - -RunningAverage raMinute(60); -RunningAverage raHour(60); - -int samples = 0; - -void setup(void) -{ - Serial.begin(115200); - Serial.println("Demo RunningAverage lib - average per minute & hour"); - Serial.print("Version: "); - Serial.println(RUNNINGAVERAGE_LIB_VERSION); - raHour.clear(); - raMinute.clear(); -} - -void loop(void) -{ - long rn = random(0, 100); - raMinute.addValue(rn); - samples++; - - if (samples % 60 == 0) raHour.addValue(raMinute.getAverage()); - - Serial.print(" raMinute: "); - Serial.print(raMinute.getAverage(), 4); - Serial.print(" raHour: "); - Serial.println(raHour.getAverage(), 4); -} \ No newline at end of file diff --git a/libraries/RunningAverage/examples/ra_test/ra_test.ino b/libraries/RunningAverage/examples/ra_test/ra_test.ino deleted file mode 100644 index 146112e..0000000 --- a/libraries/RunningAverage/examples/ra_test/ra_test.ino +++ /dev/null @@ -1,40 +0,0 @@ -// -// FILE: runningAverageTest.pde -// AUTHOR: Rob Tillaart -// VERSION: 0.1.01 -// DATE: 2012-12-30 -// -// PUPROSE: show working of runningAverage -// - -#include "RunningAverage.h" - -RunningAverage myRA(10); -int samples = 0; - -void setup(void) -{ - Serial.begin(115200); - Serial.println("Demo RunningAverage lib"); - Serial.print("Version: "); - Serial.println(RUNNINGAVERAGE_LIB_VERSION); - myRA.clear(); // explicitly start clean -} - -void loop(void) -{ - long rn = random(0, 1000); - myRA.addValue(rn * 0.001); - samples++; - Serial.print(samples); - Serial.print("\t Running Average: "); - Serial.println(myRA.getAverage(), 3); - - if (samples == 300) - { - samples = 0; - myRA.clear(); - Serial.println(); - } - delay(10); -} \ No newline at end of file diff --git a/libraries/RunningAverage/keywords.txt b/libraries/RunningAverage/keywords.txt deleted file mode 100644 index 3bac631..0000000 --- a/libraries/RunningAverage/keywords.txt +++ /dev/null @@ -1,32 +0,0 @@ -####################################### -# Syntax Coloring Map For RunningAverage -####################################### - -####################################### -# Datatypes (KEYWORD1) -####################################### - -RunningAverage KEYWORD1 - -####################################### -# Methods and Functions (KEYWORD2) -####################################### - -clear KEYWORD2 -addValue KEYWORD2 -getAverage KEYWORD2 -getMin KEYWORD2 -getMax KEYWORD2 -fillValue KEYWORD2 -getElement KEYWORD2 -getSize KEYWORD2 -getCount KEYWORD2 - -####################################### -# Instances (KEYWORD2) -####################################### - - -####################################### -# Constants (LITERAL1) -####################################### \ No newline at end of file diff --git a/libraries/SFE_BMP180/SFE_BMP180.cpp b/libraries/SFE_BMP180/SFE_BMP180.cpp deleted file mode 100644 index 4383fc5..0000000 --- a/libraries/SFE_BMP180/SFE_BMP180.cpp +++ /dev/null @@ -1,387 +0,0 @@ -/* - SFE_BMP180.cpp - Bosch BMP180 pressure sensor library for the Arduino microcontroller - Mike Grusin, SparkFun Electronics - - Uses floating-point equations from the Weather Station Data Logger project - http://wmrx00.sourceforge.net/ - http://wmrx00.sourceforge.net/Arduino/BMP085-Calcs.pdf - - Forked from BMP085 library by M.Grusin - - version 1.0 2013/09/20 initial version - - Our example code uses the "beerware" license. You can do anything - you like with this code. No really, anything. If you find it useful, - buy me a (root) beer someday. -*/ - -#include -#include -#include -#include - - -SFE_BMP180::SFE_BMP180() -// Base library type -{ -} - - -char SFE_BMP180::begin() -// Initialize library for subsequent pressure measurements -{ - double c3,c4,b1; - - // Start up the Arduino's "wire" (I2C) library: - - Wire.begin(); - - // The BMP180 includes factory calibration data stored on the device. - // Each device has different numbers, these must be retrieved and - // used in the calculations when taking pressure measurements. - - // Retrieve calibration data from device: - - if (readInt(0xAA,AC1) && - readInt(0xAC,AC2) && - readInt(0xAE,AC3) && - readUInt(0xB0,AC4) && - readUInt(0xB2,AC5) && - readUInt(0xB4,AC6) && - readInt(0xB6,VB1) && - readInt(0xB8,VB2) && - readInt(0xBA,MB) && - readInt(0xBC,MC) && - readInt(0xBE,MD)) - { - - // All reads completed successfully! - - // If you need to check your math using known numbers, - // you can uncomment one of these examples. - // (The correct results are commented in the below functions.) - - // Example from Bosch datasheet - // AC1 = 408; AC2 = -72; AC3 = -14383; AC4 = 32741; AC5 = 32757; AC6 = 23153; - // B1 = 6190; B2 = 4; MB = -32768; MC = -8711; MD = 2868; - - // Example from http://wmrx00.sourceforge.net/Arduino/BMP180-Calcs.pdf - // AC1 = 7911; AC2 = -934; AC3 = -14306; AC4 = 31567; AC5 = 25671; AC6 = 18974; - // VB1 = 5498; VB2 = 46; MB = -32768; MC = -11075; MD = 2432; - - /* - Serial.print("AC1: "); Serial.println(AC1); - Serial.print("AC2: "); Serial.println(AC2); - Serial.print("AC3: "); Serial.println(AC3); - Serial.print("AC4: "); Serial.println(AC4); - Serial.print("AC5: "); Serial.println(AC5); - Serial.print("AC6: "); Serial.println(AC6); - Serial.print("VB1: "); Serial.println(VB1); - Serial.print("VB2: "); Serial.println(VB2); - Serial.print("MB: "); Serial.println(MB); - Serial.print("MC: "); Serial.println(MC); - Serial.print("MD: "); Serial.println(MD); - */ - - // Compute floating-point polynominals: - - c3 = 160.0 * pow(2,-15) * AC3; - c4 = pow(10,-3) * pow(2,-15) * AC4; - b1 = pow(160,2) * pow(2,-30) * VB1; - c5 = (pow(2,-15) / 160) * AC5; - c6 = AC6; - mc = (pow(2,11) / pow(160,2)) * MC; - md = MD / 160.0; - x0 = AC1; - x1 = 160.0 * pow(2,-13) * AC2; - x2 = pow(160,2) * pow(2,-25) * VB2; - y0 = c4 * pow(2,15); - y1 = c4 * c3; - y2 = c4 * b1; - p0 = (3791.0 - 8.0) / 1600.0; - p1 = 1.0 - 7357.0 * pow(2,-20); - p2 = 3038.0 * 100.0 * pow(2,-36); - - /* - Serial.println(); - Serial.print("c3: "); Serial.println(c3); - Serial.print("c4: "); Serial.println(c4); - Serial.print("c5: "); Serial.println(c5); - Serial.print("c6: "); Serial.println(c6); - Serial.print("b1: "); Serial.println(b1); - Serial.print("mc: "); Serial.println(mc); - Serial.print("md: "); Serial.println(md); - Serial.print("x0: "); Serial.println(x0); - Serial.print("x1: "); Serial.println(x1); - Serial.print("x2: "); Serial.println(x2); - Serial.print("y0: "); Serial.println(y0); - Serial.print("y1: "); Serial.println(y1); - Serial.print("y2: "); Serial.println(y2); - Serial.print("p0: "); Serial.println(p0); - Serial.print("p1: "); Serial.println(p1); - Serial.print("p2: "); Serial.println(p2); - */ - - // Success! - return(1); - } - else - { - // Error reading calibration data; bad component or connection? - return(0); - } -} - - -char SFE_BMP180::readInt(char address, int &value) -// Read a signed integer (two bytes) from device -// address: register to start reading (plus subsequent register) -// value: external variable to store data (function modifies value) -{ - unsigned char data[2]; - - data[0] = address; - if (readBytes(data,2)) - { - value = (((int)data[0]<<8)|(int)data[1]); - //if (*value & 0x8000) *value |= 0xFFFF0000; // sign extend if negative - return(1); - } - value = 0; - return(0); -} - - -char SFE_BMP180::readUInt(char address, unsigned int &value) -// Read an unsigned integer (two bytes) from device -// address: register to start reading (plus subsequent register) -// value: external variable to store data (function modifies value) -{ - unsigned char data[2]; - - data[0] = address; - if (readBytes(data,2)) - { - value = (((unsigned int)data[0]<<8)|(unsigned int)data[1]); - return(1); - } - value = 0; - return(0); -} - - -char SFE_BMP180::readBytes(unsigned char *values, char length) -// Read an array of bytes from device -// values: external array to hold data. Put starting register in values[0]. -// length: number of bytes to read -{ - char x; - - Wire.beginTransmission(BMP180_ADDR); - Wire.write(values[0]); - _error = Wire.endTransmission(); - if (_error == 0) - { - Wire.requestFrom(BMP180_ADDR,length); - while(Wire.available() != length) ; // wait until bytes are ready - for(x=0;x= 100 -#include "Arduino.h" -#else -#include "WProgram.h" -#endif - -class SFE_BMP180 -{ - public: - SFE_BMP180(); // base type - - char begin(); - // call pressure.begin() to initialize BMP180 before use - // returns 1 if success, 0 if failure (bad component or I2C bus shorted?) - - char startTemperature(void); - // command BMP180 to start a temperature measurement - // returns (number of ms to wait) for success, 0 for fail - - char getTemperature(double &T); - // return temperature measurement from previous startTemperature command - // places returned value in T variable (deg C) - // returns 1 for success, 0 for fail - - char startPressure(char oversampling); - // command BMP180 to start a pressure measurement - // oversampling: 0 - 3 for oversampling value - // returns (number of ms to wait) for success, 0 for fail - - char getPressure(double &P, double &T); - // return absolute pressure measurement from previous startPressure command - // note: requires previous temperature measurement in variable T - // places returned value in P variable (mbar) - // returns 1 for success, 0 for fail - - double sealevel(double P, double A); - // convert absolute pressure to sea-level pressure (as used in weather data) - // P: absolute pressure (mbar) - // A: current altitude (meters) - // returns sealevel pressure in mbar - - double altitude(double P, double P0); - // convert absolute pressure to altitude (given baseline pressure; sea-level, runway, etc.) - // P: absolute pressure (mbar) - // P0: fixed baseline pressure (mbar) - // returns signed altitude in meters - - char getError(void); - // If any library command fails, you can retrieve an extended - // error code using this command. Errors are from the wire library: - // 0 = Success - // 1 = Data too long to fit in transmit buffer - // 2 = Received NACK on transmit of address - // 3 = Received NACK on transmit of data - // 4 = Other error - - private: - - char readInt(char address, int &value); - // read an signed int (16 bits) from a BMP180 register - // address: BMP180 register address - // value: external signed int for returned value (16 bits) - // returns 1 for success, 0 for fail, with result in value - - char readUInt(char address, unsigned int &value); - // read an unsigned int (16 bits) from a BMP180 register - // address: BMP180 register address - // value: external unsigned int for returned value (16 bits) - // returns 1 for success, 0 for fail, with result in value - - char readBytes(unsigned char *values, char length); - // read a number of bytes from a BMP180 register - // values: array of char with register address in first location [0] - // length: number of bytes to read back - // returns 1 for success, 0 for fail, with read bytes in values[] array - - char writeBytes(unsigned char *values, char length); - // write a number of bytes to a BMP180 register (and consecutive subsequent registers) - // values: array of char with register address in first location [0] - // length: number of bytes to write - // returns 1 for success, 0 for fail - - int AC1,AC2,AC3,VB1,VB2,MB,MC,MD; - unsigned int AC4,AC5,AC6; - double c5,c6,mc,md,x0,x1,x2,y0,y1,y2,p0,p1,p2; - char _error; -}; - -#define BMP180_ADDR 0x77 // 7-bit address - -#define BMP180_REG_CONTROL 0xF4 -#define BMP180_REG_RESULT 0xF6 - -#define BMP180_COMMAND_TEMPERATURE 0x2E -#define BMP180_COMMAND_PRESSURE0 0x34 -#define BMP180_COMMAND_PRESSURE1 0x74 -#define BMP180_COMMAND_PRESSURE2 0xB4 -#define BMP180_COMMAND_PRESSURE3 0xF4 - -#endif diff --git a/libraries/SFE_BMP180/examples/BMP180_altitude_example/BMP180_altitude_example.ino b/libraries/SFE_BMP180/examples/BMP180_altitude_example/BMP180_altitude_example.ino deleted file mode 100644 index fdb8358..0000000 --- a/libraries/SFE_BMP180/examples/BMP180_altitude_example/BMP180_altitude_example.ino +++ /dev/null @@ -1,168 +0,0 @@ -/* SFE_BMP180 altitude example sketch - -This sketch shows how to use the Bosch BMP180 pressure sensor -as an altimiter. -https://www.sparkfun.com/products/11824 - -Like most pressure sensors, the BMP180 measures absolute pressure. -Since absolute pressure varies with altitude, you can use the pressure -to determine your altitude. - -Because pressure also varies with weather, you must first take a pressure -reading at a known baseline altitude. Then you can measure variations -from that pressure - -Hardware connections: - -- (GND) to GND -+ (VDD) to 3.3V - -(WARNING: do not connect + to 5V or the sensor will be damaged!) - -You will also need to connect the I2C pins (SCL and SDA) to your -Arduino. The pins are different on different Arduinos: - -Any Arduino pins labeled: SDA SCL -Uno, Redboard, Pro: A4 A5 -Mega2560, Due: 20 21 -Leonardo: 2 3 - -Leave the IO (VDDIO) pin unconnected. This pin is for connecting -the BMP180 to systems with lower logic levels such as 1.8V - -Have fun! -Your friends at SparkFun. - -The SFE_BMP180 library uses floating-point equations developed by the -Weather Station Data Logger project: http://wmrx00.sourceforge.net/ - -Our example code uses the "beerware" license. You can do anything -you like with this code. No really, anything. If you find it useful, -buy me a beer someday. - -V10 Mike Grusin, SparkFun Electronics 10/24/2013 -*/ - -// Your sketch must #include this library, and the Wire library. -// (Wire is a standard library included with Arduino.): - -#include -#include - -// You will need to create an SFE_BMP180 object, here called "pressure": - -SFE_BMP180 pressure; - -double baseline; // baseline pressure - -void setup() -{ - Serial.begin(9600); - Serial.println("REBOOT"); - - // Initialize the sensor (it is important to get calibration values stored on the device). - - if (pressure.begin()) - Serial.println("BMP180 init success"); - else - { - // Oops, something went wrong, this is usually a connection problem, - // see the comments at the top of this sketch for the proper connections. - - Serial.println("BMP180 init fail (disconnected?)\n\n"); - while(1); // Pause forever. - } - - // Get the baseline pressure: - - baseline = getPressure(); - - Serial.print("baseline pressure: "); - Serial.print(baseline); - Serial.println(" mb"); -} - -void loop() -{ - double a,P; - - // Get a new pressure reading: - - P = getPressure(); - - // Show the relative altitude difference between - // the new reading and the baseline reading: - - a = pressure.altitude(P,baseline); - - Serial.print("relative altitude: "); - if (a >= 0.0) Serial.print(" "); // add a space for positive numbers - Serial.print(a,1); - Serial.print(" meters, "); - if (a >= 0.0) Serial.print(" "); // add a space for positive numbers - Serial.print(a*3.28084,0); - Serial.println(" feet"); - - delay(500); -} - - -double getPressure() -{ - char status; - double T,P,p0,a; - - // You must first get a temperature measurement to perform a pressure reading. - - // Start a temperature measurement: - // If request is successful, the number of ms to wait is returned. - // If request is unsuccessful, 0 is returned. - - status = pressure.startTemperature(); - if (status != 0) - { - // Wait for the measurement to complete: - - delay(status); - - // Retrieve the completed temperature measurement: - // Note that the measurement is stored in the variable T. - // Use '&T' to provide the address of T to the function. - // Function returns 1 if successful, 0 if failure. - - status = pressure.getTemperature(T); - if (status != 0) - { - // Start a pressure measurement: - // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait). - // If request is successful, the number of ms to wait is returned. - // If request is unsuccessful, 0 is returned. - - status = pressure.startPressure(3); - if (status != 0) - { - // Wait for the measurement to complete: - delay(status); - - // Retrieve the completed pressure measurement: - // Note that the measurement is stored in the variable P. - // Use '&P' to provide the address of P. - // Note also that the function requires the previous temperature measurement (T). - // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.) - // Function returns 1 if successful, 0 if failure. - - status = pressure.getPressure(P,T); - if (status != 0) - { - return(P); - } - else Serial.println("error retrieving pressure measurement\n"); - } - else Serial.println("error starting pressure measurement\n"); - } - else Serial.println("error retrieving temperature measurement\n"); - } - else Serial.println("error starting temperature measurement\n"); -} - - - diff --git a/libraries/SFE_BMP180/examples/SFE_BMP180_example/SFE_BMP180_example.ino b/libraries/SFE_BMP180/examples/SFE_BMP180_example/SFE_BMP180_example.ino deleted file mode 100644 index a43c905..0000000 --- a/libraries/SFE_BMP180/examples/SFE_BMP180_example/SFE_BMP180_example.ino +++ /dev/null @@ -1,200 +0,0 @@ -/* SFE_BMP180 library example sketch - -This sketch shows how to use the SFE_BMP180 library to read the -Bosch BMP180 barometric pressure sensor. -https://www.sparkfun.com/products/11824 - -Like most pressure sensors, the BMP180 measures absolute pressure. -This is the actual ambient pressure seen by the device, which will -vary with both altitude and weather. - -Before taking a pressure reading you must take a temparture reading. -This is done with startTemperature() and getTemperature(). -The result is in degrees C. - -Once you have a temperature reading, you can take a pressure reading. -This is done with startPressure() and getPressure(). -The result is in millibar (mb) aka hectopascals (hPa). - -If you'll be monitoring weather patterns, you will probably want to -remove the effects of altitude. This will produce readings that can -be compared to the published pressure readings from other locations. -To do this, use the sealevel() function. You will need to provide -the known altitude at which the pressure was measured. - -If you want to measure altitude, you will need to know the pressure -at a baseline altitude. This can be average sealevel pressure, or -a previous pressure reading at your altitude, in which case -subsequent altitude readings will be + or - the initial baseline. -This is done with the altitude() function. - -Hardware connections: - -- (GND) to GND -+ (VDD) to 3.3V - -(WARNING: do not connect + to 5V or the sensor will be damaged!) - -You will also need to connect the I2C pins (SCL and SDA) to your -Arduino. The pins are different on different Arduinos: - -Any Arduino pins labeled: SDA SCL -Uno, Redboard, Pro: A4 A5 -Mega2560, Due: 20 21 -Leonardo: 2 3 - -Leave the IO (VDDIO) pin unconnected. This pin is for connecting -the BMP180 to systems with lower logic levels such as 1.8V - -Have fun! -Your friends at SparkFun. - -The SFE_BMP180 library uses floating-point equations developed by the -Weather Station Data Logger project: http://wmrx00.sourceforge.net/ - -Our example code uses the "beerware" license. You can do anything -you like with this code. No really, anything. If you find it useful, -buy me a beer someday. - -V10 Mike Grusin, SparkFun Electronics 10/24/2013 -*/ - -// Your sketch must #include this library, and the Wire library. -// (Wire is a standard library included with Arduino.): - -#include -#include - -// You will need to create an SFE_BMP180 object, here called "pressure": - -SFE_BMP180 pressure; - -#define ALTITUDE 1655.0 // Altitude of SparkFun's HQ in Boulder, CO. in meters - -void setup() -{ - Serial.begin(9600); - Serial.println("REBOOT"); - - // Initialize the sensor (it is important to get calibration values stored on the device). - - if (pressure.begin()) - Serial.println("BMP180 init success"); - else - { - // Oops, something went wrong, this is usually a connection problem, - // see the comments at the top of this sketch for the proper connections. - - Serial.println("BMP180 init fail\n\n"); - while(1); // Pause forever. - } -} - -void loop() -{ - char status; - double T,P,p0,a; - - // Loop here getting pressure readings every 10 seconds. - - // If you want sea-level-compensated pressure, as used in weather reports, - // you will need to know the altitude at which your measurements are taken. - // We're using a constant called ALTITUDE in this sketch: - - Serial.println(); - Serial.print("provided altitude: "); - Serial.print(ALTITUDE,0); - Serial.print(" meters, "); - Serial.print(ALTITUDE*3.28084,0); - Serial.println(" feet"); - - // If you want to measure altitude, and not pressure, you will instead need - // to provide a known baseline pressure. This is shown at the end of the sketch. - - // You must first get a temperature measurement to perform a pressure reading. - - // Start a temperature measurement: - // If request is successful, the number of ms to wait is returned. - // If request is unsuccessful, 0 is returned. - - status = pressure.startTemperature(); - if (status != 0) - { - // Wait for the measurement to complete: - delay(status); - - // Retrieve the completed temperature measurement: - // Note that the measurement is stored in the variable T. - // Function returns 1 if successful, 0 if failure. - - status = pressure.getTemperature(T); - if (status != 0) - { - // Print out the measurement: - Serial.print("temperature: "); - Serial.print(T,2); - Serial.print(" deg C, "); - Serial.print((9.0/5.0)*T+32.0,2); - Serial.println(" deg F"); - - // Start a pressure measurement: - // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait). - // If request is successful, the number of ms to wait is returned. - // If request is unsuccessful, 0 is returned. - - status = pressure.startPressure(3); - if (status != 0) - { - // Wait for the measurement to complete: - delay(status); - - // Retrieve the completed pressure measurement: - // Note that the measurement is stored in the variable P. - // Note also that the function requires the previous temperature measurement (T). - // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.) - // Function returns 1 if successful, 0 if failure. - - status = pressure.getPressure(P,T); - if (status != 0) - { - // Print out the measurement: - Serial.print("absolute pressure: "); - Serial.print(P,2); - Serial.print(" mb, "); - Serial.print(P*0.0295333727,2); - Serial.println(" inHg"); - - // The pressure sensor returns abolute pressure, which varies with altitude. - // To remove the effects of altitude, use the sealevel function and your current altitude. - // This number is commonly used in weather reports. - // Parameters: P = absolute pressure in mb, ALTITUDE = current altitude in m. - // Result: p0 = sea-level compensated pressure in mb - - p0 = pressure.sealevel(P,ALTITUDE); // we're at 1655 meters (Boulder, CO) - Serial.print("relative (sea-level) pressure: "); - Serial.print(p0,2); - Serial.print(" mb, "); - Serial.print(p0*0.0295333727,2); - Serial.println(" inHg"); - - // On the other hand, if you want to determine your altitude from the pressure reading, - // use the altitude function along with a baseline pressure (sea-level or other). - // Parameters: P = absolute pressure in mb, p0 = baseline pressure in mb. - // Result: a = altitude in m. - - a = pressure.altitude(P,p0); - Serial.print("computed altitude: "); - Serial.print(a,0); - Serial.print(" meters, "); - Serial.print(a*3.28084,0); - Serial.println(" feet"); - } - else Serial.println("error retrieving pressure measurement\n"); - } - else Serial.println("error starting pressure measurement\n"); - } - else Serial.println("error retrieving temperature measurement\n"); - } - else Serial.println("error starting temperature measurement\n"); - - delay(5000); // Pause for 5 seconds. -} diff --git a/libraries/SFE_BMP180/keywords.txt b/libraries/SFE_BMP180/keywords.txt deleted file mode 100644 index 664167d..0000000 --- a/libraries/SFE_BMP180/keywords.txt +++ /dev/null @@ -1,27 +0,0 @@ -####################################### -# Syntax Coloring Map for SFE_BMP180 -####################################### - -####################################### -# Datatypes (KEYWORD1) -####################################### - -SFE_BMP180 KEYWORD1 - -####################################### -# Methods and Functions (KEYWORD2) -####################################### - -begin KEYWORD2 -startTemperature KEYWORD2 -getTemperature KEYWORD2 -startPressure KEYWORD2 -getPressure KEYWORD2 -sealevel KEYWORD2 -altitude KEYWORD2 - -####################################### -# Constants (LITERAL1) -####################################### - -BMP180_ADDR LITERAL1 \ No newline at end of file diff --git a/libraries/SI7021/SI7021.cpp b/libraries/SI7021/SI7021.cpp index c51ab6b..4f929d1 100644 --- a/libraries/SI7021/SI7021.cpp +++ b/libraries/SI7021/SI7021.cpp @@ -38,7 +38,7 @@ bool SI7021::sensorExists() { } int SI7021::getFahrenheitHundredths() { - unsigned int c = getCelsiusHundredths(); + int c = getCelsiusHundredths(); return (1.8 * c) + 3200; } diff --git a/libraries/SPIFlash/README.md b/libraries/SPIFlash/README.md deleted file mode 100644 index 63a1cac..0000000 --- a/libraries/SPIFlash/README.md +++ /dev/null @@ -1,24 +0,0 @@ -SPIFlash -======== -Arduino/Moteino library for read/write access to SPI flash memory chips. -This works with 256byte/page SPI flash memory such as the 4MBIT W25X40CLSNIG used on (Moteinos)[www.moteino.com] for data storage and wireless programming. -
-For instance a 4MBit (512Kbyte) flash chip will have 2048 pages: 256*2048 = 524288 bytes (512Kbytes). -
Minimal modifications should allow chips that have different page size to work. -
DEPENDS ON: Arduino *SPI library*. -
-This library was primarily developed to enable **safe** wireless programming on Moteino nodes and Moteino based applications such as the SwitchMote. This has been documented at [lowpowerlab](http://lowpowerlab.com/blog/category/moteino/wireless-programming/). [Dualoptiboot](https://github.com/LowPowerLab/DualOptiboot) (all Moteinos come with it) and [WirelessProgramming library](https://github.com/LowPowerLab/WirelessProgramming) are required to be able to wirelessly re-flash a remote Moteino. - -###Installation -Copy the content of this library in the "Arduino/libraries/SPIFlash" folder. -
-To find your Arduino folder go to File>Preferences in the Arduino IDE. -
-See [this tutorial](http://learn.adafruit.com/arduino-tips-tricks-and-techniques/arduino-libraries) on Arduino libraries. - -###License -Copyright (c) 2013 by Felix Rusu -
-This library is free software; you can redistribute it and/or modify it under the terms of either the GNU General Public License version 2 or the GNU Lesser General Public License version 2.1, both as published by the Free Software Foundation. -
-This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. diff --git a/libraries/SPIFlash/SPIFlash.cpp b/libraries/SPIFlash/SPIFlash.cpp deleted file mode 100644 index bad4cdf..0000000 --- a/libraries/SPIFlash/SPIFlash.cpp +++ /dev/null @@ -1,274 +0,0 @@ -/* - * Copyright (c) 2013 by Felix Rusu - * SPI Flash memory library for arduino/moteino. - * This works with 256byte/page SPI flash memory - * For instance a 4MBit (512Kbyte) flash chip will have 2048 pages: 256*2048 = 524288 bytes (512Kbytes) - * Minimal modifications should allow chips that have different page size but modifications - * DEPENDS ON: Arduino SPI library - * - * Updated Jan. 5, 2015, TomWS1, modified writeBytes to allow blocks > 256 bytes and handle page misalignment. - * Updated Feb. 26, 2015 TomWS1, added support for SPI Transactions (Arduino 1.5.8 and above) - * - * This file is free software; you can redistribute it and/or modify - * it under the terms of either the GNU General Public License version 2 - * or the GNU Lesser General Public License version 2.1, both as - * published by the Free Software Foundation. - */ - -#include - -byte SPIFlash::UNIQUEID[8]; - -/// IMPORTANT: NAND FLASH memory requires erase before write, because -/// it can only transition from 1s to 0s and only the erase command can reset all 0s to 1s -/// See http://en.wikipedia.org/wiki/Flash_memory -/// The smallest range that can be erased is a sector (4K, 32K, 64K); there is also a chip erase command - -/// Constructor. JedecID is optional but recommended, since this will ensure that the device is present and has a valid response -/// get this from the datasheet of your flash chip -/// Example for Atmel-Adesto 4Mbit AT25DF041A: 0x1F44 (page 27: http://www.adestotech.com/sites/default/files/datasheets/doc3668.pdf) -/// Example for Winbond 4Mbit W25X40CL: 0xEF30 (page 14: http://www.winbond.com/NR/rdonlyres/6E25084C-0BFE-4B25-903D-AE10221A0929/0/W25X40CL.pdf) -SPIFlash::SPIFlash(uint8_t slaveSelectPin, uint16_t jedecID) { - _slaveSelectPin = slaveSelectPin; - _jedecID = jedecID; -} - -/// Select the flash chip -void SPIFlash::select() { - //save current SPI settings -#ifndef SPI_HAS_TRANSACTION - _SREG = SREG; - noInterrupts(); -#endif - _SPCR = SPCR; - _SPSR = SPSR; - -#ifdef SPI_HAS_TRANSACTION - SPI.beginTransaction(_settings); -#else - // set FLASH SPI settings - SPI.setDataMode(SPI_MODE0); - SPI.setBitOrder(MSBFIRST); - SPI.setClockDivider(SPI_CLOCK_DIV4); // decided to slow down from DIV2 after SPI stalling in some instances, especially visible on mega1284p when RFM69 and FLASH chip both present -#endif - digitalWrite(_slaveSelectPin, LOW); -} - -/// UNselect the flash chip -void SPIFlash::unselect() { - digitalWrite(_slaveSelectPin, HIGH); - //restore SPI settings to what they were before talking to the FLASH chip -#ifdef SPI_HAS_TRANSACTION - SPI.endTransaction(); -#else - SREG = _SREG; // restore interrupts IFF they were enabled -#endif - SPCR = _SPCR; - SPSR = _SPSR; -} - -/// setup SPI, read device ID etc... -boolean SPIFlash::initialize() -{ - _SPCR = SPCR; - _SPSR = SPSR; - digitalWrite(_slaveSelectPin, HIGH); - pinMode(_slaveSelectPin, OUTPUT); - SPI.begin(); - _settings = SPISettings(4000000, MSBFIRST, SPI_MODE0); - unselect(); - wakeup(); - - if (_jedecID == 0 || readDeviceId() == _jedecID) { - command(SPIFLASH_STATUSWRITE, true); // Write Status Register - SPI.transfer(0); // Global Unprotect - unselect(); - return true; - } - return false; -} - -/// Get the manufacturer and device ID bytes (as a short word) -word SPIFlash::readDeviceId() -{ -#if defined(__AVR_ATmega32U4__) // Arduino Leonardo, MoteinoLeo - command(SPIFLASH_IDREAD); // Read JEDEC ID -#else - select(); - SPI.transfer(SPIFLASH_IDREAD); -#endif - word jedecid = SPI.transfer(0) << 8; - jedecid |= SPI.transfer(0); - unselect(); - return jedecid; -} - -/// Get the 64 bit unique identifier, stores it in UNIQUEID[8]. Only needs to be called once, ie after initialize -/// Returns the byte pointer to the UNIQUEID byte array -/// Read UNIQUEID like this: -/// flash.readUniqueId(); for (byte i=0;i<8;i++) { Serial.print(flash.UNIQUEID[i], HEX); Serial.print(' '); } -/// or like this: -/// flash.readUniqueId(); byte* MAC = flash.readUniqueId(); for (byte i=0;i<8;i++) { Serial.print(MAC[i], HEX); Serial.print(' '); } -byte* SPIFlash::readUniqueId() -{ - command(SPIFLASH_MACREAD); - SPI.transfer(0); - SPI.transfer(0); - SPI.transfer(0); - SPI.transfer(0); - for (byte i=0;i<8;i++) - UNIQUEID[i] = SPI.transfer(0); - unselect(); - return UNIQUEID; -} - -/// read 1 byte from flash memory -byte SPIFlash::readByte(long addr) { - command(SPIFLASH_ARRAYREADLOWFREQ); - SPI.transfer(addr >> 16); - SPI.transfer(addr >> 8); - SPI.transfer(addr); - byte result = SPI.transfer(0); - unselect(); - return result; -} - -/// read unlimited # of bytes -void SPIFlash::readBytes(long addr, void* buf, word len) { - command(SPIFLASH_ARRAYREAD); - SPI.transfer(addr >> 16); - SPI.transfer(addr >> 8); - SPI.transfer(addr); - SPI.transfer(0); //"dont care" - for (word i = 0; i < len; ++i) - ((byte*) buf)[i] = SPI.transfer(0); - unselect(); -} - -/// Send a command to the flash chip, pass TRUE for isWrite when its a write command -void SPIFlash::command(byte cmd, boolean isWrite){ -#if defined(__AVR_ATmega32U4__) // Arduino Leonardo, MoteinoLeo - DDRB |= B00000001; // Make sure the SS pin (PB0 - used by RFM12B on MoteinoLeo R1) is set as output HIGH! - PORTB |= B00000001; -#endif - if (isWrite) - { - command(SPIFLASH_WRITEENABLE); // Write Enable - unselect(); - } - //wait for any write/erase to complete - // a time limit cannot really be added here without it being a very large safe limit - // that is because some chips can take several seconds to carry out a chip erase or other similar multi block or entire-chip operations - // a recommended alternative to such situations where chip can be or not be present is to add a 10k or similar weak pulldown on the - // open drain MISO input which can read noise/static and hence return a non 0 status byte, causing the while() to hang when a flash chip is not present - while(busy()); - select(); - SPI.transfer(cmd); -} - -/// check if the chip is busy erasing/writing -boolean SPIFlash::busy() -{ - /* - select(); - SPI.transfer(SPIFLASH_STATUSREAD); - byte status = SPI.transfer(0); - unselect(); - return status & 1; - */ - return readStatus() & 1; -} - -/// return the STATUS register -byte SPIFlash::readStatus() -{ - select(); - SPI.transfer(SPIFLASH_STATUSREAD); - byte status = SPI.transfer(0); - unselect(); - return status; -} - - -/// Write 1 byte to flash memory -/// WARNING: you can only write to previously erased memory locations (see datasheet) -/// use the block erase commands to first clear memory (write 0xFFs) -void SPIFlash::writeByte(long addr, uint8_t byt) { - command(SPIFLASH_BYTEPAGEPROGRAM, true); // Byte/Page Program - SPI.transfer(addr >> 16); - SPI.transfer(addr >> 8); - SPI.transfer(addr); - SPI.transfer(byt); - unselect(); -} - -/// write multiple bytes to flash memory (up to 64K) -/// WARNING: you can only write to previously erased memory locations (see datasheet) -/// use the block erase commands to first clear memory (write 0xFFs) -/// This version handles both page alignment and data blocks larger than 256 bytes. -/// -void SPIFlash::writeBytes(long addr, const void* buf, uint16_t len) { - uint16_t n; - uint16_t maxBytes = 256-(addr%256); // force the first set of bytes to stay within the first page - uint16_t offset = 0; - while (len>0) - { - n = (len<=maxBytes) ? len : maxBytes; - command(SPIFLASH_BYTEPAGEPROGRAM, true); // Byte/Page Program - SPI.transfer(addr >> 16); - SPI.transfer(addr >> 8); - SPI.transfer(addr); - for (uint16_t i = 0; i < n; i++) - SPI.transfer(((byte*) buf)[offset + i]); - unselect(); - addr+=n; // adjust the addresses and remaining bytes by what we've just transferred. - offset +=n; - len -= n; - maxBytes = 256; // now we can do up to 256 bytes per loop - } -} - - -/// erase entire flash memory array -/// may take several seconds depending on size, but is non blocking -/// so you may wait for this to complete using busy() or continue doing -/// other things and later check if the chip is done with busy() -/// note that any command will first wait for chip to become available using busy() -/// so no need to do that twice -void SPIFlash::chipErase() { - command(SPIFLASH_CHIPERASE, true); - unselect(); -} - -/// erase a 4Kbyte block -void SPIFlash::blockErase4K(long addr) { - command(SPIFLASH_BLOCKERASE_4K, true); // Block Erase - SPI.transfer(addr >> 16); - SPI.transfer(addr >> 8); - SPI.transfer(addr); - unselect(); -} - -/// erase a 32Kbyte block -void SPIFlash::blockErase32K(long addr) { - command(SPIFLASH_BLOCKERASE_32K, true); // Block Erase - SPI.transfer(addr >> 16); - SPI.transfer(addr >> 8); - SPI.transfer(addr); - unselect(); -} - -void SPIFlash::sleep() { - command(SPIFLASH_SLEEP); - unselect(); -} - -void SPIFlash::wakeup() { - command(SPIFLASH_WAKE); - unselect(); -} - -/// cleanup -void SPIFlash::end() { - SPI.end(); -} \ No newline at end of file diff --git a/libraries/SPIFlash/SPIFlash.h b/libraries/SPIFlash/SPIFlash.h deleted file mode 100644 index 8f530b7..0000000 --- a/libraries/SPIFlash/SPIFlash.h +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (c) 2013 by Felix Rusu - * SPI Flash memory library for arduino/moteino. - * This works with 256uint8_t/page SPI flash memory - * For instance a 4MBit (512Kuint8_t) flash chip will have 2048 pages: 256*2048 = 524288 uint8_ts (512Kuint8_ts) - * Minimal modifications should allow chips that have different page size but modifications - * DEPENDS ON: Arduino SPI library - * - * Updated Feb. 26, 2015 TomWS1, added support for SPI Transactions (Arduino 1.5.8 and above) - * - * This file is free software; you can redistribute it and/or modify - * it under the terms of either the GNU General Public License version 2 - * or the GNU Lesser General Public License version 2.1, both as - * published by the Free Software Foundation. - */ - -#ifndef _SPIFLASH_H_ -#define _SPIFLASH_H_ - -#if ARDUINO >= 100 -#include -#else -#include -#include "pins_arduino.h" -#endif - -#include - -/// IMPORTANT: NAND FLASH memory requires erase before write, because -/// it can only transition from 1s to 0s and only the erase command can reset all 0s to 1s -/// See http://en.wikipedia.org/wiki/Flash_memory -/// The smallest range that can be erased is a sector (4K, 32K, 64K); there is also a chip erase command - -/// Standard SPI flash commands -/// Assuming the WP pin is pulled up (to disable hardware write protection) -/// To use any write commands the WEL bit in the status register must be set to 1. -/// This is accomplished by sending a 0x06 command before any such write/erase command. -/// The WEL bit in the status register resets to the logical �0� state after a -/// device power-up or reset. In addition, the WEL bit will be reset to the logical �0� state automatically under the following conditions: -/// � Write Disable operation completes successfully -/// � Write Status Register operation completes successfully or aborts -/// � Protect Sector operation completes successfully or aborts -/// � Unprotect Sector operation completes successfully or aborts -/// � Byte/Page Program operation completes successfully or aborts -/// � Sequential Program Mode reaches highest unprotected memory location -/// � Sequential Program Mode reaches the end of the memory array -/// � Sequential Program Mode aborts -/// � Block Erase operation completes successfully or aborts -/// � Chip Erase operation completes successfully or aborts -/// � Hold condition aborts -#define SPIFLASH_WRITEENABLE 0x06 // write enable -#define SPIFLASH_WRITEDISABLE 0x04 // write disable - -#define SPIFLASH_BLOCKERASE_4K 0x20 // erase one 4K block of flash memory -#define SPIFLASH_BLOCKERASE_32K 0x52 // erase one 32K block of flash memory -#define SPIFLASH_BLOCKERASE_64K 0xD8 // erase one 64K block of flash memory -#define SPIFLASH_CHIPERASE 0x60 // chip erase (may take several seconds depending on size) - // but no actual need to wait for completion (instead need to check the status register BUSY bit) -#define SPIFLASH_STATUSREAD 0x05 // read status register -#define SPIFLASH_STATUSWRITE 0x01 // write status register -#define SPIFLASH_ARRAYREAD 0x0B // read array (fast, need to add 1 dummy uint8_t after 3 address uint8_ts) -#define SPIFLASH_ARRAYREADLOWFREQ 0x03 // read array (low frequency) - -#define SPIFLASH_SLEEP 0xB9 // deep power down -#define SPIFLASH_WAKE 0xAB // deep power wake up -#define SPIFLASH_BYTEPAGEPROGRAM 0x02 // write (1 to 256uint8_ts) -#define SPIFLASH_IDREAD 0x9F // read JEDEC manufacturer and device ID (2 uint8_ts, specific uint8_ts for each manufacturer and device) - // Example for Atmel-Adesto 4Mbit AT25DF041A: 0x1F44 (page 27: http://www.adestotech.com/sites/default/files/datasheets/doc3668.pdf) - // Example for Winbond 4Mbit W25X40CL: 0xEF30 (page 14: http://www.winbond.com/NR/rdonlyres/6E25084C-0BFE-4B25-903D-AE10221A0929/0/W25X40CL.pdf) -#define SPIFLASH_MACREAD 0x4B // read unique ID number (MAC) - -class SPIFlash { -public: - static uint8_t UNIQUEID[8]; - SPIFlash(uint8_t slaveSelectPin, uint16_t jedecID=0); - boolean initialize(); - void command(uint8_t cmd, boolean isWrite=false); - uint8_t readStatus(); - uint8_t readByte(long addr); - void readBytes(long addr, void* buf, word len); - void writeByte(long addr, uint8_t byt); - void writeBytes(long addr, const void* buf, uint16_t len); - boolean busy(); - void chipErase(); - void blockErase4K(long address); - void blockErase32K(long address); - word readDeviceId(); - uint8_t* readUniqueId(); - - void sleep(); - void wakeup(); - void end(); -protected: - void select(); - void unselect(); - uint8_t _slaveSelectPin; - uint16_t _jedecID; - uint8_t _SPCR; - uint8_t _SPSR; - uint8_t _SREG; - -#ifdef SPI_HAS_TRANSACTION - SPISettings _settings; -#endif - -}; - -#endif \ No newline at end of file diff --git a/libraries/SPIFlash/examples/SPIFlash_ReadWrite/SPIFlash_ReadWrite.ino b/libraries/SPIFlash/examples/SPIFlash_ReadWrite/SPIFlash_ReadWrite.ino deleted file mode 100644 index dd8d0e8..0000000 --- a/libraries/SPIFlash/examples/SPIFlash_ReadWrite/SPIFlash_ReadWrite.ino +++ /dev/null @@ -1,116 +0,0 @@ -// ********************************************************************************** -// This sketch is an example of using the SPIFlash library with a Moteino -// that has an onboard SPI Flash chip. This sketch listens to a few serial commands -// Hence type the following commands to interact with the SPI flash memory array: -// - 'd' dumps the first 256bytes of the flash chip to screen -// - 'e' erases the entire memory chip -// - 'i' print manufacturer/device ID -// - [0-9] writes a random byte to addresses [0-9] (either 0xAA or 0xBB) -// Get the SPIFlash library from here: https://github.com/LowPowerLab/SPIFlash -// ********************************************************************************** -// Copyright Felix Rusu, LowPowerLab.com -// Library and code by Felix Rusu - felix@lowpowerlab.com -// ********************************************************************************** -// License -// ********************************************************************************** -// This program is free software; you can redistribute it -// and/or modify it under the terms of the GNU General -// Public License as published by the Free Software -// Foundation; either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will -// be useful, but WITHOUT ANY WARRANTY; without even the -// implied warranty of MERCHANTABILITY or FITNESS FOR A -// PARTICULAR PURPOSE. See the GNU General Public -// License for more details. -// -// You should have received a copy of the GNU General -// Public License along with this program. -// If not, see . -// -// Licence can be viewed at -// http://www.gnu.org/licenses/gpl-3.0.txt -// -// Please maintain this license information along with authorship -// and copyright notices in any redistribution of this code -// ********************************************************************************** - - -#include //get it here: https://github.com/LowPowerLab/SPIFlash -#include - -#define SERIAL_BAUD 115200 -char input = 0; -long lastPeriod = -1; - -#ifdef __AVR_ATmega1284P__ - #define LED 15 // Moteino MEGAs have LEDs on D15 - #define FLASH_SS 23 // and FLASH SS on D23 -#else - #define LED 9 // Moteinos have LEDs on D9 - #define FLASH_SS 8 // and FLASH SS on D8 -#endif - -////////////////////////////////////////// -// flash(SPI_CS, MANUFACTURER_ID) -// SPI_CS - CS pin attached to SPI flash chip (8 in case of Moteino) -// MANUFACTURER_ID - OPTIONAL, 0x1F44 for adesto(ex atmel) 4mbit flash -// 0xEF30 for windbond 4mbit flash -////////////////////////////////////////// -SPIFlash flash(FLASH_SS, 0xEF30); - -void setup(){ - Serial.begin(SERIAL_BAUD); - Serial.print("Start..."); - - if (flash.initialize()) - Serial.println("Init OK!"); - else - Serial.println("Init FAIL!"); -} - -void loop(){ - // Handle serial input (to allow basic DEBUGGING of FLASH chip) - // ie: display first 256 bytes in FLASH, erase chip, write bytes at first 10 positions, etc - if (Serial.available() > 0) { - input = Serial.read(); - if (input == 'd') //d=dump flash area - { - Serial.println("Flash content:"); - int counter = 0; - - while(counter<=256){ - Serial.print(flash.readByte(counter++), HEX); - Serial.print('.'); - } - - Serial.println(); - } - else if (input == 'e') - { - Serial.print("Erasing Flash chip ... "); - flash.chipErase(); - while(flash.busy()); - Serial.println("DONE"); - } - else if (input == 'i') - { - Serial.print("DeviceID: "); - Serial.println(flash.readDeviceId(), HEX); - } - else if (input >= 48 && input <= 57) //0-9 - { - Serial.print("\nWriteByte("); Serial.print(input); Serial.print(")"); - flash.writeByte(input-48, millis()%2 ? 0xaa : 0xbb); - } - } - - // Periodically blink the onboard LED while listening for serial commands - if ((int)(millis()/500) > lastPeriod) - { - lastPeriod++; - pinMode(9, OUTPUT); - digitalWrite(9, lastPeriod%2); - } -} \ No newline at end of file diff --git a/libraries/SPIFlash/keywords.txt b/libraries/SPIFlash/keywords.txt deleted file mode 100644 index 652da98..0000000 --- a/libraries/SPIFlash/keywords.txt +++ /dev/null @@ -1,18 +0,0 @@ -SPIFlash KEYWORD1 -initialize KEYWORD2 -command KEYWORD2 -readStatus KEYWORD2 -readByte KEYWORD2 -readBytes KEYWORD2 -writeByte KEYWORD2 -writeBytes KEYWORD2 -flashBusy KEYWORD2 -chipErase KEYWORD2 -blockErase4K KEYWORD2 -blockErase32K KEYWORD2 -readDeviceId KEYWORD2 -readUniqueId KEYWORD2 -UNIQUEID KEYWORD2 -sleep KEYWORD2 -wakeup KEYWORD2 -end KEYWORD2 \ No newline at end of file diff --git a/libraries/SPIFlash/library.json b/libraries/SPIFlash/library.json deleted file mode 100644 index b199318..0000000 --- a/libraries/SPIFlash/library.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "name": "SPIFlash", - "keywords": "flash, spi", - "description": "Library for read/write access to SPI flash memory chips", - "repository": - { - "type": "git", - "url": "https://github.com/LowPowerLab/SPIFlash.git" - }, - "frameworks": "arduino", - "platforms": "atmelavr" -} diff --git a/libraries/SimpleTimer/README.markdown b/libraries/SimpleTimer/README.markdown deleted file mode 100644 index 19aa6be..0000000 --- a/libraries/SimpleTimer/README.markdown +++ /dev/null @@ -1,34 +0,0 @@ -## SimpleTimer - A timer library for Arduino. - -### Uploader -Derek Chafin -Jun 16, 2012 -info...@gmail.com - -###Disclaimer -I am pushing this library to github because I like git better than I like the Windows clipboard. - -For reference this library can also be found at http://playground.arduino.cc/Code/SimpleTimer. - -### Original Author -Marcello Romani -mrom...@ottotecnica.com -Copyright (c) 2010 OTTOTECNICA Italy - -### License - GNU LGPL 2.1+ -This library is free software; you can redistribute it -and/or modify it under the terms of the GNU Lesser -General Public License as published by the Free Software -Foundation; either version 2.1 of the License, or (at -your option) any later version. - -This library is distributed in the hope that it will -be useful, but WITHOUT ANY WARRANTY; without even the -implied warranty of MERCHANTABILITY or FITNESS FOR A -PARTICULAR PURPOSE. See the GNU Lesser General Public -License for more details. - -You should have received a copy of the GNU Lesser -General Public License along with this library; if not, -write to the Free Software Foundation, Inc., -51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA \ No newline at end of file diff --git a/libraries/SimpleTimer/SimpleTimer.cpp b/libraries/SimpleTimer/SimpleTimer.cpp deleted file mode 100644 index 5a88acb..0000000 --- a/libraries/SimpleTimer/SimpleTimer.cpp +++ /dev/null @@ -1,224 +0,0 @@ -/* - * SimpleTimer.cpp - * - * SimpleTimer - A timer library for Arduino. - * Author: mromani@ottotecnica.com - * Copyright (c) 2010 OTTOTECNICA Italy - * - * This library is free software; you can redistribute it - * and/or modify it under the terms of the GNU Lesser - * General Public License as published by the Free Software - * Foundation; either version 2.1 of the License, or (at - * your option) any later version. - * - * This library is distributed in the hope that it will - * be useful, but WITHOUT ANY WARRANTY; without even the - * implied warranty of MERCHANTABILITY or FITNESS FOR A - * PARTICULAR PURPOSE. See the GNU Lesser General Public - * License for more details. - * - * You should have received a copy of the GNU Lesser - * General Public License along with this library; if not, - * write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - - -#include "SimpleTimer.h" - - -SimpleTimer::SimpleTimer() { - unsigned long current_millis = millis(); - - for (int i = 0; i < MAX_TIMERS; i++) { - enabled[i] = false; - callbacks[i] = 0; // if the callback pointer is zero, the slot is free, i.e. doesn't "contain" any timer - prev_millis[i] = current_millis; - numRuns[i] = 0; - } - - numTimers = 0; -} - - -void SimpleTimer::run() { - int i; - unsigned long current_millis; - - // get current time - current_millis = millis(); - - for (i = 0; i < MAX_TIMERS; i++) { - - toBeCalled[i] = DEFCALL_DONTRUN; - - // no callback == no timer, i.e. jump over empty slots - if (callbacks[i]) { - - // is it time to process this timer ? - if (current_millis - prev_millis[i] >= delays[i]) { - - // update time - prev_millis[i] = current_millis; - - // check if the timer callback has to be executed - if (enabled[i]) { - - // "run forever" timers must always be executed - if (maxNumRuns[i] == RUN_FOREVER) { - toBeCalled[i] = DEFCALL_RUNONLY; - } - // other timers get executed the specified number of times - else if (numRuns[i] < maxNumRuns[i]) { - toBeCalled[i] = DEFCALL_RUNONLY; - numRuns[i]++; - - // after the last run, delete the timer - if (numRuns[i] >= maxNumRuns[i]) { - toBeCalled[i] = DEFCALL_RUNANDDEL; - } - } - } - } - } - } - - for (i = 0; i < MAX_TIMERS; i++) { - switch(toBeCalled[i]) { - case DEFCALL_DONTRUN: - break; - - case DEFCALL_RUNONLY: - (*callbacks[i])(); - break; - - case DEFCALL_RUNANDDEL: - (*callbacks[i])(); - deleteTimer(i); - break; - } - } -} - - -// find the first available slot -// return -1 if none found -int SimpleTimer::findFirstFreeSlot() { - int i; - - // all slots are used - if (numTimers >= MAX_TIMERS) { - return -1; - } - - // return the first slot with no callback (i.e. free) - for (i = 0; i < MAX_TIMERS; i++) { - if (callbacks[i] == 0) { - return i; - } - } - - // we should never reach this point... - return -1; -} - - -int SimpleTimer::setTimer(long d, timer_callback f, int n) { - int freeTimer; - - freeTimer = findFirstFreeSlot(); - if (freeTimer < 0) { - return -1; - } - - delays[freeTimer] = d; - callbacks[freeTimer] = f; - maxNumRuns[freeTimer] = n; - enabled[freeTimer] = true; - prev_millis[freeTimer] = millis(); - - numTimers++; - - return freeTimer; -} - - -int SimpleTimer::setInterval(long d, timer_callback f) { - return setTimer(d, f, RUN_FOREVER); -} - - -int SimpleTimer::setTimeout(long d, timer_callback f) { - return setTimer(d, f, RUN_ONCE); -} - - -void SimpleTimer::deleteTimer(int numTimer) { - if (numTimer >= MAX_TIMERS) { - return; - } - - // nothing to delete if no timers are in use - if (numTimers == 0) { - return; - } - - callbacks[numTimer] = 0; - enabled[numTimer] = false; - delays[numTimer] = 0; - numRuns[numTimer] = 0; - - // update number of timers - numTimers--; -} - - -// function contributed by code@rowansimms.com -void SimpleTimer::restartTimer(int numTimer) { - if (numTimer >= MAX_TIMERS) { - return; - } - - prev_millis[numTimer] = millis(); -} - - -boolean SimpleTimer::isEnabled(int numTimer) { - if (numTimer >= MAX_TIMERS) { - return false; - } - - return enabled[numTimer]; -} - - -void SimpleTimer::enable(int numTimer) { - if (numTimer >= MAX_TIMERS) { - return; - } - - enabled[numTimer] = true; -} - - -void SimpleTimer::disable(int numTimer) { - if (numTimer >= MAX_TIMERS) { - return; - } - - enabled[numTimer] = false; -} - - -void SimpleTimer::toggle(int numTimer) { - if (numTimer >= MAX_TIMERS) { - return; - } - - enabled[numTimer] = !enabled[numTimer]; -} - - -int SimpleTimer::getNumTimers() { - return numTimers; -} diff --git a/libraries/SimpleTimer/SimpleTimer.h b/libraries/SimpleTimer/SimpleTimer.h deleted file mode 100644 index 4ed39b5..0000000 --- a/libraries/SimpleTimer/SimpleTimer.h +++ /dev/null @@ -1,124 +0,0 @@ -/* - * SimpleTimer.h - * - * SimpleTimer - A timer library for Arduino. - * Author: mromani@ottotecnica.com - * Copyright (c) 2010 OTTOTECNICA Italy - * - * This library is free software; you can redistribute it - * and/or modify it under the terms of the GNU Lesser - * General Public License as published by the Free Software - * Foundation; either version 2.1 of the License, or (at - * your option) any later version. - * - * This library is distributed in the hope that it will - * be useful, but WITHOUT ANY WARRANTY; without even the - * implied warranty of MERCHANTABILITY or FITNESS FOR A - * PARTICULAR PURPOSE. See the GNU Lesser General Public - * License for more details. - * - * You should have received a copy of the GNU Lesser - * General Public License along with this library; if not, - * write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - */ - - -#ifndef SIMPLETIMER_H -#define SIMPLETIMER_H - -#if defined(ARDUINO) && ARDUINO >= 100 -#include -#else -#include -#endif - -typedef void (*timer_callback)(void); - -class SimpleTimer { - -public: - // maximum number of timers - const static int MAX_TIMERS = 10; - - // setTimer() constants - const static int RUN_FOREVER = 0; - const static int RUN_ONCE = 1; - - // constructor - SimpleTimer(); - - // this function must be called inside loop() - void run(); - - // call function f every d milliseconds - int setInterval(long d, timer_callback f); - - // call function f once after d milliseconds - int setTimeout(long d, timer_callback f); - - // call function f every d milliseconds for n times - int setTimer(long d, timer_callback f, int n); - - // destroy the specified timer - void deleteTimer(int numTimer); - - // restart the specified timer - void restartTimer(int numTimer); - - // returns true if the specified timer is enabled - boolean isEnabled(int numTimer); - - // enables the specified timer - void enable(int numTimer); - - // disables the specified timer - void disable(int numTimer); - - // enables the specified timer if it's currently disabled, - // and vice-versa - void toggle(int numTimer); - - // returns the number of used timers - int getNumTimers(); - - // returns the number of available timers - int getNumAvailableTimers() { return MAX_TIMERS - numTimers; }; - -private: - // deferred call constants - const static int DEFCALL_DONTRUN = 0; // don't call the callback function - const static int DEFCALL_RUNONLY = 1; // call the callback function but don't delete the timer - const static int DEFCALL_RUNANDDEL = 2; // call the callback function and delete the timer - - // find the first available slot - int findFirstFreeSlot(); - - // value returned by the millis() function - // in the previous run() call - unsigned long prev_millis[MAX_TIMERS]; - - // pointers to the callback functions - timer_callback callbacks[MAX_TIMERS]; - - // delay values - long delays[MAX_TIMERS]; - - // number of runs to be executed for each timer - int maxNumRuns[MAX_TIMERS]; - - // number of executed runs for each timer - int numRuns[MAX_TIMERS]; - - // which timers are enabled - boolean enabled[MAX_TIMERS]; - - // deferred function call (sort of) - N.B.: this array is only used in run() - int toBeCalled[MAX_TIMERS]; - - // actual number of timers in use - int numTimers; -}; - -#endif diff --git a/libraries/SimpleTimer/keywords.txt b/libraries/SimpleTimer/keywords.txt deleted file mode 100644 index c9643fe..0000000 --- a/libraries/SimpleTimer/keywords.txt +++ /dev/null @@ -1,32 +0,0 @@ -####################################### -# Syntax Coloring Map -####################################### - -####################################### -# Datatypes (KEYWORD1) -####################################### - -SimpleTimer KEYWORD1 - -####################################### -# Methods and Functions (KEYWORD2) -####################################### -run KEYWORD2 -setInterval KEYWORD2 -setTimeout KEYWORD2 -setTimer KEYWORD2 -deleteTimer KEYWORD2 -restartTimer KEYWORD2 -isEnabled KEYWORD2 -enable KEYWORD2 -disable KEYWORD2 -toggle KEYWORD2 -getNumTimers KEYWORD2 -getNumAvailableTimers KEYWORD2 - -####################################### -# Constants (LITERAL1) -####################################### -MAX_TIMERS LITERAL1 -RUN_ONCE LITERAL1 -RUN_FOREVER LITERAL1 diff --git a/libraries/Vcc/.gitignore b/libraries/Vcc/.gitignore new file mode 100644 index 0000000..0def275 --- /dev/null +++ b/libraries/Vcc/.gitignore @@ -0,0 +1,21 @@ +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app diff --git a/libraries/Vcc/LICENSE b/libraries/Vcc/LICENSE new file mode 100644 index 0000000..86cd459 --- /dev/null +++ b/libraries/Vcc/LICENSE @@ -0,0 +1,504 @@ +GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +(This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.) + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + {description} + Copyright (C) {year} {fullname} + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random + Hacker. + + {signature of Ty Coon}, 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! \ No newline at end of file diff --git a/libraries/Vcc/README.md b/libraries/Vcc/README.md new file mode 100644 index 0000000..272818f --- /dev/null +++ b/libraries/Vcc/README.md @@ -0,0 +1,4 @@ +arduino_vcc +=========== + +Arduino library to read VCC supply level without external components diff --git a/libraries/Vcc/Vcc.cpp b/libraries/Vcc/Vcc.cpp new file mode 100644 index 0000000..8dbcbe3 --- /dev/null +++ b/libraries/Vcc/Vcc.cpp @@ -0,0 +1,78 @@ +/* + Vcc - A supply voltage measuring library for Arduino + + Created by Ivo Pullens, Emmission, 2014 + + Inspired by: + http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/ + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "Vcc.h" + +Vcc::Vcc( const float correction ) + : m_correction(correction) +{ +} + +#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) +#define ADMUX_VCCWRT1V1 (_BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1)) +#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) +#define ADMUX_VCCWRT1V1 (_BV(MUX5) | _BV(MUX0)) +#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) +#define ADMUX_VCCWRT1V1 (_BV(MUX3) | _BV(MUX2)) +#else +#define ADMUX_VCCWRT1V1 (_BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1)) +#endif + +float Vcc::Read_Volts(void) +{ + // Read 1.1V reference against AVcc + // set the reference to Vcc and the measurement to the internal 1.1V reference + if (ADMUX != ADMUX_VCCWRT1V1) + { + ADMUX = ADMUX_VCCWRT1V1; + + // Bandgap reference start-up time: max 70us + // Wait for Vref to settle. + delayMicroseconds(350); + } + + // Start conversion and wait for it to finish. + ADCSRA |= _BV(ADSC); + while (bit_is_set(ADCSRA,ADSC)) {}; + + // Result is now stored in ADC. + + // Calculate Vcc (in V) + float vcc = 1.1*1024.0 / ADC; + + // Apply compensation + vcc *= m_correction; + + return vcc; +} + +float Vcc::Read_Perc(const float range_min, const float range_max, const boolean clip) +{ + // Read Vcc and convert to percentage + float perc = 100.0 * (Read_Volts()-range_min) / (range_max-range_min); + // Clip to [0..100]% range, when requested. + if (clip) + perc = constrain(perc, 0.0, 100.0); + + return perc; +} diff --git a/libraries/Vcc/Vcc.h b/libraries/Vcc/Vcc.h new file mode 100644 index 0000000..1d82e22 --- /dev/null +++ b/libraries/Vcc/Vcc.h @@ -0,0 +1,64 @@ +/* + Vcc - A supply voltage measuring library for Arduino + + Created by Ivo Pullens, Emmission, 2014 + + Inspired by: + http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/ + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef VCC_H +#define VCC_H + +#include "Arduino.h" + +class Vcc +{ + public: + /** + * Constructor + * + * @param correction Correction factor, when reported Vcc is off from measured (externally) Vcc + * (due to variations in bandgap voltage of +/- 0.1V) + * Calculate as Vcc_measured/Vcc_reported. Defaults to 1. + */ + Vcc( const float correction = 1.0 ); + + /** + * Retrieve current Vcc level. + * + * @return Current Vcc level, in Volts. + */ + float Read_Volts(void); + + /** + * Retrieve current Vcc level. The total voltage range shall be passed + * as low/high bound. For e.g. an alkaline AA battery this range can be set + * to [0.6,..,1.5] Volts. + * + * @param range_min Low bound to Vcc level range, in Volts. + * @param range_max High bound to Vcc level range, in Volts. + * @param clip When set, assures returned percentage is clipped to [0..100]% range. + * @return Current Vcc level, as percentage of expected Vcc level. + */ + float Read_Perc(const float range_min = 0.0, const float range_max = 0.0, const boolean clip = true); + + protected: + float m_correction; /**< Correction factor, when reported Vcc is off. */ +}; + +#endif \ No newline at end of file diff --git a/libraries/Vcc/examples/VccSimple/VccSimple.ino b/libraries/Vcc/examples/VccSimple/VccSimple.ino new file mode 100644 index 0000000..cbbe704 --- /dev/null +++ b/libraries/Vcc/examples/VccSimple/VccSimple.ino @@ -0,0 +1,27 @@ +#include + +const float VccMin = 0.0; // Minimum expected Vcc level, in Volts. +const float VccMax = 5.0; // Maximum expected Vcc level, in Volts. +const float VccCorrection = 1.0/1.0; // Measured Vcc by multimeter divided by reported Vcc + +Vcc vcc(VccCorrection); + +void setup() +{ + Serial.begin(115200); +} + +void loop() +{ + float v = vcc.Read_Volts(); + Serial.print("VCC = "); + Serial.print(v); + Serial.println(" Volts"); + + float p = vcc.Read_Perc(VccMin, VccMax); + Serial.print("VCC = "); + Serial.print(p); + Serial.println(" %"); + + delay(2000); +} diff --git a/libraries/Vcc/examples/VccSleep/VccSleep.ino b/libraries/Vcc/examples/VccSleep/VccSleep.ino new file mode 100644 index 0000000..7523fbe --- /dev/null +++ b/libraries/Vcc/examples/VccSleep/VccSleep.ino @@ -0,0 +1,30 @@ +#include +#include + +const float VccMin = 2.0*0.6; // Minimum expected Vcc level, in Volts. Example for 2xAA Alkaline. +const float VccMax = 2.0*1.5; // Maximum expected Vcc level, in Volts. Example for 2xAA Alkaline. +const float VccCorrection = 1.0/1.0; // Measured Vcc by multimeter divided by reported Vcc + +Vcc vcc(VccCorrection); + +void setup() +{ + Serial.begin(115200); +} + +void loop() +{ + float v = vcc.Read_Volts(); + Serial.print("VCC = "); + Serial.print(v); + Serial.println(" Volts"); + + float p = vcc.Read_Perc(VccMin, VccMax); + Serial.print("VCC = "); + Serial.print(p); + Serial.println(" %"); + + delay(200); //delay to allow serial to fully print before sleep + + LowPower.powerDown(SLEEP_2S, ADC_OFF, BOD_OFF); +} diff --git a/libraries/sha204/sha204_lib_return_codes.h b/libraries/sha204/sha204_lib_return_codes.h deleted file mode 100644 index db61120..0000000 --- a/libraries/sha204/sha204_lib_return_codes.h +++ /dev/null @@ -1,48 +0,0 @@ -// ---------------------------------------------------------------------------- -// ATMEL Microcontroller Software Support - Colorado Springs, CO - -// ---------------------------------------------------------------------------- -// DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR -// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE -// DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, -// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, -// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, -// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// ---------------------------------------------------------------------------- - -/** \file - * \brief SHA204 Library Return Code Definitions - * \author Atmel Crypto Products - * \date September 27, 2010 - */ - -#ifndef SHA204_LIB_RETURN_CODES_H -# define SHA204_LIB_RETURN_CODES_H - -#include // data type definitions - -/** \todo Use same values for same meanings for SHA204 and AES132. - * */ - -#define SHA204_SUCCESS ((uint8_t) 0x00) //!< Function succeeded. -#define SHA204_PARSE_ERROR ((uint8_t) 0xD2) //!< response status byte indicates parsing error -#define SHA204_CMD_FAIL ((uint8_t) 0xD3) //!< response status byte indicates command execution error -#define SHA204_STATUS_CRC ((uint8_t) 0xD4) //!< response status byte indicates CRC error -#define SHA204_STATUS_UNKNOWN ((uint8_t) 0xD5) //!< response status byte is unknown -#define SHA204_FUNC_FAIL ((uint8_t) 0xE0) //!< Function could not execute due to incorrect condition / state. -#define SHA204_GEN_FAIL ((uint8_t) 0xE1) //!< unspecified error -#define SHA204_BAD_PARAM ((uint8_t) 0xE2) //!< bad argument (out of range, null pointer, etc.) -#define SHA204_INVALID_ID ((uint8_t) 0xE3) //!< invalid device id, id not set -#define SHA204_INVALID_SIZE ((uint8_t) 0xE4) //!< Count value is out of range or greater than buffer size. -#define SHA204_BAD_CRC ((uint8_t) 0xE5) //!< incorrect CRC received -#define SHA204_RX_FAIL ((uint8_t) 0xE6) //!< Timed out while waiting for response. Number of bytes received is > 0. -#define SHA204_RX_NO_RESPONSE ((uint8_t) 0xE7) //!< Not an error while the Command layer is polling for a command response. -#define SHA204_RESYNC_WITH_WAKEUP ((uint8_t) 0xE8) //!< re-synchronization succeeded, but only after generating a Wake-up - -#define SHA204_COMM_FAIL ((uint8_t) 0xF0) //!< Communication with device failed. Same as in hardware dependent modules. -#define SHA204_TIMEOUT ((uint8_t) 0xF1) //!< Timed out while waiting for response. Number of bytes received is 0. - -#endif diff --git a/libraries/sha204/sha204_library.cpp b/libraries/sha204/sha204_library.cpp deleted file mode 100644 index a747c4d..0000000 --- a/libraries/sha204/sha204_library.cpp +++ /dev/null @@ -1,839 +0,0 @@ -#include "Arduino.h" -#include "sha204_library.h" -#include "sha204_lib_return_codes.h" - - -// atsha204Class Constructor -// Feed this function the Arduino-ized pin number you want to assign to the ATSHA204's SDA pin -// For AVR ARCH, this will find the DDRX, PORTX, and PINX registrs it'll need to point to to control that pin -// As well as the bit value for each of those registers -// For others it will only store the pin number which is used in subsequent functions - -atsha204Class::atsha204Class(uint8_t pin) -{ -#if defined(ARDUINO_ARCH_AVR) - device_pin = digitalPinToBitMask(pin); // Find the bit value of the pin - uint8_t port = digitalPinToPort(pin); // temoporarily used to get the next three registers - - // Point to data direction register port of pin - device_port_DDR = portModeRegister(port); - // Point to output register of pin - device_port_OUT = portOutputRegister(port); - // Point to input register of pin - device_port_IN = portInputRegister(port); -#else - device_pin = pin; -#endif -} - -/* Puts a the ATSHA204's unique, 4-byte serial number in the response array - returns an SHA204 Return code */ -uint8_t atsha204Class::getSerialNumber(uint8_t * response) -{ - uint8_t readCommand[READ_COUNT]; - uint8_t readResponse[READ_4_RSP_SIZE]; - - /* read from bytes 0->3 of config zone */ - uint8_t returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN03); - if (!returnCode) // should return 0 if successful - { - for (int i=0; i<4; i++) // store bytes 0-3 into respones array - response[i] = readResponse[SHA204_BUFFER_POS_DATA+i]; - - /* read from bytes 8->11 of config zone */ - returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN47); - - for (int i=4; i<8; i++) // store bytes 4-7 of SN into response array - response[i] = readResponse[SHA204_BUFFER_POS_DATA+(i-4)]; - - if (!returnCode) - { /* Finally if last two reads were successful, read byte 8 of the SN */ - returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN8); - response[8] = readResponse[SHA204_BUFFER_POS_DATA]; // Byte 8 of SN should always be 0xEE - } - } - - return returnCode; -} - -/* Calculates CRC16 value of provided data (and optionally including provided existing CRC16 data) - returns the calculated CRC16 value */ -uint16_t atsha204Class::calculateAndUpdateCrc(uint8_t length, uint8_t *data, uint16_t current_crc) -{ - uint8_t counter; - uint16_t crc_register = current_crc; - uint16_t polynom = 0x8005; - uint8_t shift_register; - uint8_t data_bit, crc_bit; - - for (counter = 0; counter < length; counter++) - { - for (shift_register = 0x01; shift_register > 0x00; shift_register <<= 1) - { - data_bit = (data[counter] & shift_register) ? 1 : 0; - crc_bit = crc_register >> 15; - - // Shift CRC to the left by 1. - crc_register <<= 1; - - if ((data_bit ^ crc_bit) != 0) - crc_register ^= polynom; - } - } - return crc_register; -} - -/* SWI bit bang functions */ - -void atsha204Class::swi_set_signal_pin(uint8_t is_high) -{ - SHA204_SET_OUTPUT(); - if (is_high) { - SHA204_POUT_HIGH(); - } - else { - SHA204_POUT_LOW(); - } - -} - -uint8_t atsha204Class::swi_send_bytes(uint8_t count, uint8_t *buffer) -{ - uint8_t i, bit_mask; - - // Disable interrupts while sending. - noInterrupts(); //swi_disable_interrupts(); - - // Set signal pin as output. - SHA204_POUT_HIGH(); - SHA204_SET_OUTPUT(); - // Wait turn around time. - delayMicroseconds(RX_TX_DELAY); //RX_TX_DELAY; - - for (i = 0; i < count; i++) - { - for (bit_mask = 1; bit_mask > 0; bit_mask <<= 1) - { - if (bit_mask & buffer[i]) - { - SHA204_POUT_LOW(); - delayMicroseconds(BIT_DELAY); //BIT_DELAY_1; - SHA204_POUT_HIGH(); - delayMicroseconds(7*BIT_DELAY); //BIT_DELAY_7; - } - else - { - // Send a zero bit. - SHA204_POUT_LOW(); - delayMicroseconds(BIT_DELAY); //BIT_DELAY_1; - SHA204_POUT_HIGH(); - delayMicroseconds(BIT_DELAY); //BIT_DELAY_1; - SHA204_POUT_LOW(); - delayMicroseconds(BIT_DELAY); //BIT_DELAY_1; - SHA204_POUT_HIGH(); - delayMicroseconds(5*BIT_DELAY); //BIT_DELAY_5; - } - } - } - interrupts(); //swi_enable_interrupts(); - return SWI_FUNCTION_RETCODE_SUCCESS; -} - -uint8_t atsha204Class::swi_send_byte(uint8_t value) -{ - return swi_send_bytes(1, &value); -} - -uint8_t atsha204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer) -{ - uint8_t status = SWI_FUNCTION_RETCODE_SUCCESS; - uint8_t i; - uint8_t bit_mask; - uint8_t pulse_count; - uint8_t timeout_count; - - // Disable interrupts while receiving. - noInterrupts(); //swi_disable_interrupts(); - - // Configure signal pin as input - SHA204_SET_INPUT(); - - // Receive bits and store in buffer. - for (i = 0; i < count; i++) - { - for (bit_mask = 1; bit_mask > 0; bit_mask <<= 1) - { - pulse_count = 0; - - // Make sure that the variable below is big enough. - // Change it to uint16_t if 255 is too small, but be aware that - // the loop resolution decreases on an 8-bit controller in that case. - timeout_count = START_PULSE_TIME_OUT; - - // Detect start bit. - while (--timeout_count > 0) - { - // Wait for falling edge. - if (SHA204_PIN_READ() == 0) - break; - } - - if (timeout_count == 0) - { - status = SWI_FUNCTION_RETCODE_TIMEOUT; - break; - } - - do - { - // Wait for rising edge. - if (SHA204_PIN_READ() != 0) - { - // For an Atmel microcontroller this might be faster than "pulse_count++". - pulse_count = 1; - break; - } - } while (--timeout_count > 0); - - if (pulse_count == 0) - { - status = SWI_FUNCTION_RETCODE_TIMEOUT; - break; - } - - // Trying to measure the time of start bit and calculating the timeout - // for zero bit detection is not accurate enough for an 8 MHz 8-bit CPU. - // So let's just wait the maximum time for the falling edge of a zero bit - // to arrive after we have detected the rising edge of the start bit. - timeout_count = ZERO_PULSE_TIME_OUT; - - // Detect possible edge indicating zero bit. - do - { - if (SHA204_PIN_READ() == 0) - { - // For an Atmel microcontroller this might be faster than "pulse_count++". - pulse_count = 2; - break; - } - } while (--timeout_count > 0); - - // Wait for rising edge of zero pulse before returning. Otherwise we might interpret - // its rising edge as the next start pulse. - if (pulse_count == 2) - { - do - { - if (SHA204_PIN_READ() != 0) - break; - } while (timeout_count-- > 0); - } - - // Update byte at current buffer index. - else - buffer[i] |= bit_mask; // received "one" bit - } - - if (status != SWI_FUNCTION_RETCODE_SUCCESS) - break; - } - interrupts(); //swi_enable_interrupts(); - - if (status == SWI_FUNCTION_RETCODE_TIMEOUT) - { - if (i > 0) - // Indicate that we timed out after having received at least one byte. - status = SWI_FUNCTION_RETCODE_RX_FAIL; - } - return status; -} - -/* Physical functions */ - -uint8_t atsha204Class::sha204p_wakeup() -{ - swi_set_signal_pin(0); - delayMicroseconds(10*SHA204_WAKEUP_PULSE_WIDTH); - swi_set_signal_pin(1); - delay(SHA204_WAKEUP_DELAY); - - return SHA204_SUCCESS; -} - -uint8_t atsha204Class::sha204p_sleep() -{ - return swi_send_byte(SHA204_SWI_FLAG_SLEEP); -} - -uint8_t atsha204Class::sha204p_resync(uint8_t size, uint8_t *response) -{ - delay(SHA204_SYNC_TIMEOUT); - return sha204p_receive_response(size, response); -} - -uint8_t atsha204Class::sha204p_receive_response(uint8_t size, uint8_t *response) -{ - uint8_t count_byte; - uint8_t i; - uint8_t ret_code; - - for (i = 0; i < size; i++) - response[i] = 0; - - (void) swi_send_byte(SHA204_SWI_FLAG_TX); - - ret_code = swi_receive_bytes(size, response); - if (ret_code == SWI_FUNCTION_RETCODE_SUCCESS || ret_code == SWI_FUNCTION_RETCODE_RX_FAIL) - { - count_byte = response[SHA204_BUFFER_POS_COUNT]; - if ((count_byte < SHA204_RSP_SIZE_MIN) || (count_byte > size)) - return SHA204_INVALID_SIZE; - - return SHA204_SUCCESS; - } - - // Translate error so that the Communication layer - // can distinguish between a real error or the - // device being busy executing a command. - if (ret_code == SWI_FUNCTION_RETCODE_TIMEOUT) - return SHA204_RX_NO_RESPONSE; - else - return SHA204_RX_FAIL; -} - -uint8_t atsha204Class::sha204p_send_command(uint8_t count, uint8_t * command) -{ - uint8_t ret_code = swi_send_byte(SHA204_SWI_FLAG_CMD); - if (ret_code != SWI_FUNCTION_RETCODE_SUCCESS) - return SHA204_COMM_FAIL; - - return swi_send_bytes(count, command); -} - -/* Communication functions */ - -uint8_t atsha204Class::sha204c_wakeup(uint8_t *response) -{ - uint8_t ret_code = sha204p_wakeup(); - if (ret_code != SHA204_SUCCESS) - return ret_code; - - ret_code = sha204p_receive_response(SHA204_RSP_SIZE_MIN, response); - if (ret_code != SHA204_SUCCESS) - return ret_code; - - // Verify status response. - if (response[SHA204_BUFFER_POS_COUNT] != SHA204_RSP_SIZE_MIN) - ret_code = SHA204_INVALID_SIZE; - else if (response[SHA204_BUFFER_POS_STATUS] != SHA204_STATUS_BYTE_WAKEUP) - ret_code = SHA204_COMM_FAIL; - else - { - if ((response[SHA204_RSP_SIZE_MIN - SHA204_CRC_SIZE] != 0x33) - || (response[SHA204_RSP_SIZE_MIN + 1 - SHA204_CRC_SIZE] != 0x43)) - ret_code = SHA204_BAD_CRC; - } - if (ret_code != SHA204_SUCCESS) - delay(SHA204_COMMAND_EXEC_MAX); - - return ret_code; -} - -uint8_t atsha204Class::sha204c_resync(uint8_t size, uint8_t *response) -{ - // Try to re-synchronize without sending a Wake token - // (step 1 of the re-synchronization process). - uint8_t ret_code = sha204p_resync(size, response); - if (ret_code == SHA204_SUCCESS) - return ret_code; - - // We lost communication. Send a Wake pulse and try - // to receive a response (steps 2 and 3 of the - // re-synchronization process). - (void) sha204p_sleep(); - ret_code = sha204c_wakeup(response); - - // Translate a return value of success into one - // that indicates that the device had to be woken up - // and might have lost its TempKey. - return (ret_code == SHA204_SUCCESS ? SHA204_RESYNC_WITH_WAKEUP : ret_code); -} - -uint8_t atsha204Class::sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer, uint8_t execution_delay, uint8_t execution_timeout) -{ - uint8_t ret_code = SHA204_FUNC_FAIL; - uint8_t ret_code_resync; - uint8_t n_retries_send; - uint8_t n_retries_receive; - uint8_t i; - uint8_t status_byte; - uint8_t count = tx_buffer[SHA204_BUFFER_POS_COUNT]; - uint8_t count_minus_crc = count - SHA204_CRC_SIZE; - uint16_t execution_timeout_us = (uint16_t) (execution_timeout * 1000) + SHA204_RESPONSE_TIMEOUT; - volatile uint16_t timeout_countdown; - - // Append CRC. - sha204c_calculate_crc(count_minus_crc, tx_buffer, tx_buffer + count_minus_crc); - - // Retry loop for sending a command and receiving a response. - n_retries_send = SHA204_RETRY_COUNT + 1; - - while ((n_retries_send-- > 0) && (ret_code != SHA204_SUCCESS)) - { - // Send command. - ret_code = sha204p_send_command(count, tx_buffer); - if (ret_code != SHA204_SUCCESS) - { - if (sha204c_resync(rx_size, rx_buffer) == SHA204_RX_NO_RESPONSE) - return ret_code; // The device seems to be dead in the water. - else - continue; - } - - // Wait minimum command execution time and then start polling for a response. - delay(execution_delay); - - // Retry loop for receiving a response. - n_retries_receive = SHA204_RETRY_COUNT + 1; - while (n_retries_receive-- > 0) - { - // Reset response buffer. - for (i = 0; i < rx_size; i++) - rx_buffer[i] = 0; - - // Poll for response. - timeout_countdown = execution_timeout_us; - do - { - ret_code = sha204p_receive_response(rx_size, rx_buffer); - timeout_countdown -= SHA204_RESPONSE_TIMEOUT; - } - while ((timeout_countdown > SHA204_RESPONSE_TIMEOUT) && (ret_code == SHA204_RX_NO_RESPONSE)); - - if (ret_code == SHA204_RX_NO_RESPONSE) - { - // We did not receive a response. Re-synchronize and send command again. - if (sha204c_resync(rx_size, rx_buffer) == SHA204_RX_NO_RESPONSE) - // The device seems to be dead in the water. - return ret_code; - else - break; - } - - // Check whether we received a valid response. - if (ret_code == SHA204_INVALID_SIZE) - { - // We see 0xFF for the count when communication got out of sync. - ret_code_resync = sha204c_resync(rx_size, rx_buffer); - if (ret_code_resync == SHA204_SUCCESS) - // We did not have to wake up the device. Try receiving response again. - continue; - if (ret_code_resync == SHA204_RESYNC_WITH_WAKEUP) - // We could re-synchronize, but only after waking up the device. - // Re-send command. - break; - else - // We failed to re-synchronize. - return ret_code; - } - - // We received a response of valid size. - // Check the consistency of the response. - ret_code = sha204c_check_crc(rx_buffer); - if (ret_code == SHA204_SUCCESS) - { - // Received valid response. - if (rx_buffer[SHA204_BUFFER_POS_COUNT] > SHA204_RSP_SIZE_MIN) - // Received non-status response. We are done. - return ret_code; - - // Received status response. - status_byte = rx_buffer[SHA204_BUFFER_POS_STATUS]; - - // Translate the three possible device status error codes - // into library return codes. - if (status_byte == SHA204_STATUS_BYTE_PARSE) - return SHA204_PARSE_ERROR; - if (status_byte == SHA204_STATUS_BYTE_EXEC) - return SHA204_CMD_FAIL; - if (status_byte == SHA204_STATUS_BYTE_COMM) - { - // In case of the device status byte indicating a communication - // error this function exits the retry loop for receiving a response - // and enters the overall retry loop - // (send command / receive response). - ret_code = SHA204_STATUS_CRC; - break; - } - - // Received status response from CheckMAC, DeriveKey, GenDig, - // Lock, Nonce, Pause, UpdateExtra, or Write command. - return ret_code; - } - - else - { - // Received response with incorrect CRC. - ret_code_resync = sha204c_resync(rx_size, rx_buffer); - if (ret_code_resync == SHA204_SUCCESS) - // We did not have to wake up the device. Try receiving response again. - continue; - if (ret_code_resync == SHA204_RESYNC_WITH_WAKEUP) - // We could re-synchronize, but only after waking up the device. - // Re-send command. - break; - else - // We failed to re-synchronize. - return ret_code; - } // block end of check response consistency - - } // block end of receive retry loop - - } // block end of send and receive retry loop - - return ret_code; -} - - -/* Marshaling functions */ - -uint8_t atsha204Class::sha204m_random(uint8_t * tx_buffer, uint8_t * rx_buffer, uint8_t mode) -{ - if (!tx_buffer || !rx_buffer || (mode > RANDOM_NO_SEED_UPDATE)) - return SHA204_BAD_PARAM; - - tx_buffer[SHA204_COUNT_IDX] = RANDOM_COUNT; - tx_buffer[SHA204_OPCODE_IDX] = SHA204_RANDOM; - tx_buffer[RANDOM_MODE_IDX] = mode & RANDOM_SEED_UPDATE; - - tx_buffer[RANDOM_PARAM2_IDX] = - tx_buffer[RANDOM_PARAM2_IDX + 1] = 0; - - return sha204c_send_and_receive(&tx_buffer[0], RANDOM_RSP_SIZE, &rx_buffer[0], RANDOM_DELAY, RANDOM_EXEC_MAX - RANDOM_DELAY); -} - -uint8_t atsha204Class::sha204m_dev_rev(uint8_t *tx_buffer, uint8_t *rx_buffer) -{ - if (!tx_buffer || !rx_buffer) - return SHA204_BAD_PARAM; - - tx_buffer[SHA204_COUNT_IDX] = DEVREV_COUNT; - tx_buffer[SHA204_OPCODE_IDX] = SHA204_DEVREV; - - // Parameters are 0. - tx_buffer[DEVREV_PARAM1_IDX] = - tx_buffer[DEVREV_PARAM2_IDX] = - tx_buffer[DEVREV_PARAM2_IDX + 1] = 0; - - return sha204c_send_and_receive(&tx_buffer[0], DEVREV_RSP_SIZE, &rx_buffer[0], - DEVREV_DELAY, DEVREV_EXEC_MAX - DEVREV_DELAY); -} - -uint8_t atsha204Class::sha204m_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address) -{ - uint8_t rx_size; - - if (!tx_buffer || !rx_buffer || ((zone & ~READ_ZONE_MASK) != 0) - || ((zone & READ_ZONE_MODE_32_BYTES) && (zone == SHA204_ZONE_OTP))) - return SHA204_BAD_PARAM; - - address >>= 2; - if ((zone & SHA204_ZONE_MASK) == SHA204_ZONE_CONFIG) - { - if (address > SHA204_ADDRESS_MASK_CONFIG) - return SHA204_BAD_PARAM; - } - else if ((zone & SHA204_ZONE_MASK) == SHA204_ZONE_OTP) - { - if (address > SHA204_ADDRESS_MASK_OTP) - return SHA204_BAD_PARAM; - } - else if ((zone & SHA204_ZONE_MASK) == SHA204_ZONE_DATA) - { - if (address > SHA204_ADDRESS_MASK) - return SHA204_BAD_PARAM; - } - - tx_buffer[SHA204_COUNT_IDX] = READ_COUNT; - tx_buffer[SHA204_OPCODE_IDX] = SHA204_READ; - tx_buffer[READ_ZONE_IDX] = zone; - tx_buffer[READ_ADDR_IDX] = (uint8_t) (address & SHA204_ADDRESS_MASK); - tx_buffer[READ_ADDR_IDX + 1] = 0; - - rx_size = (zone & SHA204_ZONE_COUNT_FLAG) ? READ_32_RSP_SIZE : READ_4_RSP_SIZE; - - return sha204c_send_and_receive(&tx_buffer[0], rx_size, &rx_buffer[0], READ_DELAY, READ_EXEC_MAX - READ_DELAY); -} - -uint8_t atsha204Class::sha204m_execute(uint8_t op_code, uint8_t param1, uint16_t param2, - uint8_t datalen1, uint8_t *data1, uint8_t datalen2, uint8_t *data2, uint8_t datalen3, uint8_t *data3, - uint8_t tx_size, uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer) -{ - uint8_t poll_delay, poll_timeout, response_size; - uint8_t *p_buffer; - uint8_t len; - - uint8_t ret_code = sha204m_check_parameters(op_code, param1, param2, - datalen1, data1, datalen2, data2, datalen3, data3, - tx_size, tx_buffer, rx_size, rx_buffer); - if (ret_code != SHA204_SUCCESS) - return ret_code; - - // Supply delays and response size. - switch (op_code) - { - case SHA204_CHECKMAC: - poll_delay = CHECKMAC_DELAY; - poll_timeout = CHECKMAC_EXEC_MAX - CHECKMAC_DELAY; - response_size = CHECKMAC_RSP_SIZE; - break; - - case SHA204_DERIVE_KEY: - poll_delay = DERIVE_KEY_DELAY; - poll_timeout = DERIVE_KEY_EXEC_MAX - DERIVE_KEY_DELAY; - response_size = DERIVE_KEY_RSP_SIZE; - break; - - case SHA204_DEVREV: - poll_delay = DEVREV_DELAY; - poll_timeout = DEVREV_EXEC_MAX - DEVREV_DELAY; - response_size = DEVREV_RSP_SIZE; - break; - - case SHA204_GENDIG: - poll_delay = GENDIG_DELAY; - poll_timeout = GENDIG_EXEC_MAX - GENDIG_DELAY; - response_size = GENDIG_RSP_SIZE; - break; - - case SHA204_HMAC: - poll_delay = HMAC_DELAY; - poll_timeout = HMAC_EXEC_MAX - HMAC_DELAY; - response_size = HMAC_RSP_SIZE; - break; - - case SHA204_LOCK: - poll_delay = LOCK_DELAY; - poll_timeout = LOCK_EXEC_MAX - LOCK_DELAY; - response_size = LOCK_RSP_SIZE; - break; - - case SHA204_MAC: - poll_delay = MAC_DELAY; - poll_timeout = MAC_EXEC_MAX - MAC_DELAY; - response_size = MAC_RSP_SIZE; - break; - - case SHA204_NONCE: - poll_delay = NONCE_DELAY; - poll_timeout = NONCE_EXEC_MAX - NONCE_DELAY; - response_size = param1 == NONCE_MODE_PASSTHROUGH - ? NONCE_RSP_SIZE_SHORT : NONCE_RSP_SIZE_LONG; - break; - - case SHA204_PAUSE: - poll_delay = PAUSE_DELAY; - poll_timeout = PAUSE_EXEC_MAX - PAUSE_DELAY; - response_size = PAUSE_RSP_SIZE; - break; - - case SHA204_RANDOM: - poll_delay = RANDOM_DELAY; - poll_timeout = RANDOM_EXEC_MAX - RANDOM_DELAY; - response_size = RANDOM_RSP_SIZE; - break; - - case SHA204_READ: - poll_delay = READ_DELAY; - poll_timeout = READ_EXEC_MAX - READ_DELAY; - response_size = (param1 & SHA204_ZONE_COUNT_FLAG) - ? READ_32_RSP_SIZE : READ_4_RSP_SIZE; - break; - - case SHA204_UPDATE_EXTRA: - poll_delay = UPDATE_DELAY; - poll_timeout = UPDATE_EXEC_MAX - UPDATE_DELAY; - response_size = UPDATE_RSP_SIZE; - break; - - case SHA204_WRITE: - poll_delay = WRITE_DELAY; - poll_timeout = WRITE_EXEC_MAX - WRITE_DELAY; - response_size = WRITE_RSP_SIZE; - break; - - default: - poll_delay = 0; - poll_timeout = SHA204_COMMAND_EXEC_MAX; - response_size = rx_size; - } - - // Assemble command. - len = datalen1 + datalen2 + datalen3 + SHA204_CMD_SIZE_MIN; - p_buffer = tx_buffer; - *p_buffer++ = len; - *p_buffer++ = op_code; - *p_buffer++ = param1; - *p_buffer++ = param2 & 0xFF; - *p_buffer++ = param2 >> 8; - - if (datalen1 > 0) { - memcpy(p_buffer, data1, datalen1); - p_buffer += datalen1; - } - if (datalen2 > 0) { - memcpy(p_buffer, data2, datalen2); - p_buffer += datalen2; - } - if (datalen3 > 0) { - memcpy(p_buffer, data3, datalen3); - p_buffer += datalen3; - } - - sha204c_calculate_crc(len - SHA204_CRC_SIZE, tx_buffer, p_buffer); - - // Send command and receive response. - return sha204c_send_and_receive(&tx_buffer[0], response_size, - &rx_buffer[0], poll_delay, poll_timeout); -} - -uint8_t atsha204Class::sha204m_check_parameters(uint8_t op_code, uint8_t param1, uint16_t param2, - uint8_t datalen1, uint8_t *data1, uint8_t datalen2, uint8_t *data2, uint8_t datalen3, uint8_t *data3, - uint8_t tx_size, uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer) -{ -#ifdef SHA204_CHECK_PARAMETERS - - uint8_t len = datalen1 + datalen2 + datalen3 + SHA204_CMD_SIZE_MIN; - if (!tx_buffer || tx_size < len || rx_size < SHA204_RSP_SIZE_MIN || !rx_buffer) - return SHA204_BAD_PARAM; - - if ((datalen1 > 0 && !data1) || (datalen2 > 0 && !data2) || (datalen3 > 0 && !data3)) - return SHA204_BAD_PARAM; - - // Check parameters depending on op-code. - switch (op_code) - { - case SHA204_CHECKMAC: - if ( - // no null pointers allowed - !data1 || !data2 - // No reserved bits should be set. - || (param1 | CHECKMAC_MODE_MASK) != CHECKMAC_MODE_MASK - // key_id > 15 not allowed - || param2 > SHA204_KEY_ID_MAX - ) - return SHA204_BAD_PARAM; - break; - - case SHA204_DERIVE_KEY: - if (param2 > SHA204_KEY_ID_MAX) - return SHA204_BAD_PARAM; - break; - - case SHA204_DEVREV: - break; - - case SHA204_GENDIG: - if ((param1 != GENDIG_ZONE_OTP) && (param1 != GENDIG_ZONE_DATA)) - return SHA204_BAD_PARAM; - break; - - case SHA204_HMAC: - if ((param1 & ~HMAC_MODE_MASK) != 0) - return SHA204_BAD_PARAM; - break; - - case SHA204_LOCK: - if (((param1 & ~LOCK_ZONE_MASK) != 0) - || ((param1 & LOCK_ZONE_NO_CRC) && (param2 != 0))) - return SHA204_BAD_PARAM; - break; - - case SHA204_MAC: - if (((param1 & ~MAC_MODE_MASK) != 0) - || (((param1 & MAC_MODE_BLOCK2_TEMPKEY) == 0) && !data1)) - return SHA204_BAD_PARAM; - break; - - case SHA204_NONCE: - if ( !data1 - || (param1 > NONCE_MODE_PASSTHROUGH) - || (param1 == NONCE_MODE_INVALID) - ) - return SHA204_BAD_PARAM; - break; - - case SHA204_PAUSE: - break; - - case SHA204_RANDOM: - if (param1 > RANDOM_NO_SEED_UPDATE) - return SHA204_BAD_PARAM; - break; - - case SHA204_READ: - if (((param1 & ~READ_ZONE_MASK) != 0) - || ((param1 & READ_ZONE_MODE_32_BYTES) && (param1 == SHA204_ZONE_OTP))) - return SHA204_BAD_PARAM; - break; - - case SHA204_TEMPSENSE: - break; - - case SHA204_UPDATE_EXTRA: - if (param1 > UPDATE_CONFIG_BYTE_86) - return SHA204_BAD_PARAM; - break; - - case SHA204_WRITE: - if (!data1 || ((param1 & ~WRITE_ZONE_MASK) != 0)) - return SHA204_BAD_PARAM; - break; - - default: - // unknown op-code - return SHA204_BAD_PARAM; - } - - return SHA204_SUCCESS; - -#else - (void)rx_size; - (void)tx_size; - (void)tx_buffer; - (void)rx_buffer; - (void)param1; - (void)param2; - (void)data1; - (void)data2; - (void)data3; - (void)datalen1; - (void)datalen2; - (void)datalen3; - (void)op_code; - return SHA204_SUCCESS; -#endif -} - -/* CRC Calculator and Checker */ - -void atsha204Class::sha204c_calculate_crc(uint8_t length, uint8_t *data, uint8_t *crc) -{ - uint16_t crc_register = 0; - - crc_register = calculateAndUpdateCrc(length, data, crc_register); - crc[0] = (uint8_t) (crc_register & 0x00FF); - crc[1] = (uint8_t) (crc_register >> 8); -} - -uint8_t atsha204Class::sha204c_check_crc(uint8_t *response) -{ - uint8_t crc[SHA204_CRC_SIZE]; - uint8_t count = response[SHA204_BUFFER_POS_COUNT]; - - count -= SHA204_CRC_SIZE; - sha204c_calculate_crc(count, response, crc); - - return (crc[0] == response[count] && crc[1] == response[count + 1]) - ? SHA204_SUCCESS : SHA204_BAD_CRC; -} diff --git a/libraries/sha204/sha204_library.h b/libraries/sha204/sha204_library.h deleted file mode 100644 index c22f9ed..0000000 --- a/libraries/sha204/sha204_library.h +++ /dev/null @@ -1,340 +0,0 @@ -#include "Arduino.h" - -#ifndef sha204_library_H -#define sha204_library_H - -/* bitbang_config.h */ - -#define PORT_ACCESS_TIME (630) //! time it takes to toggle the pin at CPU clock of 16 MHz (ns) -#define START_PULSE_WIDTH (4340) //! width of start pulse (ns) -#define BIT_DELAY (4) //! delay macro for width of one pulse (start pulse or zero pulse, in ns) -#define RX_TX_DELAY (15) //! turn around time when switching from receive to transmit -#define START_PULSE_TIME_OUT (255) //! This value is decremented while waiting for the falling edge of a start pulse. -#define ZERO_PULSE_TIME_OUT (26) //! This value is decremented while waiting for the falling edge of a zero pulse. - -/* swi_phys.h */ - -#define SWI_FUNCTION_RETCODE_SUCCESS ((uint8_t) 0x00) //!< Communication with device succeeded. -#define SWI_FUNCTION_RETCODE_TIMEOUT ((uint8_t) 0xF1) //!< Communication timed out. -#define SWI_FUNCTION_RETCODE_RX_FAIL ((uint8_t) 0xF9) //!< Communication failed after at least one byte was received. - -/* sha204_physical.h */ - -#define SHA204_RSP_SIZE_MIN ((uint8_t) 4) //!< minimum number of bytes in response -#define SHA204_RSP_SIZE_MAX ((uint8_t) 35) //!< maximum size of response packet -#define SHA204_BUFFER_POS_COUNT (0) //!< buffer index of count byte in command or response -#define SHA204_BUFFER_POS_DATA (1) //!< buffer index of data in response -#define SHA204_WAKEUP_PULSE_WIDTH (uint8_t) (6.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5) //! width of Wakeup pulse in 10 us units -#define SHA204_WAKEUP_DELAY (uint8_t) (3.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5) //! delay between Wakeup pulse and communication in ms - -/* sha204_swi.c */ -#define SHA204_SWI_FLAG_CMD ((uint8_t) 0x77) //!< flag preceding a command -#define SHA204_SWI_FLAG_TX ((uint8_t) 0x88) //!< flag requesting a response -#define SHA204_SWI_FLAG_IDLE ((uint8_t) 0xBB) //!< flag requesting to go into Idle mode -#define SHA204_SWI_FLAG_SLEEP ((uint8_t) 0xCC) //!< flag requesting to go into Sleep mode - -/* sha204_comm_marshaling.h */ -// command op-code definitions -#define SHA204_CHECKMAC ((uint8_t) 0x28) //!< CheckMac command op-code -#define SHA204_DERIVE_KEY ((uint8_t) 0x1C) //!< DeriveKey command op-code -#define SHA204_DEVREV ((uint8_t) 0x30) //!< DevRev command op-code -#define SHA204_GENDIG ((uint8_t) 0x15) //!< GenDig command op-code -#define SHA204_HMAC ((uint8_t) 0x11) //!< HMAC command op-code -#define SHA204_LOCK ((uint8_t) 0x17) //!< Lock command op-code -#define SHA204_MAC ((uint8_t) 0x08) //!< MAC command op-code -#define SHA204_NONCE ((uint8_t) 0x16) //!< Nonce command op-code -#define SHA204_PAUSE ((uint8_t) 0x01) //!< Pause command op-code -#define SHA204_RANDOM ((uint8_t) 0x1B) //!< Random command op-code -#define SHA204_READ ((uint8_t) 0x02) //!< Read command op-code -#define SHA204_UPDATE_EXTRA ((uint8_t) 0x20) //!< UpdateExtra command op-code -#define SHA204_WRITE ((uint8_t) 0x12) //!< Write command op-code - -// packet size definitions -#define SHA204_RSP_SIZE_VAL ((uint8_t) 7) //!< size of response packet containing four bytes of data - -// parameter range definitions -#define SHA204_KEY_ID_MAX ((uint8_t) 15) //!< maximum value for key id -#define SHA204_OTP_BLOCK_MAX ((uint8_t) 1) //!< maximum value for OTP block - -// definitions for command packet indexes common to all commands -#define SHA204_COUNT_IDX ( 0) //!< command packet index for count -#define SHA204_OPCODE_IDX ( 1) //!< command packet index for op-code -#define SHA204_PARAM1_IDX ( 2) //!< command packet index for first parameter -#define SHA204_PARAM2_IDX ( 3) //!< command packet index for second parameter -#define SHA204_DATA_IDX ( 5) //!< command packet index for second parameter - -// zone definitions -#define SHA204_ZONE_CONFIG ((uint8_t) 0x00) //!< Configuration zone -#define SHA204_ZONE_OTP ((uint8_t) 0x01) //!< OTP (One Time Programming) zone -#define SHA204_ZONE_DATA ((uint8_t) 0x02) //!< Data zone -#define SHA204_ZONE_MASK ((uint8_t) 0x03) //!< Zone mask -#define SHA204_ZONE_COUNT_FLAG ((uint8_t) 0x80) //!< Zone bit 7 set: Access 32 bytes, otherwise 4 bytes. -#define SHA204_ZONE_ACCESS_4 ((uint8_t) 4) //!< Read or write 4 bytes. -#define SHA204_ZONE_ACCESS_32 ((uint8_t) 32) //!< Read or write 32 bytes. -#define SHA204_ADDRESS_MASK_CONFIG ( 0x001F) //!< Address bits 5 to 7 are 0 for Configuration zone. -#define SHA204_ADDRESS_MASK_OTP ( 0x000F) //!< Address bits 4 to 7 are 0 for OTP zone. -#define SHA204_ADDRESS_MASK ( 0x007F) //!< Address bit 7 to 15 are always 0. - -// CheckMAC command definitions -#define CHECKMAC_MODE_IDX SHA204_PARAM1_IDX //!< CheckMAC command index for mode -#define CHECKMAC_KEYID_IDX SHA204_PARAM2_IDX //!< CheckMAC command index for key identifier -#define CHECKMAC_CLIENT_CHALLENGE_IDX SHA204_DATA_IDX //!< CheckMAC command index for client challenge -#define CHECKMAC_CLIENT_RESPONSE_IDX (37) //!< CheckMAC command index for client response -#define CHECKMAC_DATA_IDX (69) //!< CheckMAC command index for other data -#define CHECKMAC_COUNT (84) //!< CheckMAC command packet size -#define CHECKMAC_MODE_MASK ((uint8_t) 0x27) //!< CheckMAC mode bits 3, 4, 6, and 7 are 0. -#define CHECKMAC_CLIENT_CHALLENGE_SIZE (32) //!< CheckMAC size of client challenge -#define CHECKMAC_CLIENT_RESPONSE_SIZE (32) //!< CheckMAC size of client response -#define CHECKMAC_OTHER_DATA_SIZE (13) //!< CheckMAC size of "other data" - -// DeriveKey command definitions -#define DERIVE_KEY_RANDOM_IDX SHA204_PARAM1_IDX //!< DeriveKey command index for random bit -#define DERIVE_KEY_TARGETKEY_IDX SHA204_PARAM2_IDX //!< DeriveKey command index for target slot -#define DERIVE_KEY_MAC_IDX SHA204_DATA_IDX //!< DeriveKey command index for optional MAC -#define DERIVE_KEY_COUNT_SMALL SHA204_CMD_SIZE_MIN //!< DeriveKey command packet size without MAC -#define DERIVE_KEY_COUNT_LARGE (39) //!< DeriveKey command packet size with MAC -#define DERIVE_KEY_RANDOM_FLAG ((uint8_t) 4) //!< DeriveKey 1. parameter -#define DERIVE_KEY_MAC_SIZE (32) //!< DeriveKey MAC size - -// DevRev command definitions -#define DEVREV_PARAM1_IDX SHA204_PARAM1_IDX //!< DevRev command index for 1. parameter (ignored) -#define DEVREV_PARAM2_IDX SHA204_PARAM2_IDX //!< DevRev command index for 2. parameter (ignored) -#define DEVREV_COUNT SHA204_CMD_SIZE_MIN //!< DevRev command packet size - -// GenDig command definitions -#define GENDIG_ZONE_IDX SHA204_PARAM1_IDX //!< GenDig command index for zone -#define GENDIG_KEYID_IDX SHA204_PARAM2_IDX //!< GenDig command index for key id -#define GENDIG_DATA_IDX SHA204_DATA_IDX //!< GenDig command index for optional data -#define GENDIG_COUNT SHA204_CMD_SIZE_MIN //!< GenDig command packet size without "other data" -#define GENDIG_COUNT_DATA (11) //!< GenDig command packet size with "other data" -#define GENDIG_OTHER_DATA_SIZE (4) //!< GenDig size of "other data" -#define GENDIG_ZONE_CONFIG ((uint8_t) 0) //!< GenDig zone id config -#define GENDIG_ZONE_OTP ((uint8_t) 1) //!< GenDig zone id OTP -#define GENDIG_ZONE_DATA ((uint8_t) 2) //!< GenDig zone id data - -// HMAC command definitions -#define HMAC_MODE_IDX SHA204_PARAM1_IDX //!< HMAC command index for mode -#define HMAC_KEYID_IDX SHA204_PARAM2_IDX //!< HMAC command index for key id -#define HMAC_COUNT SHA204_CMD_SIZE_MIN //!< HMAC command packet size -#define HMAC_MODE_MASK ((uint8_t) 0x74) //!< HMAC mode bits 0, 1, 3, and 7 are 0. - -// Lock command definitions -#define LOCK_ZONE_IDX SHA204_PARAM1_IDX //!< Lock command index for zone -#define LOCK_SUMMARY_IDX SHA204_PARAM2_IDX //!< Lock command index for summary -#define LOCK_COUNT SHA204_CMD_SIZE_MIN //!< Lock command packet size -#define LOCK_ZONE_NO_CONFIG ((uint8_t) 0x01) //!< Lock zone is OTP or Data -#define LOCK_ZONE_NO_CRC ((uint8_t) 0x80) //!< Lock command: Ignore summary. -#define LOCK_ZONE_MASK (0x81) //!< Lock parameter 1 bits 2 to 6 are 0. - -// Mac command definitions -#define MAC_MODE_IDX SHA204_PARAM1_IDX //!< MAC command index for mode -#define MAC_KEYID_IDX SHA204_PARAM2_IDX //!< MAC command index for key id -#define MAC_CHALLENGE_IDX SHA204_DATA_IDX //!< MAC command index for optional challenge -#define MAC_COUNT_SHORT SHA204_CMD_SIZE_MIN //!< MAC command packet size without challenge -#define MAC_COUNT_LONG (39) //!< MAC command packet size with challenge -#define MAC_MODE_BLOCK2_TEMPKEY ((uint8_t) 0x01) //!< MAC mode bit 0: second SHA block from TempKey -#define MAC_MODE_BLOCK1_TEMPKEY ((uint8_t) 0x02) //!< MAC mode bit 1: first SHA block from TempKey -#define MAC_MODE_SOURCE_FLAG_MATCH ((uint8_t) 0x04) //!< MAC mode bit 2: match TempKey.SourceFlag -#define MAC_MODE_PASSTHROUGH ((uint8_t) 0x07) //!< MAC mode bit 0-2: pass-through mode -#define MAC_MODE_INCLUDE_OTP_88 ((uint8_t) 0x10) //!< MAC mode bit 4: include first 88 OTP bits -#define MAC_MODE_INCLUDE_OTP_64 ((uint8_t) 0x20) //!< MAC mode bit 5: include first 64 OTP bits -#define MAC_MODE_INCLUDE_SN ((uint8_t) 0x40) //!< MAC mode bit 6: include serial number -#define MAC_CHALLENGE_SIZE (32) //!< MAC size of challenge -#define MAC_MODE_MASK ((uint8_t) 0x77) //!< MAC mode bits 3 and 7 are 0. - -// Nonce command definitions -#define NONCE_MODE_IDX SHA204_PARAM1_IDX //!< Nonce command index for mode -#define NONCE_PARAM2_IDX SHA204_PARAM2_IDX //!< Nonce command index for 2. parameter -#define NONCE_INPUT_IDX SHA204_DATA_IDX //!< Nonce command index for input data -#define NONCE_COUNT_SHORT (27) //!< Nonce command packet size for 20 bytes of data -#define NONCE_COUNT_LONG (39) //!< Nonce command packet size for 32 bytes of data -#define NONCE_MODE_MASK ((uint8_t) 3) //!< Nonce mode bits 2 to 7 are 0. -#define NONCE_MODE_SEED_UPDATE ((uint8_t) 0x00) //!< Nonce mode: update seed -#define NONCE_MODE_NO_SEED_UPDATE ((uint8_t) 0x01) //!< Nonce mode: do not update seed -#define NONCE_MODE_INVALID ((uint8_t) 0x02) //!< Nonce mode 2 is invalid. -#define NONCE_MODE_PASSTHROUGH ((uint8_t) 0x03) //!< Nonce mode: pass-through -#define NONCE_NUMIN_SIZE (20) //!< Nonce data length -#define NONCE_NUMIN_SIZE_PASSTHROUGH (32) //!< Nonce data length in pass-through mode (mode = 3) - -// Pause command definitions -#define PAUSE_SELECT_IDX SHA204_PARAM1_IDX //!< Pause command index for Selector -#define PAUSE_PARAM2_IDX SHA204_PARAM2_IDX //!< Pause command index for 2. parameter -#define PAUSE_COUNT SHA204_CMD_SIZE_MIN //!< Pause command packet size - -// Random command definitions -#define RANDOM_MODE_IDX SHA204_PARAM1_IDX //!< Random command index for mode -#define RANDOM_PARAM2_IDX SHA204_PARAM2_IDX //!< Random command index for 2. parameter -#define RANDOM_COUNT SHA204_CMD_SIZE_MIN //!< Random command packet size -#define RANDOM_SEED_UPDATE ((uint8_t) 0x00) //!< Random mode for automatic seed update -#define RANDOM_NO_SEED_UPDATE ((uint8_t) 0x01) //!< Random mode for no seed update - -// Read command definitions -#define READ_ZONE_IDX SHA204_PARAM1_IDX //!< Read command index for zone -#define READ_ADDR_IDX SHA204_PARAM2_IDX //!< Read command index for address -#define READ_COUNT SHA204_CMD_SIZE_MIN //!< Read command packet size -#define READ_ZONE_MASK ((uint8_t) 0x83) //!< Read zone bits 2 to 6 are 0. -#define READ_ZONE_MODE_32_BYTES ((uint8_t) 0x80) //!< Read mode: 32 bytes - -// UpdateExtra command definitions -#define UPDATE_MODE_IDX SHA204_PARAM1_IDX //!< UpdateExtra command index for mode -#define UPDATE_VALUE_IDX SHA204_PARAM2_IDX //!< UpdateExtra command index for new value -#define UPDATE_COUNT SHA204_CMD_SIZE_MIN //!< UpdateExtra command packet size -#define UPDATE_CONFIG_BYTE_86 ((uint8_t) 0x01) //!< UpdateExtra mode: update Config byte 86 - -// Write command definitions -#define WRITE_ZONE_IDX SHA204_PARAM1_IDX //!< Write command index for zone -#define WRITE_ADDR_IDX SHA204_PARAM2_IDX //!< Write command index for address -#define WRITE_VALUE_IDX SHA204_DATA_IDX //!< Write command index for data -#define WRITE_MAC_VS_IDX ( 9) //!< Write command index for MAC following short data -#define WRITE_MAC_VL_IDX (37) //!< Write command index for MAC following long data -#define WRITE_COUNT_SHORT (11) //!< Write command packet size with short data and no MAC -#define WRITE_COUNT_LONG (39) //!< Write command packet size with long data and no MAC -#define WRITE_COUNT_SHORT_MAC (43) //!< Write command packet size with short data and MAC -#define WRITE_COUNT_LONG_MAC (71) //!< Write command packet size with long data and MAC -#define WRITE_MAC_SIZE (32) //!< Write MAC size -#define WRITE_ZONE_MASK ((uint8_t) 0xC3) //!< Write zone bits 2 to 5 are 0. -#define WRITE_ZONE_WITH_MAC ((uint8_t) 0x40) //!< Write zone bit 6: write encrypted with MAC - -// Response size definitions -#define CHECKMAC_RSP_SIZE SHA204_RSP_SIZE_MIN //!< response size of DeriveKey command -#define DERIVE_KEY_RSP_SIZE SHA204_RSP_SIZE_MIN //!< response size of DeriveKey command -#define DEVREV_RSP_SIZE SHA204_RSP_SIZE_VAL //!< response size of DevRev command returns 4 bytes -#define GENDIG_RSP_SIZE SHA204_RSP_SIZE_MIN //!< response size of GenDig command -#define HMAC_RSP_SIZE SHA204_RSP_SIZE_MAX //!< response size of HMAC command -#define LOCK_RSP_SIZE SHA204_RSP_SIZE_MIN //!< response size of Lock command -#define MAC_RSP_SIZE SHA204_RSP_SIZE_MAX //!< response size of MAC command -#define NONCE_RSP_SIZE_SHORT SHA204_RSP_SIZE_MIN //!< response size of Nonce command with mode[0:1] = 3 -#define NONCE_RSP_SIZE_LONG SHA204_RSP_SIZE_MAX //!< response size of Nonce command -#define PAUSE_RSP_SIZE SHA204_RSP_SIZE_MIN //!< response size of Pause command -#define RANDOM_RSP_SIZE SHA204_RSP_SIZE_MAX //!< response size of Random command -#define READ_4_RSP_SIZE SHA204_RSP_SIZE_VAL //!< response size of Read command when reading 4 bytes -#define READ_32_RSP_SIZE SHA204_RSP_SIZE_MAX //!< response size of Read command when reading 32 bytes -#define TEMP_SENSE_RSP_SIZE SHA204_RSP_SIZE_VAL //!< response size of TempSense command returns 4 bytes -#define UPDATE_RSP_SIZE SHA204_RSP_SIZE_MIN //!< response size of UpdateExtra command -#define WRITE_RSP_SIZE SHA204_RSP_SIZE_MIN //!< response size of Write command - -// command timing definitions for minimum execution times (ms) -#define CHECKMAC_DELAY ((uint8_t) (12.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5)) -#define DERIVE_KEY_DELAY ((uint8_t) (14.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5)) -#define DEVREV_DELAY ((uint8_t) ( 0.4 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5)) -#define GENDIG_DELAY ((uint8_t) (11.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5)) -#define HMAC_DELAY ((uint8_t) (27.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5)) -#define LOCK_DELAY ((uint8_t) ( 5.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5)) -#define MAC_DELAY ((uint8_t) (12.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5)) -#define NONCE_DELAY ((uint8_t) (22.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5)) -#define PAUSE_DELAY ((uint8_t) ( 0.4 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5)) -#define RANDOM_DELAY ((uint8_t) (11.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5)) -#define READ_DELAY ((uint8_t) ( 0.4 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5)) -#define TEMP_SENSE_DELAY ((uint8_t) ( 4.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5)) -#define UPDATE_DELAY ((uint8_t) ( 4.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5)) -#define WRITE_DELAY ((uint8_t) ( 4.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5)) - -// command timing definitions for maximum execution times (ms) -#define CHECKMAC_EXEC_MAX ((uint8_t) (38.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5)) -#define DERIVE_KEY_EXEC_MAX ((uint8_t) (62.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5)) -#define DEVREV_EXEC_MAX ((uint8_t) ( 2.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5)) -#define GENDIG_EXEC_MAX ((uint8_t) (43.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5)) -#define HMAC_EXEC_MAX ((uint8_t) (69.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5)) -#define LOCK_EXEC_MAX ((uint8_t) (24.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5)) -#define MAC_EXEC_MAX ((uint8_t) (35.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5)) -#define NONCE_EXEC_MAX ((uint8_t) (60.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5)) -#define PAUSE_EXEC_MAX ((uint8_t) ( 2.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5)) -#define RANDOM_EXEC_MAX ((uint8_t) (50.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5)) -#define READ_EXEC_MAX ((uint8_t) ( 4.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5)) -#define TEMP_SENSE_EXEC_MAX ((uint8_t) (11.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5)) -#define UPDATE_EXEC_MAX ((uint8_t) ( 6.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5)) -#define WRITE_EXEC_MAX ((uint8_t) (42.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5)) - -/* from sha204_config.h */ - -#define CPU_CLOCK_DEVIATION_POSITIVE (1.01) -#define CPU_CLOCK_DEVIATION_NEGATIVE (0.99) -#define SHA204_RETRY_COUNT (1) -#define SWI_RECEIVE_TIME_OUT ((uint16_t) 163) //! #START_PULSE_TIME_OUT in us instead of loop counts -#define SWI_US_PER_BYTE ((uint16_t) 313) //! It takes 312.5 us to send a byte (9 single-wire bits / 230400 Baud * 8 flag bits). -#define SHA204_SYNC_TIMEOUT ((uint8_t) 85)//! delay before sending a transmit flag in the synchronization routine -#define SHA204_RESPONSE_TIMEOUT ((uint16_t) SWI_RECEIVE_TIME_OUT + SWI_US_PER_BYTE) //! SWI response timeout is the sum of receive timeout and the time it takes to send the TX flag. - -/* from sha204_comm.h */ - -#define SHA204_COMMAND_EXEC_MAX ((uint8_t) (69.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5)) //! maximum command delay -#define SHA204_CMD_SIZE_MIN ((uint8_t) 7) //! minimum number of bytes in command (from count byte to second CRC byte) -#ifndef SHA204_CMD_SIZE_MAX -#define SHA204_CMD_SIZE_MAX ((uint8_t) 84) //! maximum size of command packet (CheckMac) -#endif -#define SHA204_CRC_SIZE ((uint8_t) 2) //! number of CRC bytes -#define SHA204_BUFFER_POS_STATUS (1) //! buffer index of status byte in status response -#define SHA204_BUFFER_POS_DATA (1) //! buffer index of first data byte in data response -#define SHA204_STATUS_BYTE_WAKEUP ((uint8_t) 0x11) //! command parse error -#define SHA204_STATUS_BYTE_PARSE ((uint8_t) 0x03) //! command parse error -#define SHA204_STATUS_BYTE_EXEC ((uint8_t) 0x0F) //! command execution error -#define SHA204_STATUS_BYTE_COMM ((uint8_t) 0xFF) //! communication error - -/* EEPROM Addresses */ -/* Configuration Zone */ -#define ADDRESS_SN03 0 // SN[0:3] are bytes 0->3 of configuration zone -#define ADDRESS_RevNum 4 // bytes 4->7 of config zone are RevNum -#define ADDRESS_SN47 8 // SN[4:7] are bytes 8->11 of config zone -#define ADDRESS_SN8 12 // SN[8] is byte 12 of config zone, should be 0xEE -#define ADDRESS_I2CEN 14 // I2C Enable, bit 0 represents I2C enable status -#define ADDRESS_I2CADD 16 // Defines I2C address of SHA204 -#define ADDRESS_OTPMODE 18 // Sets the One-time-programmable mode -#define ADDRESS_SELECTOR 19 // Controls writability of Selector - -/* Low level HW access macros */ -/* function calls is not working, as it will have too much overhead */ -#if !defined(ARDUINO_ARCH_AVR) // For everything else than AVR use pinMode / digitalWrite -#define SHA204_SET_OUTPUT() pinMode(device_pin, OUTPUT) -#define SHA204_SET_INPUT() pinMode(device_pin, INPUT) -#define SHA204_POUT_HIGH() digitalWrite(device_pin, HIGH) -#define SHA204_POUT_LOW() digitalWrite(device_pin, LOW) -#define SHA204_PIN_READ() digitalRead(device_pin) -#else -#define SHA204_SET_INPUT() *device_port_DDR &= ~device_pin -#define SHA204_SET_OUTPUT() *device_port_DDR |= device_pin -#define SHA204_POUT_HIGH() *device_port_OUT |= device_pin -#define SHA204_POUT_LOW() *device_port_OUT &= ~device_pin -#define SHA204_PIN_READ() (*device_port_IN & device_pin) -#endif - -class atsha204Class -{ -private: - uint8_t device_pin; - #ifdef ARDUINO_ARCH_AVR - volatile uint8_t *device_port_DDR, *device_port_OUT, *device_port_IN; - #endif - void sha204c_calculate_crc(uint8_t length, uint8_t *data, uint8_t *crc); - uint8_t sha204c_check_crc(uint8_t *response); - void swi_set_signal_pin(uint8_t is_high); - uint8_t swi_receive_bytes(uint8_t count, uint8_t *buffer); - uint8_t swi_send_bytes(uint8_t count, uint8_t *buffer); - uint8_t swi_send_byte(uint8_t value); - uint8_t sha204p_receive_response(uint8_t size, uint8_t *response); - uint8_t sha204p_wakeup(); - uint8_t sha204p_send_command(uint8_t count, uint8_t * command); - uint8_t sha204p_sleep(); - uint8_t sha204p_resync(uint8_t size, uint8_t *response); - - -public: - atsha204Class(uint8_t pin); // Constructor - uint8_t sha204c_wakeup(uint8_t *response); - uint8_t sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer, uint8_t execution_delay, uint8_t execution_timeout); - uint8_t sha204c_resync(uint8_t size, uint8_t *response); - uint8_t sha204m_random(uint8_t * tx_buffer, uint8_t * rx_buffer, uint8_t mode); - uint8_t sha204m_dev_rev(uint8_t *tx_buffer, uint8_t *rx_buffer); - uint8_t sha204m_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address); - uint8_t sha204m_execute(uint8_t op_code, uint8_t param1, uint16_t param2, - uint8_t datalen1, uint8_t *data1, uint8_t datalen2, uint8_t *data2, uint8_t datalen3, uint8_t *data3, - uint8_t tx_size, uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer); - uint8_t sha204m_check_parameters(uint8_t op_code, uint8_t param1, uint16_t param2, - uint8_t datalen1, uint8_t *data1, uint8_t datalen2, uint8_t *data2, uint8_t datalen3, uint8_t *data3, - uint8_t tx_size, uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer); - - uint8_t getSerialNumber(uint8_t *response); - uint16_t calculateAndUpdateCrc(uint8_t length, uint8_t *data, uint16_t current_crc); - -}; - -#endif