diff --git a/examples/bininps/bininps.ino b/examples/bininps/bininps.ino new file mode 100644 index 0000000..87c7306 --- /dev/null +++ b/examples/bininps/bininps.ino @@ -0,0 +1,323 @@ +/* + * bininps + * + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 01/18/2012 + * + * Device: + * Binary input + counter module + * + * Description: + * Device that reports the binary state of 12 digital inputs, 4 of them being + * used as pulse counters as well. + * + * PANSTAMP AVR + * Binary inputs only: pins 2, 3, 4, 5, 6, 8, 9 and 10 + * Binary/counter inputs: pins 18, 20, 21 and 22 + * + * PANSTAMP NRG + * Binary inputs only: pins 23, 22, 11, 10, 9, 8, 6 and 5 + * Binary/counter inputs: pins 21, 20, 19 and 18 + * + * This sketch can be used to detect key/switch presses, binary alarms or + * any other binary sensor. It can also be used to count pulses from a vaste + * variety of devices such as energy meters, water meters, gas meters, etc. + * You may want to add some delays in updateValues in order to better debounce + * the binary states. We suggest to add external capacitors to the inputs as well. + * Capacitor values and delays should depend on the nature and frequence of the + * input signals. + * + * This device is low-power enabled so it will enter low-power mode just + * after reading the binary states and transmitting them over the SWAP + * network. + * + * Associated Device Definition File, defining registers, endpoints and + * configuration parameters: + * bininps.xml (Binary/Counter input module) + */ + +#include "swap.h" +#include "regtable.h" + +/** + * Macros + */ +#ifdef PANSTAMP_NRG +#define pcEnableInterrupt() P1IE |= PCINTMASK1; P2IE |= PCINTMASK2; // Enable interrupt +#define pcDisableInterrupt() P1IE = 0; P2IE = 0; // Disable interrupt +#else // PANSTAMP_AVR +#define pcEnableInterrupt() PCICR = 0x07 // Enable Pin Change interrupts on all ports +#define pcDisableInterrupt() PCICR = 0x00 // Disable Pin Change interrupts +#endif + +/** + * Pin Change Interrupt flag + */ +volatile boolean pcIRQ = false; + +/** + * Binary states + */ +byte stateLowByte = 0, stateHighByte = 0; + +/** + * Initial values + */ +int lastStateBinary[] = {-1, -1, -1, -1, -1, -1, -1, -1}; // Initial pin states +unsigned long counter[] = {0, 0, 0, 0}; // Initial counter values +int lastStateCount[] = {-1, -1, -1, -1}; // Initial pin states + +#ifdef PANSTAMP_NRG +// Interrupt masks +#define PCINTMASK1 0x03 // P1[0:5] +#define PCINTMASK2 0xDE // P2[1,2,3,4,6,7] +// Pure Binary inputs +uint8_t binaryPin[] = {0, 1, 1, 2, 3, 4, 6, 7}; // Binary pins (MSP430 port bits) +const volatile uint8_t *binaryPort[] = {&P1IN, &P1IN, &P2IN, &P2IN, &P2IN, &P2IN, &P2IN, &P2IN}; // Binary ports (MSP430 port) +// Counters +uint8_t counterPin[] = {2, 3, 4, 5}; // Counter pins (MSP430 port bits) +const volatile uint8_t *counterPort[] = {&P1IN, &P1IN, &P1IN, &P1IN}; // Counter ports (MSP430 port) + +/** + * Pin Change Interrupt vectors + */ +__attribute__((interrupt(PORT1_VECTOR))) +void port1Change(void) +{ + P1IES &= ~PCINTMASK1; + panstamp.wakeUp(); + pcIRQ = true; +} +__attribute__((interrupt(PORT2_VECTOR))) +void port2Change(void) +{ + P2IES &= ~PCINTMASK2; + panstamp.wakeUp(); + pcIRQ = true; +} + +#else // PANSTAMP_AVR +// Interrupt masks +#define PCINTMASK0 0x03 // PB[0:1] +#define PCINTMASK1 0x3F // PC[0:5] +#define PCINTMASK2 0xE8 // PD[3], PD[5:7]. These are counter inputs too +// Pure Binary inputs +uint8_t binaryPin[] = {0, 1, 0, 1, 2, 3, 4, 5}; // Binary pins (Atmega port bits) +const volatile uint8_t *binaryPort[] = {&PINB, &PINB, &PINC, &PINC, &PINC, &PINC, &PINC, &PINC}; // Binary ports (Atmega port) +// Counters +uint8_t counterPin[] = {3, 5, 6, 7}; // Counter pins (Atmega port bits) +const volatile uint8_t *counterPort[] = {&PIND, &PIND, &PIND, &PIND}; // Counter ports (Atmega port) + +/** + * Pin Change Interrupt vectors + */ +SIGNAL(PCINT0_vect) +{ + panstamp.wakeUp(); + pcIRQ = true; +} +SIGNAL(PCINT1_vect) +{ + panstamp.wakeUp(); + pcIRQ = true; +} +SIGNAL(PCINT2_vect) +{ + panstamp.wakeUp(); + pcIRQ = true; +} + +#endif + +/** + * configPorts + * + * Configure binary ports to generate interrupts + */ +void configPorts(void) +{ + #ifdef PANSTAMP_NRG + + P1DIR &= ~PCINTMASK1; + P2DIR &= ~PCINTMASK2; + P1IE |= PCINTMASK1; // Enable interrupt + P2IE |= PCINTMASK2; // Enable interrupt + P1IES |= PCINTMASK1; // Hi/Lo edge + P2IES |= PCINTMASK2; // Hi/Lo edge + P1IFG &= ~PCINTMASK1; // IFG cleared + P2IFG &= ~PCINTMASK2; // IFG cleared + + #else // PANSTAMP_AVR + + // Set pins as inputs + DDRB &= ~PCINTMASK0; + DDRC &= ~PCINTMASK1; + DDRD &= ~PCINTMASK2; + + // Set PC interrupt masks + PCMSK0 = PCINTMASK0; + PCMSK1 = PCINTMASK1; + PCMSK2 = PCINTMASK2; + + #endif +} + +/** + * updateValues + * + * Update binary state registers and counters + * + * Return: + * 0 -> No binary state change + * 1 -> Only binary states changed + * 2 -> Binary states and counters changed + */ +byte updateValues(void) +{ + byte i, res = 0; + int state; + + stateLowByte = 0; + for(i=0 ; igetData(); + + // Enter SYNC state + swap.enterSystemState(SYSTATE_SYNC); + + // During 3 seconds, listen the network for possible commands whilst the LED blinks + for(i=0 ; i<6 ; i++) + { + digitalWrite(LED, HIGH); + delay(100); + digitalWrite(LED, LOW); + delay(400); + } + // Transmit periodic Tx interval + swap.getRegister(REGI_TXINTERVAL)->getData(); + // Transmit power voltage + swap.getRegister(REGI_VOLTSUPPLY)->getData(); + + updateValues(); + // Transmit initial binary states + swap.getRegister(REGI_BININPUTS)->getData(); + // Transmit initial counter values + swap.getRegister(REGI_COUNTERS)->getData(); + + // Switch to Rx OFF state + swap.enterSystemState(SYSTATE_RXOFF); + + // Enable Pin Change Interrupts + pcEnableInterrupt(); +} + +/** + * loop + * + * Arduino main loop + */ +void loop() +{ + // Sleep for panstamp.txInterval seconds (register 10) + swap.goToSleep(); + + pcDisableInterrupt(); + + if (pcIRQ) + { + switch(updateValues()) + { + case 2: + // Transmit counter values + swap.getRegister(REGI_COUNTERS)->getData(); + case 1: + // Transmit binary states + swap.getRegister(REGI_BININPUTS)->getData(); + break; + default: + break; + } + //Ready to receive new PC interrupts + pcIRQ = false; + } + else + { + // Just send states and counter values periodically, according to the value + // of panstamp.txInterval (register 10) + swap.getRegister(REGI_COUNTERS)->getData(); + swap.getRegister(REGI_BININPUTS)->getData(); + } + + pcEnableInterrupt(); +} + diff --git a/examples/bininps/product.h b/examples/bininps/product.h new file mode 100644 index 0000000..5082258 --- /dev/null +++ b/examples/bininps/product.h @@ -0,0 +1,51 @@ +/** + * product.h + * + * Copyright (c) 2012 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 01/19/2012 + */ + +#ifndef _PRODUCT_H +#define _PRODUCT_H + +/** + * Hardware version + */ +#define HARDWARE_VERSION 0x00000200L + +/** + * Firmware version + */ +#define FIRMWARE_VERSION 0x00000101L + +/** + * Manufacturer SWAP ID + */ +#define SWAP_MANUFACT_ID 0x00000001L + +/** + * Product SWAP ID + */ +#define SWAP_PRODUCT_ID 0x00000006L + +#endif + diff --git a/examples/bininps/regtable.h b/examples/bininps/regtable.h new file mode 100644 index 0000000..28bfab4 --- /dev/null +++ b/examples/bininps/regtable.h @@ -0,0 +1,44 @@ +/** + * regtable.h + * + * Copyright (c) 2012 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 01/19/2012 + */ + +#ifndef _REGTABLE_H +#define _REGTABLE_H + +#include "Arduino.h" +#include "register.h" +#include "commonregs.h" + +/** + * Register indexes + */ +DEFINE_REGINDEX_START() + REGI_VOLTSUPPLY, + REGI_BININPUTS, + REGI_COUNTERS +DEFINE_REGINDEX_END() + +#endif + diff --git a/examples/bininps/regtable.ino b/examples/bininps/regtable.ino new file mode 100644 index 0000000..55ddd32 --- /dev/null +++ b/examples/bininps/regtable.ino @@ -0,0 +1,119 @@ +/** + * regtable + * + * Copyright (c) 2012 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 01/19/2012 + */ + +#include "product.h" +#include "regtable.h" + +/** + * Definition of common registers + */ +DEFINE_COMMON_REGISTERS() + +/* + * Definition of custom registers + */ +// Voltage supply +static byte dtVoltSupply[2]; +REGISTER regVoltSupply(dtVoltSupply, sizeof(dtVoltSupply), &updtVoltSupply, NULL); +// Binary input register +byte dtBinInputs[2]; // Binary input states +REGISTER regBinInputs(dtBinInputs, sizeof(dtBinInputs), &updtBinInputs, NULL); +// 4-byte counter registers (4 regs) +byte dtCounters[16]; // Pulse counters +REGISTER regCounters(dtCounters, sizeof(dtCounters), &updtCounters, NULL); + +/** + * Initialize table of registers + */ +DECLARE_REGISTERS_START() + ®VoltSupply, + ®BinInputs, + ®Counters +DECLARE_REGISTERS_END() + +/** + * Definition of common getter/setter callback functions + */ +DEFINE_COMMON_CALLBACKS() + +/** + * Definition of custom getter/setter callback functions + */ + +/** + * updtVoltSupply + * + * Measure voltage supply and update register + * + * 'rId' Register ID + */ +const void updtVoltSupply(byte rId) +{ + uint16_t result = panstamp.getVcc(); + + /** + * register[eId]->member can be replaced by regVoltSupply in this case since + * no other register is going to use "updtVoltSupply" as "updater" function + */ + + // Update register value + regTable[rId]->value[0] = (result >> 8) & 0xFF; + regTable[rId]->value[1] = result & 0xFF; +} + +/** + * updtBinInputs + * + * Read binary inputs + * + * 'rId' Register ID + */ +const void updtBinInputs(byte rId) +{ + // Update register + dtBinInputs[0] = stateHighByte; + dtBinInputs[1] = stateLowByte; +} + +/** + * updtCounters + * + * Update counters + * + * 'rId' Register ID + */ +const void updtCounters(byte rId) +{ + byte i, j; + + // Update register + for(i=0 ; i<4 ; i++) + { + for(j=0 ; j<4 ; j++) + dtCounters[i*4 + j] = (counter[3-i] >> 8 * (3-j)) & 0xFF; + } +} + diff --git a/examples/bininps2/bininps2.ino b/examples/bininps2/bininps2.ino new file mode 100644 index 0000000..37eff24 --- /dev/null +++ b/examples/bininps2/bininps2.ino @@ -0,0 +1,274 @@ +/* + * bininps2 + * + * Copyright (c) 2015 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 04/01/2015 + * + * Device: + * Binary input + counter module + * + * Description: + * Device that reports the binary state of 12 digital inputs, 4 of them being + * used as pulse counters as well. + * + * PANSTAMP AVR + * This sketch is not supported by panStamp AVR. + * panStamp AVR does not support attachInterrupt on pins other than INT1 (D3). + * RTC calendar mode is not supported by AVR modules either. + * With panStamp AVR use the classic "bininps" sketch or "pulsecounter" + * + * PANSTAMP NRG + * Binary inputs only: D0, D1, D2, D3, D4, D5, D6 and D7 + * Binary/counter inputs: D8, D9, D10 and D11 + * This sketch uses internal pull-up's. Counters increment when inputs turn down + * (LOW state) + * + * This sketch can be used to detect key/switch presses, binary alarms or + * any other binary sensor. It can also be used to count pulses from a vaste + * variety of devices such as energy meters, water meters, gas meters, etc. + * You may want to add some delays in updateValues in order to better debounce + * the binary states. We suggest to add external capacitors to the inputs as well. + * Capacitor values and delays should depend on the nature and frequence of the + * input signals. + * + * This device is low-power enabled so it will enter low-power mode just + * after reading the binary states and transmitting them over the SWAP + * network. + * + * Associated Device Definition File, defining registers, endpoints and + * configuration parameters: + * bininps.xml (Binary/Counter input module) + */ + +#include "swap.h" +#include "regtable.h" + +/** + * Register data containers made global + */ +byte dtBinInputs[2]; // Binary input states +byte dtCounters[16]; // Pulse counters + +// RTC data +RTCDATA rtcData; + +/** + * Input Change Interrupts + */ +// D0 +void d0irq(void) +{ + dtBinInputs[0] = digitalRead(0); // Update register +} +// D1 +void d1irq(void) +{ + dtBinInputs[0] = digitalRead(1) << 1; // Update register +} +// D2 +void d2irq(void) +{ + dtBinInputs[0] = digitalRead(2) << 2; // Update register +} +// D3 +void d3irq(void) +{ + dtBinInputs[0] = digitalRead(3) << 3; // Update register +} +// D4 +void d4irq(void) +{ + dtBinInputs[0] = digitalRead(4) << 4; // Update register +} +// D5 +void d5irq(void) +{ + dtBinInputs[0] = digitalRead(5) << 5; // Update register +} +// D6 +void d6irq(void) +{ + dtBinInputs[0] = digitalRead(6) << 6; // Update register +} +// D7 +void d7irq(void) +{ + dtBinInputs[0] |= digitalRead(7) << 7; // Update register +} +// D8 +void d8irq(void) +{ + dtBinInputs[1] = digitalRead(8); // Update register + if (!(dtBinInputs[1] & 0x01)) + dtCounters[0]++; // Increment counter if pin is low +} +// D9 +void d9irq(void) +{ + dtBinInputs[1] = digitalRead(9) << 1; // Update register + if (!(dtBinInputs[1] & 0x02)) + dtCounters[1]++; // Increment counter if pin is low +} +// D10 +void d10irq(void) +{ + dtBinInputs[1] = digitalRead(10) << 2; // Update register + if (!(dtBinInputs[1] & 0x04)) + dtCounters[2]++; // Increment counter if pin is low +} +// D11 +void d11irq(void) +{ + dtBinInputs[1] = digitalRead(11) << 3; // Update register + if (!(dtBinInputs[1] & 0x08)) + dtCounters[3]++; // Increment counter if pin is low +} + +/** + * configPorts + * + * Configure binary ports to generate interrupts + */ +void configPorts(void) +{ + // Configure digital pins as inputs with internal pull-up's + pinMode(0, INPUT_PULLUP); + pinMode(1, INPUT_PULLUP); + pinMode(2, INPUT_PULLUP); + pinMode(3, INPUT_PULLUP); + pinMode(4, INPUT_PULLUP); + pinMode(5, INPUT_PULLUP); + pinMode(6, INPUT_PULLUP); + pinMode(7, INPUT_PULLUP); + pinMode(8, INPUT_PULLUP); + pinMode(9, INPUT_PULLUP); + pinMode(10, INPUT_PULLUP); + pinMode(11, INPUT_PULLUP); + + // Attach pins to custom IRQ's + attachInterrupt(0, d0irq, CHANGE); + attachInterrupt(1, d1irq, CHANGE); + attachInterrupt(2, d2irq, CHANGE); + attachInterrupt(3, d3irq, CHANGE); + attachInterrupt(4, d4irq, CHANGE); + attachInterrupt(5, d5irq, CHANGE); + attachInterrupt(6, d6irq, CHANGE); + attachInterrupt(7, d7irq, CHANGE); + attachInterrupt(8, d8irq, CHANGE); + attachInterrupt(9, d9irq, CHANGE); + attachInterrupt(10, d10irq, CHANGE); + attachInterrupt(11, d11irq, CHANGE); +} + +/** + * updateValues + * + * Update binary state registers and counters + */ +void updateValues(void) +{ + int i; + + for (i=0 ; i<8 ; i++) + dtBinInputs[0] |= digitalRead(i) << i; + + for (i=0 ; i<4 ; i++) + dtBinInputs[1] |= digitalRead(i+8) << i; +} + +/** + * setup + * + * Arduino setup function + */ +void setup() +{ + int i; + + pinMode(LED, OUTPUT); + digitalWrite(LED, LOW); + + // Configure ports + configPorts(); + + // Init SWAP stack + swap.init(); + + // Transmit product code + swap.getRegister(REGI_PRODUCTCODE)->getData(); + + // Enter SYNC state + swap.enterSystemState(SYSTATE_SYNC); + + // During 3 seconds, listen the network for possible commands whilst the LED blinks + for(i=0 ; i<6 ; i++) + { + digitalWrite(LED, HIGH); + delay(100); + digitalWrite(LED, LOW); + delay(400); + } + // Transmit periodic Tx interval + swap.getRegister(REGI_TXINTERVAL)->getData(); + // Transmit power voltage + swap.getRegister(REGI_VOLTSUPPLY)->getData(); + + updateValues(); + // Transmit initial binary states + swap.getRegister(REGI_BININPUTS)->getData(); + // Transmit initial counter values + swap.getRegister(REGI_COUNTERS)->getData(); + + // Switch to Rx OFF state + swap.enterSystemState(SYSTATE_RXOFF); + + // Set current time : Thursday 8-Apr 2015 12:15:00 (PM) + rtcData.year = 2015; + rtcData.mon = 4; + rtcData.day = 9; + rtcData.wday = 4; + rtcData.hour = 13; + rtcData.min = 48; + rtcData.sec = 0; + + panstamp.rtc.startCalendar(&rtcData); + + //panstamp.rtc.setAlarmHour(15); + panstamp.rtc.setAlarmMinutes(50); +} + +/** + * loop + * + * Arduino main loop + */ +void loop() +{ + // Sleep until next (RTC) calendar alarm or pin interrupt + panstamp.sleep(); + + // Transmit binary states + swap.getRegister(REGI_BININPUTS)->getData(); + // Transmit counter values + swap.getRegister(REGI_COUNTERS)->getData(); +} + diff --git a/examples/bininps2/product.h b/examples/bininps2/product.h new file mode 100644 index 0000000..5003b3a --- /dev/null +++ b/examples/bininps2/product.h @@ -0,0 +1,51 @@ +/** + * product.h + * + * Copyright (c) 2015 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 01/19/2012 + */ + +#ifndef _PRODUCT_H +#define _PRODUCT_H + +/** + * Hardware version + */ +#define HARDWARE_VERSION 0x00000200L + +/** + * Firmware version + */ +#define FIRMWARE_VERSION 0x00000101L + +/** + * Manufacturer SWAP ID + */ +#define SWAP_MANUFACT_ID 0x00000001L + +/** + * Product SWAP ID + */ +#define SWAP_PRODUCT_ID 0x00000006L + +#endif + diff --git a/examples/bininps2/regtable.h b/examples/bininps2/regtable.h new file mode 100644 index 0000000..a1e2219 --- /dev/null +++ b/examples/bininps2/regtable.h @@ -0,0 +1,44 @@ +/** + * regtable.h + * + * Copyright (c) 2015 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 01/19/2012 + */ + +#ifndef _REGTABLE_H +#define _REGTABLE_H + +#include "Arduino.h" +#include "register.h" +#include "commonregs.h" + +/** + * Register indexes + */ +DEFINE_REGINDEX_START() + REGI_VOLTSUPPLY, + REGI_BININPUTS, + REGI_COUNTERS +DEFINE_REGINDEX_END() + +#endif + diff --git a/examples/bininps2/regtable.ino b/examples/bininps2/regtable.ino new file mode 100644 index 0000000..fb088fc --- /dev/null +++ b/examples/bininps2/regtable.ino @@ -0,0 +1,84 @@ +/** + * regtable + * + * Copyright (c) 2015 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 04/01/2015 + */ + +#include "product.h" +#include "regtable.h" + +/** + * Definition of common registers + */ +DEFINE_COMMON_REGISTERS() + +/* + * Definition of custom registers + */ +// Voltage supply +byte dtVoltSupply[2]; +REGISTER regVoltSupply(dtVoltSupply, sizeof(dtVoltSupply), &updtVoltSupply, NULL); +// Binary input register +REGISTER regBinInputs(dtBinInputs, sizeof(dtBinInputs), NULL, NULL); +// 4-byte counter registers (4 regs) +REGISTER regCounters(dtCounters, sizeof(dtCounters), NULL, NULL); + +/** + * Initialize table of registers + */ +DECLARE_REGISTERS_START() + ®VoltSupply, + ®BinInputs, + ®Counters +DECLARE_REGISTERS_END() + +/** + * Definition of common getter/setter callback functions + */ +DEFINE_COMMON_CALLBACKS() + +/** + * Definition of custom getter/setter callback functions + */ + +/** + * updtVoltSupply + * + * Measure voltage supply and update register + * + * 'rId' Register ID + */ +const void updtVoltSupply(byte rId) +{ + uint16_t result = panstamp.getVcc(); + + /** + * register[eId]->member can be replaced by regVoltSupply in this case since + * no other register is going to use "updtVoltSupply" as "updater" function + */ + + // Update register value + regTable[rId]->value[0] = (result >> 8) & 0xFF; + regTable[rId]->value[1] = result & 0xFF; +} + diff --git a/examples/binouts/binouts.ino b/examples/binouts/binouts.ino new file mode 100644 index 0000000..8c7cea1 --- /dev/null +++ b/examples/binouts/binouts.ino @@ -0,0 +1,128 @@ +/* + * binouts + * + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 01/26/2012 + * + * Device: + * Binary output + PWM module + * + * Description: + * Device that provides 8 controllable binary (ON/OFF) outputs and 4 PWM + * outputs + * + * PANSTAMP_AVR and PANSTAMP_NRG + * Binary outputs : pins 2, 4, 5, 6, 8, 9, 10 and 22 + * PWM outputs: pins 3, 18, 20 and 21 + * + * This sketch can be used to control loads (ON/OFF) and even run some type + * of progressive control via PWM (dim lights, control motors, blinds, etc) + * + * Associated Device Definition File, defining registers, endpoints and + * configuration parameters: + * binouts.xml (Binary/PWM output module) + */ + +#include "regtable.h" +#include "swap.h" +#ifdef PANSTAMP_NRG +#include "cc430aes.h" +#endif + +#ifdef PANSTAMP_NRG +// Binary output pins (Arduino digital pins) +uint8_t binaryPin[] = {16, 18, 15, 14, 12, 11, 10, 1}; +// PWM output pins (Arduino digital pins) +uint8_t pwmPin[] = {17, 2, 3, 5}; +#else // PANSTAMP_AVR +// Binary output pins (Arduino digital pins) +uint8_t binaryPin[] = {8, 14, 15, 16, 17, 18, 19, 7}; +// PWM output pins (Arduino digital pins) +uint8_t pwmPin[] = {9, 6, 5, 3}; +#endif + +/** + * swapStatusReceived + * + * Function automatically called by the panStamp API whenever a SWAP status + * packet is received + * + * 'status' SWAP status packet received + */ + void swapStatusReceived(SWPACKET *status) + { + // Place here you code if you want the outputs to change + // according to the value of any external register/endpoint + } + +/** + * setup + * + * Arduino setup function + */ +void setup() +{ + int i; + + pinMode(LED, OUTPUT); + digitalWrite(LED, LOW); + + // Configure output pins + for(i=0 ; igetData(); + // Enter Rx ON state + swap.enterSystemState(SYSTATE_RXON); + // Transmit initial binary states + swap.getRegister(REGI_BINOUTPUTS)->getData(); + // Transmit initial PWM values + swap.getRegister(REGI_PWMOUTPUTS)->getData(); +} + +/** + * loop + * + * Arduino main loop + */ +void loop() +{ + digitalWrite(LED, HIGH); + delay(100); + digitalWrite(LED, LOW); + delay(4900); +} + diff --git a/examples/binouts/product.h b/examples/binouts/product.h new file mode 100644 index 0000000..ab684c3 --- /dev/null +++ b/examples/binouts/product.h @@ -0,0 +1,51 @@ +/** + * product.h + * + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 01/26/2012 + */ + +#ifndef _PRODUCT_H +#define _PRODUCT_H + +/** + * Hardware version + */ +#define HARDWARE_VERSION 0x00000200L + +/** + * Firmware version + */ +#define FIRMWARE_VERSION 0x00000101L + +/** + * Manufacturer SWAP ID + */ +#define SWAP_MANUFACT_ID 0x00000001L + +/** + * Product SWAP ID + */ +#define SWAP_PRODUCT_ID 0x00000007L + +#endif + diff --git a/examples/binouts/regtable.h b/examples/binouts/regtable.h new file mode 100644 index 0000000..671cfb0 --- /dev/null +++ b/examples/binouts/regtable.h @@ -0,0 +1,43 @@ +/** + * regtable.h + * + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 01/26/2012 + */ + +#ifndef _REGTABLE_H +#define _REGTABLE_H + +#include "Arduino.h" +#include "register.h" +#include "commonregs.h" + +/** + * Register indexes + */ +DEFINE_REGINDEX_START() + REGI_BINOUTPUTS, + REGI_PWMOUTPUTS +DEFINE_REGINDEX_END() + +#endif + diff --git a/examples/binouts/regtable.ino b/examples/binouts/regtable.ino new file mode 100644 index 0000000..fe315e3 --- /dev/null +++ b/examples/binouts/regtable.ino @@ -0,0 +1,104 @@ +/** + * regtable + * + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 01/26/2012 + */ + +#include "product.h" +#include "regtable.h" + +/** + * Definition of common registers + */ +DEFINE_COMMON_REGISTERS() + +const void setBinOutputs(byte rId, byte *states); +const void setPwmOutputs(byte rId, byte *levels); + +/* + * Definition of custom registers + */ +// Binary output register +byte dtBinOutputs[1]; // 8 Binary output states +REGISTER regBinOutputs(dtBinOutputs, sizeof(dtBinOutputs), NULL, &setBinOutputs); +// PWM output register +byte dtPwmOutputs[4]; // 4 PWM outputs +REGISTER regPwmOutputs(dtPwmOutputs, sizeof(dtPwmOutputs), NULL, &setPwmOutputs); + +/** + * Initialize table of registers + */ +DECLARE_REGISTERS_START() + ®BinOutputs, + ®PwmOutputs +DECLARE_REGISTERS_END() + +/** + * Definition of common getter/setter callback functions + */ +DEFINE_COMMON_CALLBACKS() + +/** + * Definition of custom getter/setter callback functions + */ + +/** + * setBinOutputs + * + * Set binary outputs + * + * 'rId' Register ID + * 'states' New output levels + */ +const void setBinOutputs(byte rId, byte *states) +{ + byte i; + + // Update register + memcpy(dtBinOutputs, states, sizeof(dtBinOutputs)); + + // Control pins + for(i=0 ; i> i) & 0x01); +} + +/** + * setPwmOutputs + * + * Set PWM levels + * + * 'rId' Register ID + * 'levels' New PWM levels + */ +const void setPwmOutputs(byte rId, byte *levels) +{ + byte i; + + // Update register + memcpy(dtPwmOutputs, levels, sizeof(dtPwmOutputs)); + + // Control PWM outputs + for(i=0 ; i + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 01/26/2012 + * + * Device: + * Binary output + PWM module + * + * Description: + * Device providing 8 controllable binary (ON/OFF) outputs and 4 PWM + * outputs. Each output is controlled by an indewpendent register so there + * is no need to know the state of the outputs to be kept unmodified. + * Optional repeater mode + * + * PANSTAMP_AVR and PANSTAMP_NRG + * Binary outputs : pins 2, 4, 5, 6, 8, 9, 10 and 22 + * PWM outputs: pins 3, 18, 20 and 21 + * + * This sketch can be used to control loads (ON/OFF) and even run some type + * of progressive control via PWM (dim lights, control motors, blinds, etc) + * + * Associated Device Definition File, defining registers, endpoints and + * configuration parameters: + * binouts.xml (Binary/PWM output module) + */ + +#include "regtable.h" +#include "swap.h" + + +/** + * Maximum repeating count + */ +const byte maxRepeaterHop = 5; + +#ifdef PANSTAMP_NRG +// Binary output pins (Arduino digital pins) +uint8_t binaryPin[] = {16, 18, 15, 14, 12, 11, 10, 1}; +// PWM output pins (Arduino digital pins) +uint8_t pwmPin[] = {17, 2, 3, 5}; +#else // PANSTAMP_AVR +// Binary output pins (Arduino digital pins) +uint8_t binaryPin[] = {8, 14, 15, 16, 17, 18, 19, 7}; +// PWM output pins (Arduino digital pins) +uint8_t pwmPin[] = {9, 6, 5, 3}; +#endif + +/** + * setup + * + * Arduino setup function + */ +void setup() +{ + int i; + + pinMode(LED, OUTPUT); + digitalWrite(LED, LOW); + + // Configure output pins + for(i=0 ; igetData(); + + // Enter SYNC state + swap.enterSystemState(SYSTATE_RXON); + // Transmit initial binary states + for(i=0 ; igetData(); + // Transmit initial PWM values + for(i=0 ; igetData(); +} + +/** + * loop + * + * Arduino main loop + */ +void loop() +{ + digitalWrite(LED, HIGH); + delay(100); + digitalWrite(LED, LOW); + delay(4900); +} + diff --git a/examples/binouts2/product.h b/examples/binouts2/product.h new file mode 100644 index 0000000..b8eb6f7 --- /dev/null +++ b/examples/binouts2/product.h @@ -0,0 +1,51 @@ +/** + * product.h + * + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 01/26/2012 + */ + +#ifndef _PRODUCT_H +#define _PRODUCT_H + +/** + * Hardware version + */ +#define HARDWARE_VERSION 0x00000200L + +/** + * Firmware version + */ +#define FIRMWARE_VERSION 0x00000101L + +/** + * Manufacturer SWAP ID + */ +#define SWAP_MANUFACT_ID 0x00000001L + +/** + * Product SWAP ID + */ +#define SWAP_PRODUCT_ID 0x0000000CL + +#endif + diff --git a/examples/binouts2/regtable.h b/examples/binouts2/regtable.h new file mode 100644 index 0000000..8414e1b --- /dev/null +++ b/examples/binouts2/regtable.h @@ -0,0 +1,53 @@ +/** + * regtable.h + * + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 01/26/2012 + */ + +#ifndef _REGTABLE_H +#define _REGTABLE_H + +#include "register.h" +#include "commonregs.h" + +/** + * Register indexes + */ +DEFINE_REGINDEX_START() + REGI_REPEATERCFG, + REGI_BINOUTPUT0, + REGI_BINOUTPUT1, + REGI_BINOUTPUT2, + REGI_BINOUTPUT3, + REGI_BINOUTPUT4, + REGI_BINOUTPUT5, + REGI_BINOUTPUT6, + REGI_BINOUTPUT7, + REGI_PWMOUTPUT0, + REGI_PWMOUTPUT1, + REGI_PWMOUTPUT2, + REGI_PWMOUTPUT3 +DEFINE_REGINDEX_END() + +#endif + diff --git a/examples/binouts2/regtable.ino b/examples/binouts2/regtable.ino new file mode 100644 index 0000000..bdf9b68 --- /dev/null +++ b/examples/binouts2/regtable.ino @@ -0,0 +1,133 @@ +/** + * regtable + * + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 01/26/2012 + */ + +#include "product.h" +#include "regtable.h" + +/** + * Definition of common registers + */ +DEFINE_COMMON_REGISTERS() + +/* + * Definition of custom registers + */ +// Repater config. Not being used by the application. Only kept for backward compatibility +byte dtRepeaterCfg[1]; // Repeater config +REGISTER regRepeaterCfg(dtRepeaterCfg, sizeof(dtRepeaterCfg), NULL, NULL); +// Binary output registers +byte dtBinOutput0[1]; // Binary output state 0 +REGISTER regBinOutput0(dtBinOutput0, sizeof(dtBinOutput0), NULL, &setBinOutput); +byte dtBinOutput1[1]; // Binary output state 1 +REGISTER regBinOutput1(dtBinOutput1, sizeof(dtBinOutput1), NULL, &setBinOutput); +byte dtBinOutput2[1]; // Binary output state 2 +REGISTER regBinOutput2(dtBinOutput2, sizeof(dtBinOutput2), NULL, &setBinOutput); +byte dtBinOutput3[1]; // Binary output state 3 +REGISTER regBinOutput3(dtBinOutput3, sizeof(dtBinOutput3), NULL, &setBinOutput); +byte dtBinOutput4[1]; // Binary output state 4 +REGISTER regBinOutput4(dtBinOutput4, sizeof(dtBinOutput4), NULL, &setBinOutput); +byte dtBinOutput5[1]; // Binary output state 5 +REGISTER regBinOutput5(dtBinOutput5, sizeof(dtBinOutput5), NULL, &setBinOutput); +byte dtBinOutput6[1]; // Binary output state 6 +REGISTER regBinOutput6(dtBinOutput6, sizeof(dtBinOutput6), NULL, &setBinOutput); +byte dtBinOutput7[1]; // Binary output state 7 +REGISTER regBinOutput7(dtBinOutput7, sizeof(dtBinOutput7), NULL, &setBinOutput); +// PWM output registers +byte dtPwmOutput0[1]; // PWM output 0 +REGISTER regPwmOutput0(dtPwmOutput0, sizeof(dtPwmOutput0), NULL, &setPwmOutput); +byte dtPwmOutput1[1]; // PWM output 1 +REGISTER regPwmOutput1(dtPwmOutput1, sizeof(dtPwmOutput1), NULL, &setPwmOutput); +byte dtPwmOutput2[1]; // PWM output 2 +REGISTER regPwmOutput2(dtPwmOutput2, sizeof(dtPwmOutput2), NULL, &setPwmOutput); +byte dtPwmOutput3[1]; // PWM output 3 +REGISTER regPwmOutput3(dtPwmOutput3, sizeof(dtPwmOutput3), NULL, &setPwmOutput); + +/** + * Initialize table of registers + */ +DECLARE_REGISTERS_START() + ®RepeaterCfg, // Not used + ®BinOutput0, + ®BinOutput1, + ®BinOutput2, + ®BinOutput3, + ®BinOutput4, + ®BinOutput5, + ®BinOutput6, + ®BinOutput7, + ®PwmOutput0, + ®PwmOutput1, + ®PwmOutput2, + ®PwmOutput3 +DECLARE_REGISTERS_END() + +/** + * Definition of common getter/setter callback functions + */ +DEFINE_COMMON_CALLBACKS() + +/** + * Definition of custom getter/setter callback functions + */ + +/** + * setBinOutput + * + * Set binary output + * + * 'rId' Register ID + * 'state' New output level + */ +const void setBinOutput(byte rId, byte *state) +{ + byte output = rId - REGI_BINOUTPUT0; + + // Update register + regTable[rId]->value[0] = state[0]; + + // Control pin + digitalWrite(binaryPin[output], state[0]); +} + +/** + * setPwmOutput + * + * Set PWM level + * + * 'rId' Register ID + * 'level' New PWM level + */ +const void setPwmOutput(byte rId, byte *level) +{ + byte output = rId - REGI_PWMOUTPUT0; + + // Update register + regTable[rId]->value[0] = level[0]; + + // Control PWM output + analogWrite(pwmPin[output], level[0]); +} + diff --git a/examples/co2meter/co2meter.ino b/examples/co2meter/co2meter.ino new file mode 100644 index 0000000..9d639cd --- /dev/null +++ b/examples/co2meter/co2meter.ino @@ -0,0 +1,167 @@ +/* + * co2meter + * + * Copyright (c) 2015 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Francisco Barrena + * Creation date: 04/10/2015 + * + * Device: + * CO2 + Temperature + Humidity sensor + * + * Description: + * This application measures: + * - Ambient CO2 level from a 0-2000 ppm COZIR sensor + * (http://www.co2meter.com/products/cozir-0-2-co2-sensor) + * - Temperature and humidity from SI7021 sensor + * + * These devices are low-power enabled so they will enter low-power mode + * just after reading the sensor values and transmitting them over the + * SWAP network. + * + * Associated Device Definition File, defining registers, endpoints and + * configuration parameters: + * co2meter.xml + */ + +#include "regtable.h" +#include "swap.h" +#include "HTU21D.h" +#include "Wire.h" +#include + +#define CO2_ENABLE_PIN 15 + +uint16_t co2Level = 0; // Holds the actual CO2 value + +// Humidity and temperature sensor +HTU21D htu; + +/** + * setup + * + * Arduino setup function + */ +void setup() +{ + Serial.begin(9600); + int i; + + pinMode(CO2_ENABLE_PIN, OUTPUT); + pinMode(LED, OUTPUT); + digitalWrite(LED, LOW); + + // Init SWAP stack + swap.init(); + + // Initialize Hhumidity+temperature sensor + htu.begin(); + + // Transmit product code + swap.getRegister(REGI_PRODUCTCODE)->getData(); + + // Enter SYNC state + swap.enterSystemState(SYSTATE_SYNC); + + // During 3 seconds, listen the network for possible commands whilst the LED blinks + for(i=0 ; i<6 ; i++) + { + digitalWrite(LED, HIGH); + delay(100); + digitalWrite(LED, LOW); + delay(400); + } + + // Transmit periodic Tx interval + swap.getRegister(REGI_TXINTERVAL)->getData(); + // Transmit power voltage + swap.getRegister(REGI_VOLTSUPPLY)->getData(); + // Switch to Rx OFF state + swap.enterSystemState(SYSTATE_RXOFF); +} + +/** + * loop + * + * Arduino main loop + */ +void loop() +{ + // Enable CO2 sensor + digitalWrite(CO2_ENABLE_PIN, HIGH); + + // Transmit power voltage + swap.getRegister(REGI_VOLTSUPPLY)->getData(); + + // Transmit Htu sensor data + swap.getRegister(REGI_HTUSENSOR)->getData(); + + co2Level = readCO2(); + // Transmit CO2 sensor data + swap.getRegister(REGI_CO2SENSOR)->getData(); + + + // disable CO2 sensor + digitalWrite(CO2_ENABLE_PIN, LOW); + + // Sleep + swap.goToSleep(); + } + +/** + * readCO2 + * + * Read CO2 level + * + * @return Current CO2 level + */ +uint16_t readCO2(void) +{ + uint8_t co2Buffer[25]; + uint8_t co2Index = 0; + uint8_t ch; + bool start = false, stop = false; + + while(!stop) + { + if(Serial.available()) + { + ch = Serial.read(); + switch (ch) + { + case 'Z': + start = true; + break; + case 'z': + stop = true; + break; + default: + if (start) + { + if(ch != 0x20) + co2Buffer[co2Index++] = ch; + } + break; + } + } + } + + return (uint16_t) atoi((char*)co2Buffer); +} diff --git a/examples/co2meter/product.h b/examples/co2meter/product.h new file mode 100644 index 0000000..a722100 --- /dev/null +++ b/examples/co2meter/product.h @@ -0,0 +1,51 @@ +/** + * product.h + * + * Copyright (c) 2015 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Francisco Barrena + * Creation date: 04/10/2015 + */ + +#ifndef _PRODUCT_H +#define _PRODUCT_H + +/** + * Hardware version + */ +#define HARDWARE_VERSION 0x00000100L + +/** + * Firmware version + */ +#define FIRMWARE_VERSION 0x00000100L + +/** + * Manufacturer SWAP ID + */ +#define SWAP_MANUFACT_ID 0x00000001L + +/** + * Product SWAP ID + */ +#define SWAP_PRODUCT_ID 0x00000011L + +#endif + diff --git a/examples/co2meter/regtable.h b/examples/co2meter/regtable.h new file mode 100644 index 0000000..469c4fc --- /dev/null +++ b/examples/co2meter/regtable.h @@ -0,0 +1,44 @@ +/** + * regtable.h + * + * Copyright (c) 2015 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Francisco Barrena + * Creation date: 04/10/2015 + */ + +#ifndef _REGTABLE_H +#define _REGTABLE_H + +//#include "Arduino.h" +#include "register.h" +#include "commonregs.h" + +/** + * Register indexes + */ +DEFINE_REGINDEX_START() + REGI_VOLTSUPPLY, + REGI_CO2SENSOR, + REGI_HTUSENSOR, +DEFINE_REGINDEX_END() + +#endif + diff --git a/examples/co2meter/regtable.ino b/examples/co2meter/regtable.ino new file mode 100644 index 0000000..6ecb5e4 --- /dev/null +++ b/examples/co2meter/regtable.ino @@ -0,0 +1,120 @@ +/** + * regtable + * + * Copyright (c) 2015 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Francisco Barrena + * Creation date: 04/10/2015 + */ + +#include "product.h" +#include "regtable.h" + +/** + * Definition of common registers + */ +DEFINE_COMMON_REGISTERS() + +/* + * Definition of custom registers + */ +// Voltage supply +static byte dtVoltSupply[2]; +REGISTER regVoltSupply(dtVoltSupply, sizeof(dtVoltSupply), &updtVoltSupply, NULL); +// Co2 Sensor value +static byte dtCo2Sensor[2]; +REGISTER regCo2Sensor(dtCo2Sensor, sizeof(dtCo2Sensor), &updtCo2Sensor, NULL); +// Htu Sensor value +static byte dtHtuSensor[4]; +REGISTER regHtuSensor(dtHtuSensor, sizeof(dtHtuSensor), &updtHtuSensor, NULL); + + + +/** + * Initialize table of registers + */ +DECLARE_REGISTERS_START() + ®VoltSupply, + ®Co2Sensor, + ®HtuSensor +DECLARE_REGISTERS_END() + +/** + * Definition of common getter/setter callback functions + */ +DEFINE_COMMON_CALLBACKS() + +/** + * Definition of custom getter/setter callback functions + */ + +/** + * updtVoltSupply + * + * Measure voltage supply and update register + * + * 'rId' Register ID + */ +const void updtVoltSupply(byte rId) +{ + unsigned long result = panstamp.getVcc(); + + // Update register value + regTable[rId]->value[0] = (result >> 8) & 0xFF; + regTable[rId]->value[1] = result & 0xFF; +} + +/** + * updtCo2Sensor + * + * Update CO2 register + * + * 'rId' Register ID + */ +const void updtCo2Sensor(byte rId) +{ + // Update register value + dtCo2Sensor[0] = (co2Level >> 8) & 0xFF; + dtCo2Sensor[1] = co2Level & 0xFF; +} + +/** + * updtHtuSensor + * + * Measure T+H sensor data and update register + * + * 'rId' Register ID + */ +const void updtHtuSensor(byte rId) +{ + // Read humidity + float h = htu.readHumidity(); + // Read temperature + float t = htu.readTemperature(); + + uint16_t humidity = h * 10; + uint16_t temperature = (t + 50) * 10; + + dtHtuSensor[0] = (temperature >> 8) & 0xFF; + dtHtuSensor[1] = temperature & 0xFF; + dtHtuSensor[2] = (humidity >> 8) & 0xFF; + dtHtuSensor[3] = humidity & 0xFF; +} + diff --git a/examples/dhtsensor/dhtsensor.ino b/examples/dhtsensor/dhtsensor.ino new file mode 100644 index 0000000..ff3b81f --- /dev/null +++ b/examples/dhtsensor/dhtsensor.ino @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 06/05/2014 + * + * Device: + * Dual Temperature + Humidity sensor + * + * IMPORTANT: This sketch works only for panStamp NRG. For panStamp AVR + * use temphumpress instead. + * + * Description: + * This sketch generates a SWAP temperature+humidity sensor device + * relying on the popular DHT22 senor and Adafruit's library + * https://github.com/adafruit/DHT-sensor-library + * + * These devices are low-power enabled so they will enter low-power mode + * just after reading the sensor values and transmitting them over the + * SWAP network. + * + * Associated Device Definition Files, defining registers, endpoints and + * configuration parameters: + * temphum.xml (Dual Humidity + Temperature sensor) + */ + +#include "regtable.h" +#include "swap.h" +#include "DHT.h" + + +/** + * Uncomment if you are reading Vcc from A0. All battery-boards do this + */ +#define VOLT_SUPPLY_A0 1 + +#ifdef PANSTAMP_NRG +#define DHTPIN 14 +#else +#define DHTPIN 16 +#endif +DHT dht(DHTPIN, DHT22); +#define PWRPIN 15 + + +/** + * setup + * + * Arduino setup function + */ +void setup() +{ + int i; + + // Init SWAP stack + swap.init(); + + // Initialize LED pins + pinMode(LED, OUTPUT); + + // Initialize sensor pins + dht.begin(); + pinMode(PWRPIN, OUTPUT); + + // Transmit product code + swap.getRegister(REGI_PRODUCTCODE)->getData(); + + // Enter SYNC state + swap.enterSystemState(SYSTATE_SYNC); + + // During 3 seconds, listen the network for possible commands whilst the LED blinks + for(i=0 ; i<6 ; i++) + { + digitalWrite(LED, HIGH); + delay(100); + digitalWrite(LED, LOW); + delay(400); + } + + // Transmit periodic Tx interval + swap.getRegister(REGI_TXINTERVAL)->getData(); + // Transmit power voltage + swap.getRegister(REGI_VOLTSUPPLY)->getData(); + // Switch to Rx OFF state + swap.enterSystemState(SYSTATE_RXOFF); +} + +/** + * loop + * + * Arduino main loop + */ +void loop() +{ +// digitalWrite(LED, HIGH); + // Transmit sensor data + swap.getRegister(REGI_SENSOR)->getData(); + // Transmit power voltage + swap.getRegister(REGI_VOLTSUPPLY)->getData(); +// digitalWrite(LED, LOW); + + // Sleep + swap.goToSleep(); +} + diff --git a/examples/dhtsensor/product.h b/examples/dhtsensor/product.h new file mode 100644 index 0000000..fdbdd17 --- /dev/null +++ b/examples/dhtsensor/product.h @@ -0,0 +1,51 @@ +/** + * product.h + * + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#ifndef _PRODUCT_H +#define _PRODUCT_H + +/** + * Hardware version + */ +#define HARDWARE_VERSION 0x00000200L + +/** + * Firmware version + */ +#define FIRMWARE_VERSION 0x00000101L + +/** + * Manufacturer SWAP ID + */ +#define SWAP_MANUFACT_ID 0x00000001L + +/** + * Product SWAP ID + */ +#define SWAP_PRODUCT_ID 0x00000001L + +#endif + diff --git a/examples/dhtsensor/regtable.h b/examples/dhtsensor/regtable.h new file mode 100644 index 0000000..61642bb --- /dev/null +++ b/examples/dhtsensor/regtable.h @@ -0,0 +1,42 @@ +/** + * regtable.h + * + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#ifndef _REGTABLE_H +#define _REGTABLE_H + +#include "register.h" +#include "commonregs.h" + +/** + * Register indexes + */ +DEFINE_REGINDEX_START() + REGI_VOLTSUPPLY, + REGI_SENSOR +DEFINE_REGINDEX_END() + +#endif + diff --git a/examples/dhtsensor/regtable.ino b/examples/dhtsensor/regtable.ino new file mode 100644 index 0000000..a57136b --- /dev/null +++ b/examples/dhtsensor/regtable.ino @@ -0,0 +1,120 @@ +/** + * regtable + * + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#include "product.h" +#include "regtable.h" + +/** + * Definition of common registers + */ +DEFINE_COMMON_REGISTERS() + +/* + * Definition of custom registers + */ +// Voltage supply +static unsigned long voltageSupply = 3300; +static byte dtVoltSupply[2]; +REGISTER regVoltSupply(dtVoltSupply, sizeof(dtVoltSupply), &updtVoltSupply, NULL); +// Sensor value register +static byte dtSensor[4]; +REGISTER regSensor(dtSensor, sizeof(dtSensor), &updtSensor, NULL); + +/** + * Initialize table of registers + */ +DECLARE_REGISTERS_START() + ®VoltSupply, + ®Sensor +DECLARE_REGISTERS_END() + +/** + * Definition of common getter/setter callback functions + */ +DEFINE_COMMON_CALLBACKS() + +/** + * Definition of custom getter/setter callback functions + */ + +/** + * updtVoltSupply + * + * Measure voltage supply and update register + * + * 'rId' Register ID + */ +const void updtVoltSupply(byte rId) +{ + unsigned long result; + + #ifdef VOLT_SUPPLY_A0 + // Read voltage supply from A0 + unsigned short ref = voltageSupply; + result = analogRead(A0); + result *= ref; + result /= 4095; + #else + result = panstamp.getVcc(); + #endif + + /** + * register[eId]->member can be replaced by regVoltSupply in this case since + * no other register is going to use "updtVoltSupply" as "updater" function + */ + + // Update register value + regTable[rId]->value[0] = (result >> 8) & 0xFF; + regTable[rId]->value[1] = result & 0xFF; +} + +/** + * updtSensor + * + * Measure sensor data and update register + * + * 'rId' Register ID + */ +const void updtSensor(byte rId) +{ + // Power sensor + digitalWrite(PWRPIN, HIGH); + delay(1500); + // Read humidity + float h = dht.readHumidity(); + // Read temperature + float t = dht.readTemperature(); + // Unpower sensor + digitalWrite(PWRPIN, LOW); + + uint16_t humidity = h * 10; + uint16_t temperature = (t + 50) * 10; + + dtSensor[0] = (temperature >> 8) & 0xFF; + dtSensor[1] = temperature & 0xFF; + dtSensor[2] = (humidity >> 8) & 0xFF; + dtSensor[3] = humidity & 0xFF; +} diff --git a/examples/htusensor/htusensor.ino b/examples/htusensor/htusensor.ino new file mode 100644 index 0000000..302922e --- /dev/null +++ b/examples/htusensor/htusensor.ino @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 08/08/2014 + * + * Device: + * Dual Temperature + Humidity sensor + * + * Description: + * This sketch generates a SWAP temperature+humidity sensor device + * relying on HTU21D sensor and Sparkfun's library + * https://github.com/sparkfun/HTU21D_Breakout + * + * This library and the current sketch is also compatible with Silabs' + * SI7021 temperature+humidity sensor. SI7021 is the sensor that can be + * hosted by panStamp NRG on the bottom layer. + * + * These devices are low-power enabled so they will enter low-power mode + * just after reading the sensor values and transmitting them over the + * SWAP network. + * + * Associated Device Definition Files, defining registers, endpoints and + * configuration parameters: + * temphum.xml (Dual Humidity + Temperature sensor) + */ + +#include "Wire.h" +#include "regtable.h" +#include "swap.h" +#include "HTU21D.h" + + +//Create an instance of the sensor object +HTU21D htu; + +void setup() +{ + int i; + + // Init SWAP stack + swap.init(); + + // Initialize LED pins + pinMode(LED, OUTPUT); + + // Initialize sensor + htu.begin(); + + // Transmit product code + swap.getRegister(REGI_PRODUCTCODE)->getData(); + + // Enter SYNC state + swap.enterSystemState(SYSTATE_SYNC); + + // During 3 seconds, listen the network for possible commands whilst the LED blinks + for(i=0 ; i<6 ; i++) + { + digitalWrite(LED, HIGH); + delay(100); + digitalWrite(LED, LOW); + delay(400); + } + + // Transmit periodic Tx interval + swap.getRegister(REGI_TXINTERVAL)->getData(); + // Transmit power voltage + swap.getRegister(REGI_VOLTSUPPLY)->getData(); + // Switch to Rx OFF state + swap.enterSystemState(SYSTATE_RXOFF); +} + +void loop() +{ +// digitalWrite(LED, HIGH); + // Transmit sensor data + swap.getRegister(REGI_SENSOR)->getData(); + // Transmit power voltage + swap.getRegister(REGI_VOLTSUPPLY)->getData(); +// digitalWrite(LED, LOW); + + // Sleep + swap.goToSleep(); +} + diff --git a/examples/htusensor/product.h b/examples/htusensor/product.h new file mode 100644 index 0000000..5f0f1be --- /dev/null +++ b/examples/htusensor/product.h @@ -0,0 +1,51 @@ +/** + * product.h + * + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 08/08/2014 + */ + +#ifndef _PRODUCT_H +#define _PRODUCT_H + +/** + * Hardware version + */ +#define HARDWARE_VERSION 0x00000200L + +/** + * Firmware version + */ +#define FIRMWARE_VERSION 0x00000101L + +/** + * Manufacturer SWAP ID + */ +#define SWAP_MANUFACT_ID 0x00000001L + +/** + * Product SWAP ID + */ +#define SWAP_PRODUCT_ID 0x00000001L + +#endif + diff --git a/examples/htusensor/regtable.h b/examples/htusensor/regtable.h new file mode 100644 index 0000000..49d5bfc --- /dev/null +++ b/examples/htusensor/regtable.h @@ -0,0 +1,42 @@ +/** + * regtable.h + * + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 08/08/2014 + */ + +#ifndef _REGTABLE_H +#define _REGTABLE_H + +#include "register.h" +#include "commonregs.h" + +/** + * Register indexes + */ +DEFINE_REGINDEX_START() + REGI_VOLTSUPPLY, + REGI_SENSOR +DEFINE_REGINDEX_END() + +#endif + diff --git a/examples/htusensor/regtable.ino b/examples/htusensor/regtable.ino new file mode 100644 index 0000000..d62518f --- /dev/null +++ b/examples/htusensor/regtable.ino @@ -0,0 +1,99 @@ +/** + * regtable + * + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 08/08/2014 + */ + +#include "product.h" +#include "regtable.h" + +/** + * Definition of common registers + */ +DEFINE_COMMON_REGISTERS() + +/* + * Definition of custom registers + */ +// Voltage supply +static byte dtVoltSupply[2]; +REGISTER regVoltSupply(dtVoltSupply, sizeof(dtVoltSupply), &updtVoltSupply, NULL); +// Sensor value register +static byte dtSensor[4]; +REGISTER regSensor(dtSensor, sizeof(dtSensor), &updtSensor, NULL); + +/** + * Initialize table of registers + */ +DECLARE_REGISTERS_START() + ®VoltSupply, + ®Sensor +DECLARE_REGISTERS_END() + +/** + * Definition of common getter/setter callback functions + */ +DEFINE_COMMON_CALLBACKS() + +/** + * Definition of custom getter/setter callback functions + */ + +/** + * updtVoltSupply + * + * Measure voltage supply and update register + * + * 'rId' Register ID + */ +const void updtVoltSupply(byte rId) +{ + unsigned long result = panstamp.getVcc(); + + // Update register value + regTable[rId]->value[0] = (result >> 8) & 0xFF; + regTable[rId]->value[1] = result & 0xFF; +} + +/** + * updtSensor + * + * Measure sensor data and update register + * + * 'rId' Register ID + */ +const void updtSensor(byte rId) +{ + // Read humidity + float h = htu.readHumidity(); + // Read temperature + float t = htu.readTemperature(); + + uint16_t humidity = h * 10; + uint16_t temperature = (t + 50) * 10; + + dtSensor[0] = (temperature >> 8) & 0xFF; + dtSensor[1] = temperature & 0xFF; + dtSensor[2] = (humidity >> 8) & 0xFF; + dtSensor[3] = humidity & 0xFF; +} diff --git a/examples/ntc/ntc.ino b/examples/ntc/ntc.ino new file mode 100644 index 0000000..2931362 --- /dev/null +++ b/examples/ntc/ntc.ino @@ -0,0 +1,120 @@ +/** + * Copyright (c) 2014 panStamp S.L.U. + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * (at your option) any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 02/24/2014 + * + * Device: + * Temperature sensor + * + * Description: + * This is a basic device measuring temperature from the onboard 10Kohm + * NTC thermistor + * + * Thos device is low-power enabled so it will enter low-power mode + * just after reading the sensor value and transmitting it over the + * SWAP network. + * + * Associated Device Definition Files, defining registers, endpoints and + * configuration parameters: + * temp.xml (Temperature sensor) + */ + +#include "regtable.h" +#include "swap.h" +#include "thermistor.h" + + +// Uncomment for AVR - Leave commented for NRG (NTC_PIN and NTC_POWER_PIN are already defined in pins.h) +// Digital output used to power the thermistor circuit +//#define NTC_POWER_PIN 10 +// Analog pin used to read the NTC +//#define NTC_PIN A1 + +// Macros +#define powerThermistorOn() digitalWrite(NTC_POWER_PIN, HIGH) +#define powerThermistorOff() digitalWrite(NTC_POWER_PIN, LOW) + +// Thermistor object +THERMISTOR thermistor(NTC_PIN, // Analog pin + 10000, // Nominal resistance at 25 ÂșC + 3950, // thermistor's beta coefficient + 10000); // Value of the series resistor + +/** + * setup + * + * Arduino setup function + */ +void setup() +{ + int i; + + // Init SWAP stack + swap.init(); + + // Initialize LED pins + pinMode(LED, OUTPUT); + + pinMode(NTC_POWER_PIN, OUTPUT); // Configure power pin. This pin will be used to power the thermistor + powerThermistorOff(); // Unpower sensor by default + + // Transmit product code + swap.getRegister(REGI_PRODUCTCODE)->getData(); + + // Enter SYNC state + swap.enterSystemState(SYSTATE_SYNC); + + // During 3 seconds, listen the network for possible commands whilst the LED blinks + for(i=0 ; i<6 ; i++) + { + digitalWrite(LED, HIGH); + delay(100); + digitalWrite(LED, LOW); + delay(400); + } + + // Transmit periodic Tx interval + swap.getRegister(REGI_TXINTERVAL)->getData(); + + // Switch to Rx OFF state + swap.enterSystemState(SYSTATE_RXOFF); +} + +/** + * loop + * + * Arduino main loop + */ +void loop() +{ + digitalWrite(LED, HIGH); + + // Transmit temperature value + swap.getRegister(REGI_SENSOR)->getData(); + // Transmit power voltage + swap.getRegister(REGI_VOLTSUPPLY)->getData(); + + digitalWrite(LED, LOW); + + // Sleep + swap.goToSleep(); +} + diff --git a/examples/ntc/product.h b/examples/ntc/product.h new file mode 100755 index 0000000..19f2054 --- /dev/null +++ b/examples/ntc/product.h @@ -0,0 +1,49 @@ +/** + * Copyright (c) 2014 panStamp S.L.U. + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * (at your option) any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 02/24/2014 + */ + +#ifndef _PRODUCT_H +#define _PRODUCT_H + +/** + * Hardware version + */ +#define HARDWARE_VERSION 0x00000300L + +/** + * Firmware version + */ +#define FIRMWARE_VERSION 0x00000100L + +/** + * Manufacturer SWAP ID + */ +#define SWAP_MANUFACT_ID 0x00000001L + +/** + * Product SWAP ID + */ +#define SWAP_PRODUCT_ID 0x00000004L + +#endif + diff --git a/examples/ntc/regtable.h b/examples/ntc/regtable.h new file mode 100644 index 0000000..c79f11a --- /dev/null +++ b/examples/ntc/regtable.h @@ -0,0 +1,41 @@ +/** + * Copyright (c) 2014 panStamp S.L.U. + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * (at your option) any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 02/24/2014 + */ + +#ifndef _REGTABLE_H +#define _REGTABLE_H + +#include "Arduino.h" +#include "register.h" +#include "commonregs.h" + +/** + * Register indexes + */ +DEFINE_REGINDEX_START() + REGI_VOLTSUPPLY, + REGI_SENSOR +DEFINE_REGINDEX_END() + +#endif + diff --git a/examples/ntc/regtable.ino b/examples/ntc/regtable.ino new file mode 100644 index 0000000..f6f9b47 --- /dev/null +++ b/examples/ntc/regtable.ino @@ -0,0 +1,103 @@ +/** + * Copyright (c) 2014 panStamp S.L.U. + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * (at your option) any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 02/24/2014 + */ + +#include "product.h" +#include "panstamp.h" +#include "regtable.h" + +/** + * Definition of common registers + */ +DEFINE_COMMON_REGISTERS() + +/* + * Definition of custom registers + */ +// Voltage supply +static byte dtVoltSupply[2]; +REGISTER regVoltSupply(dtVoltSupply, sizeof(dtVoltSupply), &updtVoltSupply, NULL); +// Sensor value register +static byte dtSensor[2]; +REGISTER regSensor(dtSensor, sizeof(dtSensor), &updtSensor, NULL); + +/** + * Initialize table of registers + */ +DECLARE_REGISTERS_START() + ®VoltSupply, + ®Sensor +DECLARE_REGISTERS_END() + +/** + * Definition of common getter/setter callback functions + */ +DEFINE_COMMON_CALLBACKS() + +/** + * Definition of custom getter/setter callback functions + */ + +/** + * updtVoltSupply + * + * Measure voltage supply and update register + * + * 'rId' Register ID + */ +const void updtVoltSupply(byte rId) +{ + unsigned long result = panstamp.getVcc(); + + /** + * register[eId]->member can be replaced by regVoltSupply in this case since + * no other register is going to use "updtVoltSupply" as "updater" function + */ + + // Update register value + regTable[rId]->value[0] = (result >> 8) & 0xFF; + regTable[rId]->value[1] = result & 0xFF; +} + +/** + * updtSensor + * + * Measure sensor data and update register + * + * 'rId' Register ID + */ +const void updtSensor(byte rId) +{ + uint16_t temp; + + powerThermistorOn(); // Power thermistor + temp = thermistor.read(); // Read temperature + powerThermistorOff(); // Unpower thermistor + + temp += 500; + + // Fill register + dtSensor[0] = (temp >> 8) & 0xFF; + dtSensor[1] = temp & 0xFF; +} + diff --git a/examples/pulsecounter/.svn/all-wcprops b/examples/pulsecounter/.svn/all-wcprops new file mode 100644 index 0000000..414d984 --- /dev/null +++ b/examples/pulsecounter/.svn/all-wcprops @@ -0,0 +1,29 @@ +K 25 +svn:wc:ra_dav:version-url +V 53 +/svn/!svn/ver/639/trunk/arduino/sketches/pulsecounter +END +product.h +K 25 +svn:wc:ra_dav:version-url +V 63 +/svn/!svn/ver/639/trunk/arduino/sketches/pulsecounter/product.h +END +regtable.ino +K 25 +svn:wc:ra_dav:version-url +V 66 +/svn/!svn/ver/639/trunk/arduino/sketches/pulsecounter/regtable.ino +END +regtable.h +K 25 +svn:wc:ra_dav:version-url +V 64 +/svn/!svn/ver/639/trunk/arduino/sketches/pulsecounter/regtable.h +END +pulsecounter.ino +K 25 +svn:wc:ra_dav:version-url +V 70 +/svn/!svn/ver/639/trunk/arduino/sketches/pulsecounter/pulsecounter.ino +END diff --git a/examples/pulsecounter/.svn/entries b/examples/pulsecounter/.svn/entries new file mode 100644 index 0000000..56bbf39 --- /dev/null +++ b/examples/pulsecounter/.svn/entries @@ -0,0 +1,164 @@ +10 + +dir +770 +https://panstamp.googlecode.com/svn/trunk/arduino/sketches/pulsecounter +https://panstamp.googlecode.com/svn + + + +2012-07-24T14:31:36.254626Z +639 +dberenguer@usapiens.com + + + + + + + + + + + + + + +d77f469f-0316-7e13-d0a9-da7f422f7f4c + +product.h +file + + + + +2012-07-26T09:30:27.000000Z +ffcc98606a5f8839e142c3b6ff1e1e0c +2012-07-24T14:31:36.254626Z +639 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +1233 + +regtable.ino +file + + + + +2012-07-26T09:30:27.000000Z +c3765d5c263cf6124cba343f0235ef77 +2012-07-24T14:31:36.254626Z +639 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +2709 + +regtable.h +file + + + + +2012-07-26T09:30:27.000000Z +ee5250146ba9cc6ede926d2c2690a3a8 +2012-07-24T14:31:36.254626Z +639 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +1122 + +pulsecounter.ino +file + + + + +2012-07-26T09:30:27.000000Z +3dca9688c44b9af810669560b6a04c2c +2012-07-24T14:31:36.254626Z +639 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +3049 + diff --git a/examples/pulsecounter/.svn/text-base/product.h.svn-base b/examples/pulsecounter/.svn/text-base/product.h.svn-base new file mode 100644 index 0000000..e1d630e --- /dev/null +++ b/examples/pulsecounter/.svn/text-base/product.h.svn-base @@ -0,0 +1,51 @@ +/** + * product.h + * + * Copyright (c) 2012 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 01/19/2012 + */ + +#ifndef _PRODUCT_H +#define _PRODUCT_H + +/** + * Hardware version + */ +#define HARDWARE_VERSION 0x00000200 + +/** + * Firmware version + */ +#define FIRMWARE_VERSION 0x00000100 + +/** + * Manufacturer SWAP ID + */ +#define SWAP_MANUFACT_ID 0x00000001 + +/** + * Product SWAP ID + */ +#define SWAP_PRODUCT_ID 0x0000000A + +#endif + diff --git a/examples/pulsecounter/.svn/text-base/pulsecounter.ino.svn-base b/examples/pulsecounter/.svn/text-base/pulsecounter.ino.svn-base new file mode 100644 index 0000000..321fa6b --- /dev/null +++ b/examples/pulsecounter/.svn/text-base/pulsecounter.ino.svn-base @@ -0,0 +1,142 @@ +/* + * pulseCounter + * + * Copyright (c) 2012 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 07/24/2012 + * + * Device: + * Counter module + * + * Description: + * Pulse counter on INT1 (pin 18) + * + * This sketch can be used to count pulses and send this count at a + * maximum transmission rate + * + * This device is low-power enabled so it will enter low-power mode just + * after reading a count or transmitting to the SWAP network. + * + * Associated Device Definition File, defining registers, endpoints and + * configuration parameters: + * pulsecounter.xml (Pulse counter) + */ + +#include "regtable.h" +#include "panstamp.h" + +/** + * Macros + */ +#define enableINT1irq() attachInterrupt(1, isrINT1event, RISING) +#define disableINT1irq() detachInterrupt(1) + +/** + * LED pin + */ +#define LEDPIN 4 + +/** + * Counter values + */ +unsigned long currentCounter = 0; +unsigned long oldCounter = 0; + +/** + * Interrupt flag + */ +boolean int1IRQ = false; + +/** + * isrINT1event + * + * Event on INT1 + */ +void isrINT1event(void) +{ + currentCounter++; +} + +/** + * setup + * + * Arduino setup function + */ +void setup() +{ + int i; + + pinMode(LEDPIN, OUTPUT); + digitalWrite(LEDPIN, LOW); + + // Init panStamp + panstamp.init(); + + // Transmit product code + getRegister(REGI_PRODUCTCODE)->getData(); + + // Enter SYNC state + panstamp.enterSystemState(SYSTATE_SYNC); + + // During 3 seconds, listen the network for possible commands whilst the LED blinks + for(i=0 ; i<6 ; i++) + { + digitalWrite(LEDPIN, HIGH); + delay(100); + digitalWrite(LEDPIN, LOW); + delay(400); + } + // Transmit periodic Tx interval + getRegister(REGI_TXINTERVAL)->getData(); + // Transmit power voltage + getRegister(REGI_VOLTSUPPLY)->getData(); + // Transmit initial counter values + getRegister(REGI_COUNTER)->getData(); + + // Switch to Rx OFF state + panstamp.enterSystemState(SYSTATE_RXOFF); + + // Enable INT1 Interrupt + enableINT1irq(); +} + +/** + * loop + * + * Arduino main loop + */ +void loop() +{ + // Sleep for panstamp.txInterval seconds (register 10) + panstamp.goToSleep(); + + disableINT1irq(); + + // Transmit counter value only if changed + if (currentCounter != oldCounter) + { + getRegister(REGI_COUNTER)->getData(); + oldCounter = currentCounter; + } + + enableINT1irq(); +} + diff --git a/examples/pulsecounter/.svn/text-base/regtable.h.svn-base b/examples/pulsecounter/.svn/text-base/regtable.h.svn-base new file mode 100644 index 0000000..13face5 --- /dev/null +++ b/examples/pulsecounter/.svn/text-base/regtable.h.svn-base @@ -0,0 +1,43 @@ +/** + * regtable.h + * + * Copyright (c) 2012 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 01/19/2012 + */ + +#ifndef _REGTABLE_H +#define _REGTABLE_H + +#include "Arduino.h" +#include "register.h" +#include "commonregs.h" + +/** + * Register indexes + */ +DEFINE_REGINDEX_START() + REGI_VOLTSUPPLY, + REGI_COUNTER +DEFINE_REGINDEX_END() + +#endif + diff --git a/examples/pulsecounter/.svn/text-base/regtable.ino.svn-base b/examples/pulsecounter/.svn/text-base/regtable.ino.svn-base new file mode 100644 index 0000000..9665764 --- /dev/null +++ b/examples/pulsecounter/.svn/text-base/regtable.ino.svn-base @@ -0,0 +1,114 @@ +/** + * regtable + * + * Copyright (c) 2012 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 01/19/2012 + */ + +#include +#include "product.h" +#include "panstamp.h" +#include "regtable.h" + +/** + * Declaration of common callback functions + */ +DECLARE_COMMON_CALLBACKS() + +/** + * Definition of common registers + */ +DEFINE_COMMON_REGISTERS() + +/* + * Definition of custom registers + */ +// Voltage supply +static byte dtVoltSupply[2]; +REGISTER regVoltSupply(dtVoltSupply, sizeof(dtVoltSupply), &updtVoltSupply, NULL); +// 4-byte counte registers +byte dtCounter[4]; // Pulse counters +REGISTER regCounter(dtCounter, sizeof(dtCounter), &updtCounter, NULL); + +/** + * Initialize table of registers + */ +DECLARE_REGISTERS_START() + ®VoltSupply, + ®Counter +DECLARE_REGISTERS_END() + +/** + * Definition of common getter/setter callback functions + */ +DEFINE_COMMON_CALLBACKS() + +/** + * Definition of custom getter/setter callback functions + */ + +/** + * updtVoltSupply + * + * Measure voltage supply and update register + * + * 'rId' Register ID + */ +const void updtVoltSupply(byte rId) +{ + unsigned short result; + + // Read 1.1V reference against AVcc + ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); + 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; // Back-calculate AVcc in mV + + /** + * register[eId]->member can be replaced by regVoltSupply in this case since + * no other register is going to use "updtVoltSupply" as "updater" function + */ + + // Update register value + regTable[rId]->value[0] = (result >> 8) & 0xFF; + regTable[rId]->value[1] = result & 0xFF; +} + +/** + * updtCounter + * + * Update counter + * + * 'rId' Register ID + */ +const void updtCounter(byte rId) +{ + byte i, j; + + // Update register + for(i=0 ; i<4 ; i++) + dtCounter[i] = (currentCounter >> 8 * (3-i)) & 0xFF; +} + diff --git a/examples/pulsecounter/product.h b/examples/pulsecounter/product.h new file mode 100644 index 0000000..d7aff4a --- /dev/null +++ b/examples/pulsecounter/product.h @@ -0,0 +1,51 @@ +/** + * product.h + * + * Copyright (c) 2012 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 01/19/2012 + */ + +#ifndef _PRODUCT_H +#define _PRODUCT_H + +/** + * Hardware version + */ +#define HARDWARE_VERSION 0x00000200L + +/** + * Firmware version + */ +#define FIRMWARE_VERSION 0x00000100L + +/** + * Manufacturer SWAP ID + */ +#define SWAP_MANUFACT_ID 0x00000001L + +/** + * Product SWAP ID + */ +#define SWAP_PRODUCT_ID 0x0000000AL + +#endif + diff --git a/examples/pulsecounter/pulsecounter.ino b/examples/pulsecounter/pulsecounter.ino new file mode 100644 index 0000000..cfad33f --- /dev/null +++ b/examples/pulsecounter/pulsecounter.ino @@ -0,0 +1,137 @@ +/* + * pulseCounter + * + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 07/24/2012 + * + * Device: + * Counter module + * + * Description: + * PANSTAMP_NRG + * Pulse counter on Digital pin 1 (pin 22) + * PANSTAMP_AVR + * Pulse counter on INT1 (pin 18) + * + * This sketch can be used to count pulses and send this count at a + * maximum transmission rate + * + * This device is low-power enabled so it will enter low-power mode just + * after reading a count or transmitting to the SWAP network. + * + * Associated Device Definition File, defining registers, endpoints and + * configuration parameters: + * pulsecounter.xml (Pulse counter) + */ + +#include "regtable.h" +#include "swap.h" + +/** + * Macros + */ +#define ENABLE_PIN_IRQ() attachInterrupt(1, isrPinEvent, RISING) +#define DISABLE_PIN_IRQ() detachInterrupt(1) + +/** + * Counter values + */ +unsigned long currentCounter = 0; +unsigned long oldCounter = 0; + +/** + * isrPinEvent + * + * Event on pin + */ +void isrPinEvent(void) +{ + currentCounter++; +} + +/** + * setup + * + * Arduino setup function + */ +void setup() +{ + int i; + + pinMode(LED, OUTPUT); + digitalWrite(LED, LOW); + + // Init SWAP stack + swap.init(); + + // Transmit product code + swap.getRegister(REGI_PRODUCTCODE)->getData(); + + // Enter SYNC state + swap.enterSystemState(SYSTATE_SYNC); + + // During 3 seconds, listen the network for possible commands whilst the LED blinks + for(i=0 ; i<6 ; i++) + { + digitalWrite(LED, HIGH); + delay(100); + digitalWrite(LED, LOW); + delay(400); + } + // Transmit periodic Tx interval + swap.getRegister(REGI_TXINTERVAL)->getData(); + // Transmit power voltage + swap.getRegister(REGI_VOLTSUPPLY)->getData(); + // Transmit initial counter values + swap.getRegister(REGI_COUNTER)->getData(); + + // Switch to Rx OFF state + swap.enterSystemState(SYSTATE_RXOFF); + + pinMode(1, INPUT); + + // Enable INT1 Interrupt + ENABLE_PIN_IRQ(); +} + +/** + * loop + * + * Arduino main loop + */ +void loop() +{ + // Sleep for panstamp.txInterval seconds (register 10) + swap.goToSleep(); + + DISABLE_PIN_IRQ(); + + // Transmit counter value only if changed + if (currentCounter != oldCounter) + { + swap.getRegister(REGI_COUNTER)->getData(); + oldCounter = currentCounter; + } + + ENABLE_PIN_IRQ(); +} + diff --git a/examples/pulsecounter/regtable.h b/examples/pulsecounter/regtable.h new file mode 100644 index 0000000..6009cc1 --- /dev/null +++ b/examples/pulsecounter/regtable.h @@ -0,0 +1,42 @@ +/** + * regtable.h + * + * Copyright (c) 2012 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 01/19/2012 + */ + +#ifndef _REGTABLE_H +#define _REGTABLE_H + +#include "register.h" +#include "commonregs.h" + +/** + * Register indexes + */ +DEFINE_REGINDEX_START() + REGI_VOLTSUPPLY, + REGI_COUNTER +DEFINE_REGINDEX_END() + +#endif + diff --git a/examples/pulsecounter/regtable.ino b/examples/pulsecounter/regtable.ino new file mode 100644 index 0000000..6c411b2 --- /dev/null +++ b/examples/pulsecounter/regtable.ino @@ -0,0 +1,93 @@ +/** + * regtable + * + * Copyright (c) 2012 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 01/19/2012 + */ + +#include "product.h" +#include "regtable.h" + +/** + * Definition of common registers + */ +DEFINE_COMMON_REGISTERS() + +/* + * Definition of custom registers + */ +// Voltage supply +static byte dtVoltSupply[2]; +REGISTER regVoltSupply(dtVoltSupply, sizeof(dtVoltSupply), &updtVoltSupply, NULL); +// 4-byte counte registers +byte dtCounter[4]; // Pulse counters +REGISTER regCounter(dtCounter, sizeof(dtCounter), &updtCounter, NULL); + +/** + * Initialize table of registers + */ +DECLARE_REGISTERS_START() + ®VoltSupply, + ®Counter +DECLARE_REGISTERS_END() + +/** + * Definition of common getter/setter callback functions + */ +DEFINE_COMMON_CALLBACKS() + +/** + * Definition of custom getter/setter callback functions + */ + +/** + * updtVoltSupply + * + * Measure voltage supply and update register + * + * 'rId' Register ID + */ +const void updtVoltSupply(byte rId) +{ + unsigned long result = panstamp.getVcc(); + + // Update register value + regTable[rId]->value[0] = (result >> 8) & 0xFF; + regTable[rId]->value[1] = result & 0xFF; +} + +/** + * updtCounter + * + * Update counter + * + * 'rId' Register ID + */ +const void updtCounter(byte rId) +{ + byte i, j; + + // Update register + for(i=0 ; i<4 ; i++) + dtCounter[i] = (currentCounter >> 8 * (3-i)) & 0xFF; +} + diff --git a/examples/respira/.svn/all-wcprops b/examples/respira/.svn/all-wcprops new file mode 100644 index 0000000..74e6bd7 --- /dev/null +++ b/examples/respira/.svn/all-wcprops @@ -0,0 +1,41 @@ +K 25 +svn:wc:ra_dav:version-url +V 48 +/svn/!svn/ver/913/trunk/arduino/sketches/respira +END +product.h +K 25 +svn:wc:ra_dav:version-url +V 58 +/svn/!svn/ver/913/trunk/arduino/sketches/respira/product.h +END +regtable.ino +K 25 +svn:wc:ra_dav:version-url +V 61 +/svn/!svn/ver/913/trunk/arduino/sketches/respira/regtable.ino +END +respira.ino +K 25 +svn:wc:ra_dav:version-url +V 60 +/svn/!svn/ver/913/trunk/arduino/sketches/respira/respira.ino +END +regtable.h +K 25 +svn:wc:ra_dav:version-url +V 59 +/svn/!svn/ver/913/trunk/arduino/sketches/respira/regtable.h +END +sensor.ino +K 25 +svn:wc:ra_dav:version-url +V 59 +/svn/!svn/ver/913/trunk/arduino/sketches/respira/sensor.ino +END +sensor.h +K 25 +svn:wc:ra_dav:version-url +V 57 +/svn/!svn/ver/913/trunk/arduino/sketches/respira/sensor.h +END diff --git a/examples/respira/.svn/entries b/examples/respira/.svn/entries new file mode 100644 index 0000000..9f49fc6 --- /dev/null +++ b/examples/respira/.svn/entries @@ -0,0 +1,232 @@ +10 + +dir +913 +https://panstamp.googlecode.com/svn/trunk/arduino/sketches/respira +https://panstamp.googlecode.com/svn + + + +2013-04-30T18:18:21.025856Z +913 +dberenguer@usapiens.com + + + + + + + + + + + + + + +d77f469f-0316-7e13-d0a9-da7f422f7f4c + +sensor.h +file + + + + +2013-02-08T14:47:52.891174Z +b0438262aaa360ec389cfebe2a39434c +2013-04-30T18:18:21.025856Z +913 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +2231 + +product.h +file + + + + +2013-02-08T10:15:43.431095Z +5c72394458ac69d58097a0a6638c98ab +2013-04-30T18:18:21.025856Z +913 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +1233 + +respira.ino +file + + + + +2013-02-26T20:23:37.182544Z +332da8c5cb19e8a4f94299a02fd55950 +2013-04-30T18:18:21.025856Z +913 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +2596 + +regtable.ino +file + + + + +2013-02-21T18:33:52.188248Z +7560f90b9b7941edd6eacfd3e9602974 +2013-04-30T18:18:21.025856Z +913 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +3311 + +regtable.h +file + + + + +2013-02-08T11:19:10.455113Z +24527101c79a1eca9bc1d8f6d8f59430 +2013-04-30T18:18:21.025856Z +913 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +1141 + +sensor.ino +file + + + + +2013-02-26T20:22:59.166543Z +d701628f618b001192d631998f86abfc +2013-04-30T18:18:21.025856Z +913 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +4717 + diff --git a/examples/respira/.svn/text-base/product.h.svn-base b/examples/respira/.svn/text-base/product.h.svn-base new file mode 100644 index 0000000..188b492 --- /dev/null +++ b/examples/respira/.svn/text-base/product.h.svn-base @@ -0,0 +1,51 @@ +/** + * product.h + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#ifndef _PRODUCT_H +#define _PRODUCT_H + +/** + * Hardware version + */ +#define HARDWARE_VERSION 0x00000100 + +/** + * Firmware version + */ +#define FIRMWARE_VERSION 0x00000100 + +/** + * Manufacturer SWAP ID + */ +#define SWAP_MANUFACT_ID 0x00000001 + +/** + * Product SWAP ID + */ +#define SWAP_PRODUCT_ID 0x0000000D + +#endif + diff --git a/examples/respira/.svn/text-base/regtable.h.svn-base b/examples/respira/.svn/text-base/regtable.h.svn-base new file mode 100644 index 0000000..e1c9da0 --- /dev/null +++ b/examples/respira/.svn/text-base/regtable.h.svn-base @@ -0,0 +1,44 @@ +/** + * regtable.h + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#ifndef _REGTABLE_H +#define _REGTABLE_H + +#include "Arduino.h" +#include "register.h" +#include "commonregs.h" + +/** + * Register indexes + */ +DEFINE_REGINDEX_START() + REGI_VOLTSUPPLY, + REGI_HTSENSOR, + REGI_AIRSENSOR +DEFINE_REGINDEX_END() + +#endif + diff --git a/examples/respira/.svn/text-base/regtable.ino.svn-base b/examples/respira/.svn/text-base/regtable.ino.svn-base new file mode 100644 index 0000000..b670f28 --- /dev/null +++ b/examples/respira/.svn/text-base/regtable.ino.svn-base @@ -0,0 +1,138 @@ +/** + * regtable + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#include +#include "product.h" +#include "panstamp.h" +#include "regtable.h" +#include "sensor.h" + +/** + * Declaration of common callback functions + */ +DECLARE_COMMON_CALLBACKS() + +/** + * Definition of common registers + */ +DEFINE_COMMON_REGISTERS() + +/* + * Definition of custom registers + */ +// Voltage supply +static unsigned long voltageSupply = 3300; +static byte dtVoltSupply[2]; +REGISTER regVoltSupply(dtVoltSupply, sizeof(dtVoltSupply), &updtVoltSupply, NULL); +// Humidity + temperature sensor register +REGISTER regHtSensor(dtHtSensor, sizeof(dtHtSensor), &updtHtSensor, NULL); +// CO + NO2 sensor register +REGISTER regAirSensor(dtAirSensor, sizeof(dtAirSensor), &updtAirSensor, NULL); + +/** + * Initialize table of registers + */ +DECLARE_REGISTERS_START() + ®VoltSupply, + ®HtSensor, + ®AirSensor +DECLARE_REGISTERS_END() + +/** + * Definition of common getter/setter callback functions + */ +DEFINE_COMMON_CALLBACKS() + +/** + * Definition of custom getter/setter callback functions + */ + +/** + * updtVoltSupply + * + * Measure voltage supply and update register + * + * 'rId' Register ID + */ +const void updtVoltSupply(byte rId) +{ + unsigned long result; + + // Read 1.1V reference against AVcc + ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); + 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; // Back-calculate AVcc in mV + voltageSupply = result; // Update global variable Vcc + + #ifdef VOLT_SUPPLY_A7 + + // Read voltage supply from A7 + unsigned short ref = voltageSupply; + result = analogRead(7); + result *= ref; + result /= 1024; + #endif + + /** + * register[eId]->member can be replaced by regVoltSupply in this case since + * no other register is going to use "updtVoltSupply" as "updater" function + */ + + // Update register value + regTable[rId]->value[0] = (result >> 8) & 0xFF; + regTable[rId]->value[1] = result & 0xFF; +} + +/** + * updtHtSensor + * + * Read humidity + temperature sensor and update register + * + * 'rId' Register ID + */ +const void updtHtSensor(byte rId) +{ + // Read temperature and humidity from sensor + sensor_ReadTempHum(); +} + +/** + * updtAirSensor + * + * Read Co + NO2 sensor and update register + * + * 'rId' Register ID + */ +const void updtAirSensor(byte rId) +{ + // Read temperature and humidity from sensor + sensor_ReadMics4514(); +} + diff --git a/examples/respira/.svn/text-base/respira.ino.svn-base b/examples/respira/.svn/text-base/respira.ino.svn-base new file mode 100644 index 0000000..2aed2cf --- /dev/null +++ b/examples/respira/.svn/text-base/respira.ino.svn-base @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 02/08/2013 + * + * Device: + * Respira sensor for CO and NO2 + * + * Description: + * This sketch can runs on panStamp's respira sensor. This sensor measures + * humidity, temperature, CO and NO2 levels. + * + * Associated Device Definition Files, defining registers, endpoints and + * configuration parameters: + * respira.xml + */ + +#include "regtable.h" +#include "panstamp.h" + +/** + * Uncomment if you are reading Vcc from A7. All battery-boards do this + */ +//#define VOLT_SUPPLY_A7 + +/** + * LED pin + */ +#define LEDPIN 4 + +/** + * Wireless transmission interval (seconds) + */ +unsigned int txInterval; + +unsigned int loopCnt; + +/** + * setup + * + * Arduino setup function + */ +void setup() +{ + int i; + + pinMode(LEDPIN, OUTPUT); + digitalWrite(LEDPIN, LOW); + + // Init panStamp + panstamp.init(); + + /* + Serial.begin(38400); + Serial.println("Respira sensor ready!"); + */ + + // Wireless transmission interval + txInterval = panstamp.txInterval[0]; + txInterval = txInterval << 8 | panstamp.txInterval[1]; + //txInterval = 10; + + // Transmit product code + getRegister(REGI_PRODUCTCODE)->getData(); + + // Enter Rx ON state + panstamp.enterSystemState(SYSTATE_RXON); + + // Transmit periodic Tx interval + getRegister(REGI_TXINTERVAL)->getData(); + // Transmit power voltage + getRegister(REGI_VOLTSUPPLY)->getData(); + + // Initialize sensors + initSensor(); + +} + +/** + * loop + * + * Arduino main loop + */ +void loop() +{ + digitalWrite(LEDPIN, HIGH); + + // Transmit Humidity + temperaturesensor data + getRegister(REGI_HTSENSOR)->getData(); + + delay(100); + + // Transmit CO + NO2 sensor level + getRegister(REGI_AIRSENSOR)->getData(); + + digitalWrite(LEDPIN, LOW); + + for (loopCnt=0 ; loopCnt < txInterval ; loopCnt++) + delay(1000); +} + diff --git a/examples/respira/.svn/text-base/sensor.h.svn-base b/examples/respira/.svn/text-base/sensor.h.svn-base new file mode 100644 index 0000000..6373ef5 --- /dev/null +++ b/examples/respira/.svn/text-base/sensor.h.svn-base @@ -0,0 +1,68 @@ +/** + * sensor.h + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 09/01/2012 + */ + +#ifndef _SENSOR_H +#define _SENSOR_H + +/** + * Sensor values + */ +byte dtHtSensor[4]; // Humidity + temperature sensor +byte dtAirSensor[4]; // Air quality (CO + NO2) sensor + +/** + * Pin definitions + */ +// Temperature + Humidity (DHT22 sensor) +#define PORTW_DHT_DATA PORTD +#define PORTR_DHT_DATA PIND +#define PORTD_DHT_DATA DDRD +#define BIT_DHT_DATA 6 +#define PIN_DHT_DATA 6 + +#define setDataPin() bitSet(PORTW_DHT_DATA, BIT_DHT_DATA) +#define clearDataPin() bitClear(PORTW_DHT_DATA, BIT_DHT_DATA) +#define getDataPin() bitRead(PORTR_DHT_DATA, BIT_DHT_DATA) +#define setDataInput() bitClear(PORTD_DHT_DATA, BIT_DHT_DATA) +#define setDataOutput() bitSet(PORTD_DHT_DATA, BIT_DHT_DATA) + +#define PIN_PWRDHT 5 +#define dhtSensorON() digitalWrite(PIN_PWRDHT, HIGH); +#define dhtSensorOFF() digitalWrite(PIN_PWRDHT, LOW); + +// CO + NO2 (MiCS-4514 sensor) +#define ANALOG_NO2 3 // Arduino analog pin +#define ANALOG_CO 4 // Arduino analog pin +#define PIN_HEATING_NO2 19 // Arduino digital pin + +#define preHeaterON() digitalWrite(PIN_HEATING_NO2, HIGH); +#define preHeaterOFF() digitalWrite(PIN_HEATING_NO2, LOW); + +#define CALIB_R0_NO2 2200 // R0 calibration value for the NO2 sensor +#define CALIB_R0_CO 750000 // R0 calibration value for the CO sensor + +#endif + diff --git a/examples/respira/.svn/text-base/sensor.ino.svn-base b/examples/respira/.svn/text-base/sensor.ino.svn-base new file mode 100644 index 0000000..70ec1c4 --- /dev/null +++ b/examples/respira/.svn/text-base/sensor.ino.svn-base @@ -0,0 +1,234 @@ +/** + * sensor + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#include "Arduino.h" +#include "sensor.h" + +/** + * Local functions + */ +int sensor_ReadByte(void); + +/** + * sensor_ReadByte + * + * Read data byte from DHT11 sensor + */ +int sensor_ReadByte(void) +{ + byte i, result = 0; + unsigned int count = 20000; + + for(i=0; i< 8; i++) + { + while(!getDataPin()) + { + if (--count == 0) + return -1; + } + delayMicroseconds(30); + + if (getDataPin()) + result |=(1<<(7-i)); + + count = 20000; + + while(getDataPin()) + { + if (--count == 0) + return -1; + } + } + return result; +} + +/** + * sensor_ReadTempHum + * + * Read temperature and humidity values from DHT11 sensor + * + * Return -1 in case of error. Return 0 otherwise + */ +int sensor_ReadTempHum(void) +{ + int temperature, humidity, chk; + + byte dhtData[5]; + byte i, dhtCrc; + boolean success = false; + + // Power ON sensor + dhtSensorON(); + delay(1500); + + setDataOutput(); + setDataPin(); + + // Start condition + clearDataPin(); + delay(18); + setDataPin(); + delayMicroseconds(40); + setDataInput(); + delayMicroseconds(40); + + if (!getDataPin()) + { + // Start condition met + delayMicroseconds(80); + if (getDataPin()) + { + // Start condition met + delayMicroseconds(80); + + // Now ready for data reception + for (i=0; i<5; i++) + { + if ((chk = sensor_ReadByte()) < 0) + return -1; + + dhtData[i] = (byte)chk; + } + success = true; + } + } + + setDataOutput(); + clearDataPin(); + + // Power OFF sensor + dhtSensorOFF(); + + if (!success) + return -1; + + dhtCrc = dhtData[0] + dhtData[1] + dhtData[2] + dhtData[3]; + + // check check_sum + if(dhtData[4]!= dhtCrc) + return -1; // CRC error + + // Prepare values for 2-decimal format: + int sign = 1; + if (dhtData[2] & 0x80) + { + sign = -1; + dhtData[2] &= 0x7F; + } + temperature = sign * word(dhtData[2], dhtData[3]) + 500; // 50.0 ÂșC offset in order to accept negative temperatures + humidity = word(dhtData[0], dhtData[1]); + + dtHtSensor[0] = (temperature >> 8) & 0xFF; + dtHtSensor[1] = temperature & 0xFF; + dtHtSensor[2] = (humidity >> 8) & 0xFF; + dtHtSensor[3] = humidity & 0xFF; + + return 0; +} + +/** + * sensor_ReadMics4514 + * + * Read CO and NO2 levels from MiCS-4514 sensor + * + * Return -1 in case of error. Return 0 otherwise + */ +int sensor_ReadMics4514(void) +{ + unsigned int reading; + unsigned long calib; + float fVolt, fRes, fConc; + + /** + * Read CO sensor + */ + reading = analogRead(ANALOG_CO); + // Convert reading to voltage (Volts) + fVolt = (reading * 3.3) / 1024.0; + // Get Rs/R0 value + calib = CALIB_R0_CO; + fRes = (5000.0/fVolt - 1000) / calib; + + // Convert to ppm + if (fRes > 0.7) + fRes = 0.7; + if (fRes > 0.6) + fConc = (0.711 - fRes) / 0.011; + else if (fRes > 0.3) + fConc = (0.7 - fRes) / 0.01; + else + fConc = (0.3233 - fRes) / 0.00058; + + reading = fConc; + dtAirSensor[0] = (reading >> 8) & 0xFF; + dtAirSensor[1] = reading & 0xFF; + + /** + * Read NO2 sensor + */ + + reading = analogRead(ANALOG_NO2); + // Convert reading to voltage (Volts) + fVolt = 3.3; + fVolt *= reading; + fVolt /= 1024.0; + + // Get Rs/R0 value + calib = CALIB_R0_NO2; + + fRes = (5000.0/fVolt - 1000) / calib; + + // Convert to ppm + if (fRes < 3.0) + fRes = 3.0; + if (fRes >= 3.0 && fRes < 8.0) + fConc = (fRes - 0.5) / 0.25; + else + fConc = (fRes + 129.655) / 4.589; + + reading = fConc; + dtAirSensor[2] = (reading >> 8) & 0xFF; + dtAirSensor[3] = reading & 0xFF; +} + +/** + * initSensor + * + * Initialize sensor pins + */ +void initSensor(void) +{ + // DHT22 sensor + pinMode(PIN_PWRDHT, OUTPUT); // Configure Power pin as output + dhtSensorOFF(); + + // MiCS-4514 sensor + pinMode(PIN_HEATING_NO2, OUTPUT); // Configure pre-heating pin as output + preHeaterON(); + delay(30000); + preHeaterOFF(); +} + diff --git a/examples/respira/product.h b/examples/respira/product.h new file mode 100644 index 0000000..056aa4f --- /dev/null +++ b/examples/respira/product.h @@ -0,0 +1,51 @@ +/** + * product.h + * + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#ifndef _PRODUCT_H +#define _PRODUCT_H + +/** + * Hardware version + */ +#define HARDWARE_VERSION 0x00000100L + +/** + * Firmware version + */ +#define FIRMWARE_VERSION 0x00000100L + +/** + * Manufacturer SWAP ID + */ +#define SWAP_MANUFACT_ID 0x00000001L + +/** + * Product SWAP ID + */ +#define SWAP_PRODUCT_ID 0x0000000DL + +#endif + diff --git a/examples/respira/regtable.h b/examples/respira/regtable.h new file mode 100644 index 0000000..6dcbe3b --- /dev/null +++ b/examples/respira/regtable.h @@ -0,0 +1,44 @@ +/** + * regtable.h + * + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#ifndef _REGTABLE_H +#define _REGTABLE_H + +#include "Arduino.h" +#include "register.h" +#include "commonregs.h" + +/** + * Register indexes + */ +DEFINE_REGINDEX_START() + REGI_VOLTSUPPLY, + REGI_HTSENSOR, + REGI_AIRSENSOR +DEFINE_REGINDEX_END() + +#endif + diff --git a/examples/respira/regtable.ino b/examples/respira/regtable.ino new file mode 100644 index 0000000..2902a2e --- /dev/null +++ b/examples/respira/regtable.ino @@ -0,0 +1,107 @@ +/** + * regtable + * + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#include "product.h" +#include "regtable.h" +#include "sensor.h" + +/** + * Definition of common registers + */ +DEFINE_COMMON_REGISTERS() + +/* + * Definition of custom registers + */ +// Voltage supply +static unsigned long voltageSupply = 3300; +static byte dtVoltSupply[2]; +REGISTER regVoltSupply(dtVoltSupply, sizeof(dtVoltSupply), &updtVoltSupply, NULL); +// Humidity + temperature sensor register +REGISTER regHtSensor(dtHtSensor, sizeof(dtHtSensor), &updtHtSensor, NULL); +// CO + NO2 sensor register +REGISTER regAirSensor(dtAirSensor, sizeof(dtAirSensor), &updtAirSensor, NULL); + +/** + * Initialize table of registers + */ +DECLARE_REGISTERS_START() + ®VoltSupply, + ®HtSensor, + ®AirSensor +DECLARE_REGISTERS_END() + +/** + * Definition of common getter/setter callback functions + */ +DEFINE_COMMON_CALLBACKS() + +/** + * Definition of custom getter/setter callback functions + */ + +/** + * updtVoltSupply + * + * Measure voltage supply and update register + * + * 'rId' Register ID + */ +const void updtVoltSupply(byte rId) +{ + unsigned long result = panstamp.getVcc(); + + // Update register value + regTable[rId]->value[0] = (result >> 8) & 0xFF; + regTable[rId]->value[1] = result & 0xFF; +} + +/** + * updtHtSensor + * + * Read humidity + temperature sensor and update register + * + * 'rId' Register ID + */ +const void updtHtSensor(byte rId) +{ + // Read temperature and humidity from sensor + sensor_ReadTempHum(); +} + +/** + * updtAirSensor + * + * Read Co + NO2 sensor and update register + * + * 'rId' Register ID + */ +const void updtAirSensor(byte rId) +{ + // Read temperature and humidity from sensor + sensor_ReadMics4514(); +} + diff --git a/examples/respira/respira.ino b/examples/respira/respira.ino new file mode 100644 index 0000000..f4149e4 --- /dev/null +++ b/examples/respira/respira.ino @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 02/08/2013 + * + * Device: + * Respira sensor for CO and NO2 + * + * Description: + * This sketch can runs on panStamp's respira sensor. This sensor measures + * humidity, temperature, CO and NO2 levels. + * + * Associated Device Definition Files, defining registers, endpoints and + * configuration parameters: + * respira.xml + */ + +#include "regtable.h" +#include "swap.h" +#include "DHT.h" + +/** + * LED pin + */ +#ifdef ONBOARD_LED +#define LEDPIN ONBOARD_LED +#else +#define LEDPIN 4 +#endif + +unsigned int loopCnt; + +/** + * setup + * + * Arduino setup function + */ +void setup() +{ + int i; + + pinMode(LEDPIN, OUTPUT); + digitalWrite(LEDPIN, LOW); + + // Init panStamp + panstamp.init(); + + // Init SWAP stack + swap.init(); + + /* + Serial.begin(38400); + Serial.println("Respira sensor ready!"); + */ + + // Transmit product code + swap.getRegister(REGI_PRODUCTCODE)->getData(); + + // Enter Rx ON state + swap.enterSystemState(SYSTATE_RXON); + + // Transmit periodic Tx interval + swap.getRegister(REGI_TXINTERVAL)->getData(); + // Transmit power voltage + swap.getRegister(REGI_VOLTSUPPLY)->getData(); + + // Initialize sensors + initSensor(); +} + +/** + * loop + * + * Arduino main loop + */ +void loop() +{ + digitalWrite(LEDPIN, HIGH); + + // Transmit Humidity + temperaturesensor data + swap.getRegister(REGI_HTSENSOR)->getData(); + + delay(100); + + // Transmit CO + NO2 sensor level + swap.getRegister(REGI_AIRSENSOR)->getData(); + + digitalWrite(LEDPIN, LOW); + + // Wait txInterval seconds + for (loopCnt=0 ; loopCnt < swap.txInterval ; loopCnt++) + delay(1000); +} + diff --git a/examples/respira/sensor.h b/examples/respira/sensor.h new file mode 100644 index 0000000..0ef24ff --- /dev/null +++ b/examples/respira/sensor.h @@ -0,0 +1,56 @@ +/** + * sensor.h + * + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 09/01/2012 + */ + +#ifndef _SENSOR_H +#define _SENSOR_H + +/** + * Sensor values + */ +byte dtHtSensor[4]; // Humidity + temperature sensor +byte dtAirSensor[4]; // Air quality (CO + NO2) sensor + +/** + * Pin definitions + */ +// Temperature + Humidity (DHT22 sensor) +#define DHTPIN 14 +DHT dht(DHTPIN, DHT22); +#define PWRPIN 15 + +// CO + NO2 (MiCS-4514 sensor) +#define ANALOG_NO2 3 // Arduino analog pin +#define ANALOG_CO 4 // Arduino analog pin +#define PIN_HEATING_NO2 19 // Arduino digital pin + +#define preHeaterON() digitalWrite(PIN_HEATING_NO2, HIGH); +#define preHeaterOFF() digitalWrite(PIN_HEATING_NO2, LOW); + +#define CALIB_R0_NO2 2200 // R0 calibration value for the NO2 sensor +#define CALIB_R0_CO 750000 // R0 calibration value for the CO sensor + +#endif + diff --git a/examples/respira/sensor.ino b/examples/respira/sensor.ino new file mode 100644 index 0000000..b8dd8c5 --- /dev/null +++ b/examples/respira/sensor.ino @@ -0,0 +1,135 @@ +/** + * sensor + * + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#include "Arduino.h" +#include "sensor.h" + +/** + * sensor_ReadTempHum + * + * Read temperature and humidity values from DHT11 sensor + * + * Return -1 in case of error. Return 0 otherwise + */ +int sensor_ReadTempHum(void) +{ + // Read humidity + float h = dht.readHumidity(); + // Read temperature + float t = dht.readTemperature(); + // Unpower sensor + digitalWrite(PWRPIN, LOW); + + uint16_t humidity = h * 10; + uint16_t temperature = (t + 50) * 10; + + dtHtSensor[0] = (temperature >> 8) & 0xFF; + dtHtSensor[1] = temperature & 0xFF; + dtHtSensor[2] = (humidity >> 8) & 0xFF; + dtHtSensor[3] = humidity & 0xFF; + + return 0; +} + +/** + * sensor_ReadMics4514 + * + * Read CO and NO2 levels from MiCS-4514 sensor + * + * Return -1 in case of error. Return 0 otherwise + */ +int sensor_ReadMics4514(void) +{ + unsigned int reading; + unsigned long calib; + float fVolt, fRes, fConc; + + /** + * Read CO sensor + */ + reading = analogRead(ANALOG_CO); + // Convert reading to voltage (Volts) + fVolt = (reading * 3.3) / 1024.0; + // Get Rs/R0 value + calib = CALIB_R0_CO; + fRes = (5000.0/fVolt - 1000) / calib; + + // Convert to ppm + if (fRes > 0.7) + fRes = 0.7; + if (fRes > 0.6) + fConc = (0.711 - fRes) / 0.011; + else if (fRes > 0.3) + fConc = (0.7 - fRes) / 0.01; + else + fConc = (0.3233 - fRes) / 0.00058; + + reading = fConc; + dtAirSensor[0] = (reading >> 8) & 0xFF; + dtAirSensor[1] = reading & 0xFF; + + /** + * Read NO2 sensor + */ + + reading = analogRead(ANALOG_NO2); + // Convert reading to voltage (Volts) + fVolt = 3.3; + fVolt *= reading; + fVolt /= 1024.0; + + // Get Rs/R0 value + calib = CALIB_R0_NO2; + + fRes = (5000.0/fVolt - 1000) / calib; + + // Convert to ppm + if (fRes < 3.0) + fRes = 3.0; + if (fRes >= 3.0 && fRes < 8.0) + fConc = (fRes - 0.5) / 0.25; + else + fConc = (fRes + 129.655) / 4.589; + + reading = fConc; + dtAirSensor[2] = (reading >> 8) & 0xFF; + dtAirSensor[3] = reading & 0xFF; +} + +/** + * initSensor + * + * Initialize sensor pins + */ +void initSensor(void) +{ + // MiCS-4514 sensor + pinMode(PIN_HEATING_NO2, OUTPUT); // Configure pre-heating pin as output + preHeaterON(); + delay(30000); + preHeaterOFF(); +} + diff --git a/examples/rgbdriver/.svn/all-wcprops b/examples/rgbdriver/.svn/all-wcprops new file mode 100644 index 0000000..74d5445 --- /dev/null +++ b/examples/rgbdriver/.svn/all-wcprops @@ -0,0 +1,41 @@ +K 25 +svn:wc:ra_dav:version-url +V 50 +/svn/!svn/ver/637/trunk/arduino/sketches/rgbdriver +END +rgbled.cpp +K 25 +svn:wc:ra_dav:version-url +V 61 +/svn/!svn/ver/915/trunk/arduino/sketches/rgbdriver/rgbled.cpp +END +rgbled.h +K 25 +svn:wc:ra_dav:version-url +V 59 +/svn/!svn/ver/637/trunk/arduino/sketches/rgbdriver/rgbled.h +END +product.h +K 25 +svn:wc:ra_dav:version-url +V 60 +/svn/!svn/ver/637/trunk/arduino/sketches/rgbdriver/product.h +END +regtable.ino +K 25 +svn:wc:ra_dav:version-url +V 63 +/svn/!svn/ver/637/trunk/arduino/sketches/rgbdriver/regtable.ino +END +rgbdriver.ino +K 25 +svn:wc:ra_dav:version-url +V 64 +/svn/!svn/ver/915/trunk/arduino/sketches/rgbdriver/rgbdriver.ino +END +regtable.h +K 25 +svn:wc:ra_dav:version-url +V 61 +/svn/!svn/ver/637/trunk/arduino/sketches/rgbdriver/regtable.h +END diff --git a/examples/rgbdriver/.svn/entries b/examples/rgbdriver/.svn/entries new file mode 100644 index 0000000..eb11b6e --- /dev/null +++ b/examples/rgbdriver/.svn/entries @@ -0,0 +1,232 @@ +10 + +dir +770 +https://panstamp.googlecode.com/svn/trunk/arduino/sketches/rgbdriver +https://panstamp.googlecode.com/svn + + + +2012-07-24T14:28:42.926043Z +637 +dberenguer@usapiens.com + + + + + + + + + + + + + + +d77f469f-0316-7e13-d0a9-da7f422f7f4c + +rgbled.cpp +file +915 + + + +2013-02-23T18:16:51.867933Z +23c16fcc7ffaab7cb59d36ac4495b3a7 +2013-04-30T18:25:13.020037Z +915 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +1647 + +rgbled.h +file + + + + +2012-07-26T09:30:27.000000Z +264771f0a4573d8011dd978401f66c4a +2012-07-24T14:28:42.926043Z +637 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +1838 + +product.h +file + + + + +2012-07-26T09:30:27.000000Z +3a58a0876f3e75e2ed7ee8f3a5e76d30 +2012-07-24T14:28:42.926043Z +637 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +1235 + +regtable.ino +file + + + + +2012-07-26T09:30:27.000000Z +31066f6243854e2e577f56f1292bb30c +2012-07-24T14:28:42.926043Z +637 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +1867 + +rgbdriver.ino +file +915 + + + +2013-02-23T18:16:51.843933Z +bff8ead6d71762795460b54c2f807735 +2013-04-30T18:25:13.020037Z +915 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +2924 + +regtable.h +file + + + + +2012-07-26T09:30:27.000000Z +a970a48380ae75eb340b29dc9d921f5b +2012-07-24T14:28:42.926043Z +637 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +1120 + diff --git a/examples/rgbdriver/.svn/text-base/product.h.svn-base b/examples/rgbdriver/.svn/text-base/product.h.svn-base new file mode 100644 index 0000000..03fcccc --- /dev/null +++ b/examples/rgbdriver/.svn/text-base/product.h.svn-base @@ -0,0 +1,51 @@ +/** + * product.h + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#ifndef _PRODUCT_H +#define _PRODUCT_H + +/** + * Hardware version + */ +#define HARDWARE_VERSION 0x00000100 + +/** + * Firmware version + */ +#define FIRMWARE_VERSION 0x00000100 + +/** + * Manufacturer SWAP ID + */ +#define SWAP_MANUFACT_ID 0x00000001 + +/** + * Product SWAP ID + */ +#define SWAP_PRODUCT_ID 0x00000003 + +#endif + diff --git a/examples/rgbdriver/.svn/text-base/regtable.h.svn-base b/examples/rgbdriver/.svn/text-base/regtable.h.svn-base new file mode 100644 index 0000000..ef87cf8 --- /dev/null +++ b/examples/rgbdriver/.svn/text-base/regtable.h.svn-base @@ -0,0 +1,43 @@ +/** + * regtable.h + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#ifndef _REGTABLE_H +#define _REGTABLE_H + +#include "Arduino.h" +#include "register.h" +#include "commonregs.h" + +/** + * Register indexes + */ + +DEFINE_COMMON_REGINDEX_START() + REGI_RGBLEVELS +DEFINE_COMMON_REGINDEX_END() + +#endif + diff --git a/examples/rgbdriver/.svn/text-base/regtable.ino.svn-base b/examples/rgbdriver/.svn/text-base/regtable.ino.svn-base new file mode 100644 index 0000000..9cf5e94 --- /dev/null +++ b/examples/rgbdriver/.svn/text-base/regtable.ino.svn-base @@ -0,0 +1,81 @@ +/** + * regtable.pde + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#include +#include "product.h" +#include "panstamp.h" +#include "regtable.h" + +/** + * Declaration of common callback functions + */ +DECLARE_COMMON_CALLBACKS() + +/** + * Definition of common registers + */ +DEFINE_COMMON_REGISTERS(); + +/* + * Definition of custom registers + */ +// RGB levels +static byte dtRGBlevel[3]; +REGISTER regRGBlevels(dtRGBlevel, sizeof(dtRGBlevel), NULL, &setRGBlevel); + +/** + * Initialize table of registers + */ +DECLARE_REGISTERS_START() + ®RGBlevels +DECLARE_REGISTERS_END() + +/** + * Definition of common getter/setter callback functions + */ +DEFINE_COMMON_CALLBACKS() + +/** + * Definition of custom getter/setter callback functions + */ + +/** + * setRGBlevel + * + * Set RGB color levels + * + * 'rId' Register ID + * 'levels' New RGB levels + */ +const void setRGBlevel(byte rId, byte *levels) +{ + // Update register + memcpy(dtRGBlevel, levels, sizeof(dtRGBlevel)); + + // Control RGB LED + rgbLed.setColor(levels[0], levels[1], levels[2]); +} + diff --git a/examples/rgbdriver/.svn/text-base/rgbdriver.ino.svn-base b/examples/rgbdriver/.svn/text-base/rgbdriver.ino.svn-base new file mode 100644 index 0000000..e4d15c1 --- /dev/null +++ b/examples/rgbdriver/.svn/text-base/rgbdriver.ino.svn-base @@ -0,0 +1,110 @@ +/** + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 06/22/2011 + * + * Device: + * RGB LED driver + * + * Description: + * This device controls a RGB LED or a RGB LED strip using three PWM + * outputs. RGB levels can be controlled via the RGBlevel register + * (see Device Definition File for details). At the same time, this device + * listens for the first Chronos watch appearing in the network and + * synchronizes the RGB levels with the Chronos XYZ acceleration samples. + * + * Associated Device Definition File: rgbdriver.xml + */ + +#include "panstamp.h" +#include "regtable.h" +#include "rgbled.h" + +#define CHRONOS_PRODUCT_CODE {0,0,0,1,0,0,0,2} +#define CHRONOS_REGID_ACC 14 + +// Global RGBLED object. Define your own PWM outputs here: +RGBLED rgbLed = RGBLED(9, 6, 5); + +// Address of the associated Chronos watch +byte chronosDevAddr = 0; + +/** + * swapStatusReceived + * + * Function automatically called by the panStamp API whenever a SWAP status + * packet is received + * + * 'status' SWAP status packet received + */ + void swapStatusReceived(SWPACKET *status) + { + byte chronos_pCode[] = CHRONOS_PRODUCT_CODE; + + // Still without an associated Chronos watch? + if (chronosDevAddr == 0) + { + // Product code received? + if (status->regId == REGI_PRODUCTCODE) + { + // Is this a Chronos watch? + if (!memcmp(status->value.data, chronos_pCode, sizeof(chronos_pCode))) + chronosDevAddr = status->srcAddr; // Save its device address + } + } + // We already have an associated Chronos watch + else + { + // Acceleration data received? + if (status->regId == CHRONOS_REGID_ACC) + { + // Update RGB levels + getRegister(REGI_RGBLEVELS)->setData(status->value.data); + } + } + } + +/** + * setup + * + * One-time setup function + */ +void setup() +{ + // Init panStamp + panstamp.init(); + + // Declare callback function for dispatching the incoming SWAP status packets + panstamp.setSwapStatusCallBack(swapStatusReceived); + + // Broadcast product status + getRegister(REGI_PRODUCTCODE)->getData(); +} + +/** + * loop + * + * Main loop + */ +void loop() +{ + delay(100); +} diff --git a/examples/rgbdriver/.svn/text-base/rgbled.cpp.svn-base b/examples/rgbdriver/.svn/text-base/rgbled.cpp.svn-base new file mode 100644 index 0000000..d66b1d7 --- /dev/null +++ b/examples/rgbdriver/.svn/text-base/rgbled.cpp.svn-base @@ -0,0 +1,62 @@ +/** + * rgbled.pde + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 06/22/2011 + */ + +#include "rgbled.h" + +/** + * Class constructor + * + * 'redPin' Arduino pin connected to the red LED + * 'greenPin' Arduino pin connected to the green LED + * 'bluePin' Arduino pin connected to the blue LED +*/ +RGBLED::RGBLED(int redPin, int greenPin, int bluePin) +{ + pRed = redPin; + pGreen = greenPin; + pBlue = bluePin; + + pinMode(pRed, OUTPUT); + pinMode(pGreen, OUTPUT); + pinMode(pBlue, OUTPUT); +} + +/** + * setColor + * + * Set color levels on the RGB LED + * + * 'red' Bright level of the red LED + * 'green' Bright level of the green LED + * 'blue' Bright level of the blue LED + */ +void RGBLED::setColor(byte red, byte green, byte blue) +{ + analogWrite(pRed, red); + analogWrite(pGreen, green); + analogWrite(pBlue, blue); +} + diff --git a/examples/rgbdriver/.svn/text-base/rgbled.h.svn-base b/examples/rgbdriver/.svn/text-base/rgbled.h.svn-base new file mode 100644 index 0000000..15b5e38 --- /dev/null +++ b/examples/rgbdriver/.svn/text-base/rgbled.h.svn-base @@ -0,0 +1,77 @@ +/** + * rgbled.h + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 06/22/2011 + */ + +#ifndef _RGBLED_H +#define _RGBLED_H + +#include "Arduino.h" + +/** + * Macros + */ +#define turnOff() setColor(0, 0, 0) + +class RGBLED +{ + private: + /** + * Arduino pin connected to the red LED + */ + int pRed; + + /** + * Arduino pin connected to the green LED + */ + int pGreen; + + /** + * Arduino pin connected to the blue LED + */ + int pBlue; + + public: + /** + * Class constructor + * + * 'redPin' Arduino pin connected to the red LED + * 'greenPin' Arduino pin connected to the green LED + * 'bluePin' Arduino pin connected to the blue LED + */ + RGBLED(int redPin, int greenPin, int bluePin); + + /** + * setColor + * + * Set color levels on the RGB LED + * + * 'red' Bright level of the red LED + * 'green' Bright level of the green LED + * 'blue' Bright level of the blue LED + */ + void setColor(byte red, byte green, byte blue); +}; + +#endif diff --git a/examples/rgbdriver/product.h b/examples/rgbdriver/product.h new file mode 100644 index 0000000..a83bcfc --- /dev/null +++ b/examples/rgbdriver/product.h @@ -0,0 +1,51 @@ +/** + * product.h + * + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#ifndef _PRODUCT_H +#define _PRODUCT_H + +/** + * Hardware version + */ +#define HARDWARE_VERSION 0x00000100L + +/** + * Firmware version + */ +#define FIRMWARE_VERSION 0x00000100L + +/** + * Manufacturer SWAP ID + */ +#define SWAP_MANUFACT_ID 0x00000001L + +/** + * Product SWAP ID + */ +#define SWAP_PRODUCT_ID 0x00000003L + +#endif + diff --git a/examples/rgbdriver/regtable.h b/examples/rgbdriver/regtable.h new file mode 100644 index 0000000..cd5bbfc --- /dev/null +++ b/examples/rgbdriver/regtable.h @@ -0,0 +1,43 @@ +/** + * regtable.h + * + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#ifndef _REGTABLE_H +#define _REGTABLE_H + +#include "Arduino.h" +#include "register.h" +#include "commonregs.h" + +/** + * Register indexes + */ + +DEFINE_COMMON_REGINDEX_START() + REGI_RGBLEVELS +DEFINE_COMMON_REGINDEX_END() + +#endif + diff --git a/examples/rgbdriver/regtable.ino b/examples/rgbdriver/regtable.ino new file mode 100644 index 0000000..f5733bd --- /dev/null +++ b/examples/rgbdriver/regtable.ino @@ -0,0 +1,76 @@ +/** + * regtable.pde + * + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#include "product.h" +#include "regtable.h" + +/** + * Definition of common registers + */ +DEFINE_COMMON_REGISTERS(); + +const void setRGBlevel(byte rId, byte *levels); + +/* + * Definition of custom registers + */ +// RGB levels +static byte dtRGBlevel[3]; +REGISTER regRGBlevels(dtRGBlevel, sizeof(dtRGBlevel), NULL, &setRGBlevel); + +/** + * Initialize table of registers + */ +DECLARE_REGISTERS_START() + ®RGBlevels +DECLARE_REGISTERS_END() + +/** + * Definition of common getter/setter callback functions + */ +DEFINE_COMMON_CALLBACKS() + +/** + * Definition of custom getter/setter callback functions + */ + +/** + * setRGBlevel + * + * Set RGB color levels + * + * 'rId' Register ID + * 'levels' New RGB levels + */ +const void setRGBlevel(byte rId, byte *levels) +{ + // Update register + memcpy(dtRGBlevel, levels, sizeof(dtRGBlevel)); + + // Control RGB LED + rgbLed.setColor(levels[0], levels[1], levels[2]); +} + diff --git a/examples/rgbdriver/rgbdriver.ino b/examples/rgbdriver/rgbdriver.ino new file mode 100644 index 0000000..8e596aa --- /dev/null +++ b/examples/rgbdriver/rgbdriver.ino @@ -0,0 +1,115 @@ +/** + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 06/22/2011 + * + * Device: + * RGB LED driver + * + * Description: + * This device controls a RGB LED or a RGB LED strip using three PWM + * outputs. RGB levels can be controlled via the RGBlevel register + * (see Device Definition File for details). At the same time, this device + * listens for the first Chronos watch appearing in the network and + * synchronizes the RGB levels with the Chronos XYZ acceleration samples. + * + * Associated Device Definition File: rgbdriver.xml + */ + +#include "swap.h" +#include "regtable.h" +#include "rgbled.h" + +#define CHRONOS_PRODUCT_CODE {0,0,0,1,0,0,0,2} +#define CHRONOS_REGID_ACC 14 + +// Global RGBLED object. Define your own PWM outputs here (Arduino digital pins): +#ifdef PANSTAMP_AVR +RGBLED rgbLed = RGBLED(9, 6, 5); +#endif +#ifdef PANSTAMP_NRG +RGBLED rgbLed = RGBLED(17, 2, 3); +#endif + +// Address of the associated Chronos watch +byte chronosDevAddr = 0; + +/** + * swapStatusReceived + * + * Function automatically called by the panStamp API whenever a SWAP status + * packet is received + * + * 'status' SWAP status packet received + */ + void swapStatusReceived(SWPACKET *status) + { + byte chronos_pCode[] = CHRONOS_PRODUCT_CODE; + + // Still without an associated Chronos watch? + if (chronosDevAddr == 0) + { + // Product code received? + if (status->regId == REGI_PRODUCTCODE) + { + // Is this a Chronos watch? + if (!memcmp(status->value.data, chronos_pCode, sizeof(chronos_pCode))) + chronosDevAddr = status->srcAddr; // Save its device address + } + } + // We already have an associated Chronos watch + else + { + // Acceleration data received? + if (status->regId == CHRONOS_REGID_ACC) + { + // Update RGB levels + swap.getRegister(REGI_RGBLEVELS)->setData(status->value.data); + } + } + } + +/** + * setup + * + * One-time setup function + */ +void setup() +{ + // Init SWAP stack + swap.init(); + + // Declare callback function for dispatching incoming SWAP status packets + swap.attachInterrupt(STATUS, swapStatusReceived); + + // Broadcast product status + swap.getRegister(REGI_PRODUCTCODE)->getData(); +} + +/** + * loop + * + * Main loop + */ +void loop() +{ + delay(100); +} diff --git a/examples/rgbdriver/rgbled.cpp b/examples/rgbdriver/rgbled.cpp new file mode 100644 index 0000000..1f19da0 --- /dev/null +++ b/examples/rgbdriver/rgbled.cpp @@ -0,0 +1,62 @@ +/** + * rgbled.pde + * + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 06/22/2011 + */ + +#include "rgbled.h" + +/** + * Class constructor + * + * 'redPin' Arduino pin connected to the red LED + * 'greenPin' Arduino pin connected to the green LED + * 'bluePin' Arduino pin connected to the blue LED +*/ +RGBLED::RGBLED(int redPin, int greenPin, int bluePin) +{ + pRed = redPin; + pGreen = greenPin; + pBlue = bluePin; + + pinMode(pRed, OUTPUT); + pinMode(pGreen, OUTPUT); + pinMode(pBlue, OUTPUT); +} + +/** + * setColor + * + * Set color levels on the RGB LED + * + * 'red' Bright level of the red LED + * 'green' Bright level of the green LED + * 'blue' Bright level of the blue LED + */ +void RGBLED::setColor(byte red, byte green, byte blue) +{ + analogWrite(pRed, red); + analogWrite(pGreen, green); + analogWrite(pBlue, blue); +} + diff --git a/examples/rgbdriver/rgbled.h b/examples/rgbdriver/rgbled.h new file mode 100644 index 0000000..be705f0 --- /dev/null +++ b/examples/rgbdriver/rgbled.h @@ -0,0 +1,77 @@ +/** + * rgbled.h + * + * Copyright (c) 2014 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 06/22/2011 + */ + +#ifndef _RGBLED_H +#define _RGBLED_H + +#include "Arduino.h" + +/** + * Macros + */ +#define turnOff() setColor(0, 0, 0) + +class RGBLED +{ + private: + /** + * Arduino pin connected to the red LED + */ + int pRed; + + /** + * Arduino pin connected to the green LED + */ + int pGreen; + + /** + * Arduino pin connected to the blue LED + */ + int pBlue; + + public: + /** + * Class constructor + * + * 'redPin' Arduino pin connected to the red LED + * 'greenPin' Arduino pin connected to the green LED + * 'bluePin' Arduino pin connected to the blue LED + */ + RGBLED(int redPin, int greenPin, int bluePin); + + /** + * setColor + * + * Set color levels on the RGB LED + * + * 'red' Bright level of the red LED + * 'green' Bright level of the green LED + * 'blue' Bright level of the blue LED + */ + void setColor(byte red, byte green, byte blue); +}; + +#endif diff --git a/examples/soilmoisture/.svn/all-wcprops b/examples/soilmoisture/.svn/all-wcprops new file mode 100644 index 0000000..c2c27e4 --- /dev/null +++ b/examples/soilmoisture/.svn/all-wcprops @@ -0,0 +1,29 @@ +K 25 +svn:wc:ra_dav:version-url +V 53 +/svn/!svn/ver/911/trunk/arduino/sketches/soilmoisture +END +product.h +K 25 +svn:wc:ra_dav:version-url +V 63 +/svn/!svn/ver/911/trunk/arduino/sketches/soilmoisture/product.h +END +regtable.ino +K 25 +svn:wc:ra_dav:version-url +V 66 +/svn/!svn/ver/911/trunk/arduino/sketches/soilmoisture/regtable.ino +END +regtable.h +K 25 +svn:wc:ra_dav:version-url +V 64 +/svn/!svn/ver/911/trunk/arduino/sketches/soilmoisture/regtable.h +END +soilmoisture.ino +K 25 +svn:wc:ra_dav:version-url +V 70 +/svn/!svn/ver/911/trunk/arduino/sketches/soilmoisture/soilmoisture.ino +END diff --git a/examples/soilmoisture/.svn/entries b/examples/soilmoisture/.svn/entries new file mode 100644 index 0000000..bf0e2ea --- /dev/null +++ b/examples/soilmoisture/.svn/entries @@ -0,0 +1,164 @@ +10 + +dir +911 +https://panstamp.googlecode.com/svn/trunk/arduino/sketches/soilmoisture +https://panstamp.googlecode.com/svn + + + +2013-04-30T18:13:43.004908Z +911 +dberenguer@usapiens.com + + + + + + + + + + + + + + +d77f469f-0316-7e13-d0a9-da7f422f7f4c + +product.h +file + + + + +2013-04-29T15:43:41.474588Z +3f03b073a78ce18ac552608ab9e9d21a +2013-04-30T18:13:43.004908Z +911 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +1233 + +regtable.ino +file + + + + +2013-04-30T17:49:06.286847Z +4b4268158c0936839bcfe2d29aeeecad +2013-04-30T18:13:43.004908Z +911 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +3384 + +regtable.h +file + + + + +2013-04-30T15:35:41.170872Z +1723fd148ac89f45332e08e43b432773 +2013-04-30T18:13:43.004908Z +911 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +1139 + +soilmoisture.ino +file + + + + +2013-04-30T18:12:58.434843Z +d14bcf31021c107617eb65b18ec7dafc +2013-04-30T18:13:43.004908Z +911 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +2924 + diff --git a/examples/soilmoisture/.svn/text-base/product.h.svn-base b/examples/soilmoisture/.svn/text-base/product.h.svn-base new file mode 100644 index 0000000..b7fb778 --- /dev/null +++ b/examples/soilmoisture/.svn/text-base/product.h.svn-base @@ -0,0 +1,51 @@ +/** + * product.h + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 04/29/2013 + */ + +#ifndef _PRODUCT_H +#define _PRODUCT_H + +/** + * Hardware version + */ +#define HARDWARE_VERSION 0x00000100 + +/** + * Firmware version + */ +#define FIRMWARE_VERSION 0x00000100 + +/** + * Manufacturer SWAP ID + */ +#define SWAP_MANUFACT_ID 0x00000001 + +/** + * Product SWAP ID + */ +#define SWAP_PRODUCT_ID 0x0000000E + +#endif + diff --git a/examples/soilmoisture/.svn/text-base/regtable.h.svn-base b/examples/soilmoisture/.svn/text-base/regtable.h.svn-base new file mode 100644 index 0000000..edbb8fc --- /dev/null +++ b/examples/soilmoisture/.svn/text-base/regtable.h.svn-base @@ -0,0 +1,44 @@ +/** + * regtable.h + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 04/29/2013 + */ + +#ifndef _REGTABLE_H +#define _REGTABLE_H + +#include "Arduino.h" +#include "register.h" +#include "commonregs.h" + +/** + * Register indexes + */ +DEFINE_REGINDEX_START() + REGI_VOLTSUPPLY, + REGI_SENSOR0, + REGI_SENSOR1, +DEFINE_REGINDEX_END() + +#endif + diff --git a/examples/soilmoisture/.svn/text-base/regtable.ino.svn-base b/examples/soilmoisture/.svn/text-base/regtable.ino.svn-base new file mode 100644 index 0000000..4d2df92 --- /dev/null +++ b/examples/soilmoisture/.svn/text-base/regtable.ino.svn-base @@ -0,0 +1,137 @@ +/** + * regtable + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#include +#include "product.h" +#include "panstamp.h" +#include "regtable.h" + +/** + * Declaration of common callback functions + */ +DECLARE_COMMON_CALLBACKS() + +/** + * Definition of common registers + */ +DEFINE_COMMON_REGISTERS() + +/* + * Definition of custom registers + */ +// Voltage supply +static unsigned long voltageSupply = 3300; +static byte dtVoltSupply[2]; +REGISTER regVoltSupply(dtVoltSupply, sizeof(dtVoltSupply), &updtVoltSupply, NULL); +// Sensor value register (dual sensor) +static byte dtSensor[4]; +REGISTER regSensor(dtSensor, sizeof(dtSensor), &updtSensor, NULL); + +/** + * Initialize table of registers + */ +DECLARE_REGISTERS_START() + ®VoltSupply, + ®Sensor +DECLARE_REGISTERS_END() + +/** + * Definition of common getter/setter callback functions + */ +DEFINE_COMMON_CALLBACKS() + +/** + * Definition of custom getter/setter callback functions + */ + +/** + * updtVoltSupply + * + * Measure voltage supply and update register + * + * 'rId' Register ID + */ +const void updtVoltSupply(byte rId) +{ + unsigned long result; + + // Read 1.1V reference against AVcc + ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); + 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; // Back-calculate AVcc in mV + voltageSupply = result; // Update global variable Vcc + + #ifdef VOLT_SUPPLY_A7 + + // Read voltage supply from A7 + unsigned short ref = voltageSupply; + result = analogRead(7); + result *= ref; + result /= 1024; + #endif + + /** + * register[eId]->member can be replaced by regVoltSupply in this case since + * no other register is going to use "updtVoltSupply" as "updater" function + */ + + // Update register value + regTable[rId]->value[0] = (result >> 8) & 0xFF; + regTable[rId]->value[1] = result & 0xFF; +} + +/** + * updtSensor + * + * Measure sensor data and update register + * + * 'rId' Register ID + */ +const void updtSensor(byte rId) +{ + // Power sensors + digitalWrite(POWER_0_PIN, HIGH); + digitalWrite(POWER_1_PIN, HIGH); + delay(10); + // Read analog values + unsigned int adcValue0 = analogRead(SENSOR_0_PIN); + unsigned int adcValue1 = analogRead(SENSOR_1_PIN); + // Unpower sensors + digitalWrite(POWER_0_PIN, LOW); + digitalWrite(POWER_1_PIN, LOW); + + // Update register value + dtSensor[0] = (adcValue0 >> 8) & 0xFF; + dtSensor[1] = adcValue0 & 0xFF; + dtSensor[2] = (adcValue1 >> 8) & 0xFF; + dtSensor[3] = adcValue1 & 0xFF; +} + + diff --git a/examples/soilmoisture/.svn/text-base/soilmoisture.ino.svn-base b/examples/soilmoisture/.svn/text-base/soilmoisture.ino.svn-base new file mode 100644 index 0000000..b427570 --- /dev/null +++ b/examples/soilmoisture/.svn/text-base/soilmoisture.ino.svn-base @@ -0,0 +1,122 @@ +/* + * soilmoisture + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 04/29/2013 + * + * Device: + * Soil Moisture sensor + * + * Description: + * This application measures soil moisture from any two sensor providing an + * analog signal + * + * These devices are low-power enabled so they will enter low-power mode + * just after reading the sensor values and transmitting them over the + * SWAP network. + * + * Associated Device Definition File, defining registers, endpoints and + * configuration parameters: + * soilmoisture.xml + */ + +#include "regtable.h" +#include "panstamp.h" + +/** + * Uncomment if you are reading Vcc from A7. All battery-boards do this + */ +#define VOLT_SUPPLY_A7 + +/** + * LED pin + */ +#define LEDPIN 4 + +/** + * Sensor pins + */ +#define SENSOR_0_PIN 3 // Analog pin - sensor 0 +#define POWER_0_PIN 16 // Digital pin used to powwer sensor 0 +#define SENSOR_1_PIN 5 // Analog pin - sensor 1 +#define POWER_1_PIN 18 // Digital pin used to powwer sensor 1 + +/** + * setup + * + * Arduino setup function + */ +void setup() +{ + int i; + + pinMode(LEDPIN, OUTPUT); + digitalWrite(LEDPIN, LOW); + + // Initialize power pins + pinMode(POWER_0_PIN, OUTPUT); + digitalWrite(POWER_0_PIN, LOW); + pinMode(POWER_1_PIN, OUTPUT); + digitalWrite(POWER_1_PIN, LOW); + + // Init panStamp + panstamp.init(); + + // Transmit product code + getRegister(REGI_PRODUCTCODE)->getData(); + + // Enter SYNC state + panstamp.enterSystemState(SYSTATE_SYNC); + + // During 3 seconds, listen the network for possible commands whilst the LED blinks + for(i=0 ; i<6 ; i++) + { + digitalWrite(LEDPIN, HIGH); + delay(100); + digitalWrite(LEDPIN, LOW); + delay(400); + } + + // Transmit periodic Tx interval + getRegister(REGI_TXINTERVAL)->getData(); + // Transmit power voltage + getRegister(REGI_VOLTSUPPLY)->getData(); + // Switch to Rx OFF state + panstamp.enterSystemState(SYSTATE_RXOFF); +} + +/** + * loop + * + * Arduino main loop + */ +void loop() +{ + // Transmit sensor data + getRegister(REGI_SENSOR)->getData(); + // Transmit power voltage + getRegister(REGI_VOLTSUPPLY)->getData(); + + // Sleep + panstamp.goToSleep(); +} + diff --git a/examples/soilmoisture/product.h b/examples/soilmoisture/product.h new file mode 100644 index 0000000..60bb72c --- /dev/null +++ b/examples/soilmoisture/product.h @@ -0,0 +1,51 @@ +/** + * product.h + * + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 04/29/2013 + */ + +#ifndef _PRODUCT_H +#define _PRODUCT_H + +/** + * Hardware version + */ +#define HARDWARE_VERSION 0x00000100L + +/** + * Firmware version + */ +#define FIRMWARE_VERSION 0x00000100L + +/** + * Manufacturer SWAP ID + */ +#define SWAP_MANUFACT_ID 0x00000001L + +/** + * Product SWAP ID + */ +#define SWAP_PRODUCT_ID 0x0000000EL + +#endif + diff --git a/examples/soilmoisture/regtable.h b/examples/soilmoisture/regtable.h new file mode 100644 index 0000000..9951ab2 --- /dev/null +++ b/examples/soilmoisture/regtable.h @@ -0,0 +1,43 @@ +/** + * regtable.h + * + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 04/29/2013 + */ + +#ifndef _REGTABLE_H +#define _REGTABLE_H + +//#include "Arduino.h" +#include "register.h" +#include "commonregs.h" + +/** + * Register indexes + */ +DEFINE_REGINDEX_START() + REGI_VOLTSUPPLY, + REGI_SENSOR, +DEFINE_REGINDEX_END() + +#endif + diff --git a/examples/soilmoisture/regtable.ino b/examples/soilmoisture/regtable.ino new file mode 100644 index 0000000..17d68d3 --- /dev/null +++ b/examples/soilmoisture/regtable.ino @@ -0,0 +1,105 @@ +/** + * regtable + * + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#include "product.h" +#include "regtable.h" + +/** + * Definition of common registers + */ +DEFINE_COMMON_REGISTERS() + +/* + * Definition of custom registers + */ +// Voltage supply +static byte dtVoltSupply[2]; +REGISTER regVoltSupply(dtVoltSupply, sizeof(dtVoltSupply), &updtVoltSupply, NULL); +// Sensor value register (dual sensor) +static byte dtSensor[4]; +REGISTER regSensor(dtSensor, sizeof(dtSensor), &updtSensor, NULL); + +/** + * Initialize table of registers + */ +DECLARE_REGISTERS_START() + ®VoltSupply, + ®Sensor +DECLARE_REGISTERS_END() + +/** + * Definition of common getter/setter callback functions + */ +DEFINE_COMMON_CALLBACKS() + +/** + * Definition of custom getter/setter callback functions + */ + +/** + * updtVoltSupply + * + * Measure voltage supply and update register + * + * 'rId' Register ID + */ +const void updtVoltSupply(byte rId) +{ + unsigned long result = panstamp.getVcc(); + + // Update register value + regTable[rId]->value[0] = (result >> 8) & 0xFF; + regTable[rId]->value[1] = result & 0xFF; +} + +/** + * updtSensor + * + * Measure sensor data and update register + * + * 'rId' Register ID + */ +const void updtSensor(byte rId) +{ + // Power sensors + digitalWrite(POWER_0_PIN, HIGH); + digitalWrite(POWER_1_PIN, HIGH); + delay(10); + // Read analog values + unsigned int adcValue0 = analogRead(SENSOR_0_PIN); + unsigned int adcValue1 = analogRead(SENSOR_1_PIN); + // Unpower sensors + digitalWrite(POWER_0_PIN, LOW); + digitalWrite(POWER_1_PIN, LOW); + + // Update register value + dtSensor[0] = (adcValue0 >> 8) & 0xFF; + dtSensor[1] = adcValue0 & 0xFF; + dtSensor[2] = (adcValue1 >> 8) & 0xFF; + dtSensor[3] = adcValue1 & 0xFF; +} + + diff --git a/examples/soilmoisture/soilmoisture.ino b/examples/soilmoisture/soilmoisture.ino new file mode 100644 index 0000000..8bd8372 --- /dev/null +++ b/examples/soilmoisture/soilmoisture.ino @@ -0,0 +1,112 @@ +/* + * soilmoisture + * + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 04/29/2013 + * + * Device: + * Soil Moisture sensor + * + * Description: + * This application measures soil moisture from any two sensor providing an + * analog signal + * + * These devices are low-power enabled so they will enter low-power mode + * just after reading the sensor values and transmitting them over the + * SWAP network. + * + * Associated Device Definition File, defining registers, endpoints and + * configuration parameters: + * soilmoisture.xml + */ + +#include "regtable.h" +#include "swap.h" + +/** + * Sensor pins + */ +#define SENSOR_0_PIN A3 // Analog pin - sensor 0 +#define POWER_0_PIN 16 // Digital pin used to powwer sensor 0 +#define SENSOR_1_PIN A4 // Analog pin - sensor 1 +#define POWER_1_PIN 18 // Digital pin used to powwer sensor 1 + +/** + * setup + * + * Arduino setup function + */ +void setup() +{ + int i; + + pinMode(LED, OUTPUT); + digitalWrite(LED, LOW); + + // Initialize power pins + pinMode(POWER_0_PIN, OUTPUT); + digitalWrite(POWER_0_PIN, LOW); + pinMode(POWER_1_PIN, OUTPUT); + digitalWrite(POWER_1_PIN, LOW); + + // Init SWAP stack + swap.init(); + + // Transmit product code + swap.getRegister(REGI_PRODUCTCODE)->getData(); + + // Enter SYNC state + swap.enterSystemState(SYSTATE_SYNC); + + // During 3 seconds, listen the network for possible commands whilst the LED blinks + for(i=0 ; i<6 ; i++) + { + digitalWrite(LED, HIGH); + delay(100); + digitalWrite(LED, LOW); + delay(400); + } + + // Transmit periodic Tx interval + swap.getRegister(REGI_TXINTERVAL)->getData(); + // Transmit power voltage + swap.getRegister(REGI_VOLTSUPPLY)->getData(); + // Switch to Rx OFF state + swap.enterSystemState(SYSTATE_RXOFF); +} + +/** + * loop + * + * Arduino main loop + */ +void loop() +{ + // Transmit sensor data + swap.getRegister(REGI_SENSOR)->getData(); + // Transmit power voltage + swap.getRegister(REGI_VOLTSUPPLY)->getData(); + + // Sleep + swap.goToSleep(); +} + diff --git a/examples/temphumpress/product.h b/examples/temphumpress/product.h new file mode 100644 index 0000000..83e3088 --- /dev/null +++ b/examples/temphumpress/product.h @@ -0,0 +1,59 @@ +/** + * product.h + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#ifndef _PRODUCT_H +#define _PRODUCT_H + +#include "sensor.h" + +/** + * Hardware version + */ +#define HARDWARE_VERSION 0x00000200L + +/** + * Firmware version + */ +#define FIRMWARE_VERSION 0x00000101L + +/** + * Manufacturer SWAP ID + */ +#define SWAP_MANUFACT_ID 0x00000001L + +/** + * Product SWAP ID + */ +#ifdef TEMPHUM +#define SWAP_PRODUCT_ID 0x00000001L +#elif TEMP +#define SWAP_PRODUCT_ID 0x00000004L +#elif TEMPPRESS +#define SWAP_PRODUCT_ID 0x00000005L +#endif + +#endif + diff --git a/examples/temphumpress/regtable.h b/examples/temphumpress/regtable.h new file mode 100644 index 0000000..7e451bf --- /dev/null +++ b/examples/temphumpress/regtable.h @@ -0,0 +1,42 @@ +/** + * regtable.h + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#ifndef _REGTABLE_H +#define _REGTABLE_H + +#include "register.h" +#include "commonregs.h" + +/** + * Register indexes + */ +DEFINE_REGINDEX_START() + REGI_VOLTSUPPLY, + REGI_SENSOR +DEFINE_REGINDEX_END() + +#endif + diff --git a/examples/temphumpress/regtable.ino b/examples/temphumpress/regtable.ino new file mode 100644 index 0000000..e0d40a0 --- /dev/null +++ b/examples/temphumpress/regtable.ino @@ -0,0 +1,122 @@ +/** + * regtable + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#include "product.h" +#include "regtable.h" +#include "sensor.h" + +/** + * Definition of common registers + */ +DEFINE_COMMON_REGISTERS() + +/* + * Definition of custom registers + */ +// Voltage supply +static unsigned long voltageSupply = 3300; +static byte dtVoltSupply[2]; +REGISTER regVoltSupply(dtVoltSupply, sizeof(dtVoltSupply), &updtVoltSupply, NULL); +// Sensor value register +REGISTER regSensor(dtSensor, sizeof(dtSensor), &updtSensor, NULL); + +/** + * Initialize table of registers + */ +DECLARE_REGISTERS_START() + ®VoltSupply, + ®Sensor +DECLARE_REGISTERS_END() + +/** + * Definition of common getter/setter callback functions + */ +DEFINE_COMMON_CALLBACKS() + +/** + * Definition of custom getter/setter callback functions + */ + +/** + * updtVoltSupply + * + * Measure voltage supply and update register + * + * 'rId' Register ID + */ +const void updtVoltSupply(byte rId) +{ + unsigned long result; + + // Read 1.1V reference against AVcc + ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); + 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; // Back-calculate AVcc in mV + voltageSupply = result; // Update global variable Vcc + + #ifdef VOLT_SUPPLY_A7 + + // Read voltage supply from A7 + unsigned short ref = voltageSupply; + result = analogRead(7); + result *= ref; + result /= 1024; + #endif + + /** + * register[eId]->member can be replaced by regVoltSupply in this case since + * no other register is going to use "updtVoltSupply" as "updater" function + */ + + // Update register value + regTable[rId]->value[0] = (result >> 8) & 0xFF; + regTable[rId]->value[1] = result & 0xFF; +} + +/** + * updtSensor + * + * Measure sensor data and update register + * + * 'rId' Register ID + */ +const void updtSensor(byte rId) +{ + #ifdef TEMPHUM + // Read temperature and humidity from sensor + sensor_ReadTempHum(); + #elif TEMP + // Read temperature from sensor + sensor_ReadTemp(); + #elif TEMPPRESS + // Read temperature and pressure from sensor + sensor_ReadTempPress(); + #endif +} diff --git a/examples/temphumpress/sensor.h b/examples/temphumpress/sensor.h new file mode 100644 index 0000000..be647a5 --- /dev/null +++ b/examples/temphumpress/sensor.h @@ -0,0 +1,91 @@ +/** + * sensor.h + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 09/01/2012 + */ + +#ifndef _SENSOR_H +#define _SENSOR_H + +/** + * Type of sensor. Uncomment only one of these options: TEMP, TEMPHUM or TEMPPRESS + */ +//#define TEMP 1 // Temperature sensor (TMP36) +#define TEMPHUM 1 // Temperature + Humidity sensor (DHT11 or DHT22) +//#define TEMPPRESS 1 // Temperature + Pressure sensor = BMP085 + +#ifdef TEMPHUM +//#define DHT11 1 // Temperature + Humidity sensor = DHT11 +#ifndef DHT11 +#define DHT22 1 // Temperature + Humidity sensor = DHT22 +#endif +#endif + +/** + * Sensor values + */ +#ifdef TEMP + byte dtSensor[2]; +#elif TEMPHUM + byte dtSensor[4]; +#else + byte dtSensor[6]; +#endif + +/** + * Pin definitions + */ +// Temperature + Humidity (DHT11 or DHT22) +#ifdef TEMPHUM +#define PORTW_DHT_DATA PORTC +#define PORTR_DHT_DATA PINC +#define PORTD_DHT_DATA DDRC +#define BIT_DHT_DATA 2 +#define PIN_DHT_DATA 16 + +#define setDataPin() bitSet(PORTW_DHT_DATA, BIT_DHT_DATA) +#define clearDataPin() bitClear(PORTW_DHT_DATA, BIT_DHT_DATA) +#define getDataPin() bitRead(PORTR_DHT_DATA, BIT_DHT_DATA) +#define setDataInput() bitClear(PORTD_DHT_DATA, BIT_DHT_DATA) +#define setDataOutput() bitSet(PORTD_DHT_DATA, BIT_DHT_DATA) + +#define PIN_PWRDHT 15 +#define dhtSensorON() digitalWrite(PIN_PWRDHT, HIGH); +#define dhtSensorOFF() digitalWrite(PIN_PWRDHT, LOW); + +// Temperature + Pressure (BMP085) +#elif TEMPPRESS +#define PIN_PWRPRESS 15 // Digital pin +#define pressSensorON(); digitalWrite(PIN_PWRPRESS, HIGH); +#define pressSensorOFF(); digitalWrite(PIN_PWRPRESS, LOW); + +// Temperature only (TMP36) +#elif TEMP +#define PIN_ADCTEMP 2 // Analog pin +#define PIN_PWRTEMP 15 // Digital pin +#define tempSensorON(); digitalWrite(PIN_PWRTEMP, HIGH); +#define tempSensorOFF(); digitalWrite(PIN_PWRTEMP, LOW); +#endif + +#endif + diff --git a/examples/temphumpress/sensor.ino b/examples/temphumpress/sensor.ino new file mode 100644 index 0000000..dc19ee2 --- /dev/null +++ b/examples/temphumpress/sensor.ino @@ -0,0 +1,259 @@ +/** + * sensor + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 03/31/2011 + */ + +#include "Arduino.h" +#include "sensor.h" + +#ifdef TEMPHUM +#ifdef DHT11 +#include "dht11.h" +#endif +#elif TEMPPRESS +#include "Wire.h" +#include "Adafruit_BMP085.h" +Adafruit_BMP085 bmp; +#endif + +/** + * Local functions + */ +#ifdef TEMPHUM +int sensor_ReadByte(void); + +/** + * sensor_ReadByte + * + * Read data byte from DHT11 sensor + */ +int sensor_ReadByte(void) +{ + byte i, result = 0; + unsigned int count = 20000; + + for(i=0; i< 8; i++) + { + while(!getDataPin()) + { + if (--count == 0) + return -1; + } + delayMicroseconds(30); + + if (getDataPin()) + result |=(1<<(7-i)); + + count = 20000; + + while(getDataPin()) + { + if (--count == 0) + return -1; + } + } + return result; +} + +/** + * sensor_ReadTempHum + * + * Read temperature and humidity values from DHT11 sensor + * + * Return -1 in case of error. Return 0 otherwise + */ +int sensor_ReadTempHum(void) +{ + int temperature, humidity, chk; + + #ifdef DHT11 + + dht11 sensor; + + dhtSensorON(); + delay(1500); + chk = sensor.read(PIN_DHT_DATA); + dhtSensorOFF(); + + if (chk != DHTLIB_OK) + return -1; + + temperature = sensor.temperature * 10 + 500; + humidity = sensor.humidity * 10; + + #elif DHT22 + + byte dhtData[5]; + byte i, dhtCrc; + boolean success = false; + + // Power ON sensor + dhtSensorON(); + delay(1500); + + setDataOutput(); + setDataPin(); + + // Start condition + clearDataPin(); + delay(18); + setDataPin(); + delayMicroseconds(40); + setDataInput(); + delayMicroseconds(40); + + if (!getDataPin()) + { + // Start condition met + delayMicroseconds(80); + if (getDataPin()) + { + // Start condition met + delayMicroseconds(80); + + // Now ready for data reception + for (i=0; i<5; i++) + { + if ((chk = sensor_ReadByte()) < 0) + return -1; + + dhtData[i] = (byte)chk; + } + success = true; + } + } + + setDataOutput(); + //setDataPin(); + clearDataPin(); + + // Power OFF sensor + dhtSensorOFF(); + + if (!success) + return -1; + + dhtCrc = dhtData[0] + dhtData[1] + dhtData[2] + dhtData[3]; + + // check check_sum + if(dhtData[4]!= dhtCrc) + return -1; // CRC error + + // Prepare values for 2-decimal format: + int sign = 1; + if (dhtData[2] & 0x80) + { + sign = -1; + dhtData[2] &= 0x7F; + } + temperature = sign * word(dhtData[2], dhtData[3]) + 500; // 50.0 ÂșC offset in order to accept negative temperatures + humidity = word(dhtData[0], dhtData[1]); + + #endif + + dtSensor[0] = (temperature >> 8) & 0xFF; + dtSensor[1] = temperature & 0xFF; + dtSensor[2] = (humidity >> 8) & 0xFF; + dtSensor[3] = humidity & 0xFF; + + return 0; +} + +#elif TEMP +/** + * sensor_ReadTemp + * + * Read temperature from TMP36 sensor + * + * Return -1 in case of error. Return 0 otherwise + */ +int sensor_ReadTemp(void) +{ + // Switch on sensor + tempSensorON(); + delay(200); + + // Read voltage from ADC pin + unsigned int reading = analogRead(PIN_ADCTEMP); + + // Switch off sensor + tempSensorOFF(); + + // Convert reading to voltage (mV) + float fVolt = (reading * voltageSupply) / 1024.0; + unsigned int voltage = fVolt; + + // Fill register + dtSensor[0] = (voltage >> 8) & 0xFF; + dtSensor[1] = voltage & 0xFF; + + return 0; +} + +#elif TEMPPRESS +/** + * sensor_ReadTempPress + * + * Read temperature and pressure from BMP085 sensor + * + * Return -1 in case of error. Return 0 otherwise + */ +int sensor_ReadTempPress(void) +{ + delay(400); + unsigned int temperature = bmp.readTemperature() * 10 + 500; + unsigned long pressure = bmp.readPressure(); // Pa + + dtSensor[0] = (temperature >> 8) & 0xFF; + dtSensor[1] = temperature & 0xFF; + dtSensor[2] = (pressure >> 24) & 0xFF; + dtSensor[3] = (pressure >> 16) & 0xFF; + dtSensor[4] = (pressure >> 8) & 0xFF; + dtSensor[5] = pressure & 0xFF; + + return 0; +} + +#endif + +/** + * initSensor + * + * Initialize sensor pins + */ +void initSensor(void) +{ +#ifdef TEMP + pinMode(PIN_PWRTEMP, OUTPUT); // Configure Power pin as output + tempSensorOFF(); +#elif TEMPHUM + pinMode(PIN_PWRDHT, OUTPUT); // Configure Power pin as output + dhtSensorOFF(); +#elif TEMPPRESS + pinMode(PIN_PWRPRESS, OUTPUT); // Configure Power pin as output + pressSensorON(); + bmp.begin(); +#endif +} + diff --git a/examples/temphumpress/temphumpress.ino b/examples/temphumpress/temphumpress.ino new file mode 100644 index 0000000..25709b6 --- /dev/null +++ b/examples/temphumpress/temphumpress.ino @@ -0,0 +1,130 @@ +/* + * temphum + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panStamp 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 panStamp; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 11/31/2011 + * + * IMPORTANT: This sketch works only for panStamp AVR. For panStamp NRG + * use dhtsensor ot ntc instead. + * + * Device: + * Temperature sensor + * Dual Temperature + Humidity sensor + * Dual Pressure + Temperature sensor + * + * Description: + * This sketch can generate three different devices depending on the + * definition of a pre-compiler contstant in sensor.h (only enable one + * of these options): + * + * TEMP: Device measuring temperature from a TMP36 sensor. + * Pins: PIN_ADCTEMP (ADC input) and PIN_PWRTEMP (power pin) + * + * TEMPHUM: Device measuring temperature and humidity from a DHT11/DHT22 + * sensor. In case you use a DHT11 sensor, you will need this library: + * http://arduino.cc/playground/Main/DHT11Lib + * + * Pins: DHT_DATA (digital I/O) and PIN_PWRDHT (power pin) + * + * TEMPPRESS: Device measuring temperature and barometric pressure from + * an I2C BMP085 sensor. This sketch makes use of Adafruit's BMP085 library + * for Arduino: http://learn.adafruit.com/bmp085/using-the-bmp085 + * Pins: I2C port and PIN_PWRPRESS (Power pin) + * + * These devices are low-power enabled so they will enter low-power mode + * just after reading the sensor values and transmitting them over the + * SWAP network. + * Edit sensor.h for your sensor settings (type of sensor and pins) + * + * Associated Device Definition Files, defining registers, endpoints and + * configuration parameters: + * temp.xml (Temperature sensor) + * temphum.xml (Dual Humidity + Temperature sensor) + * temppress.xml (Dual Pressure + Temperature sensor) + */ + +#include "regtable.h" +#include "swap.h" + +/** + * Uncomment if you are reading Vcc from A7. All battery-boards do this + */ +//#define VOLT_SUPPLY_A7 + +/** + * setup + * + * Arduino setup function + */ +void setup() +{ + int i; + + // Init SWAP stack + swap.init(); + + pinMode(LED, OUTPUT); + + // Initialize sensor pins + initSensor(); + + // Transmit product code + swap.getRegister(REGI_PRODUCTCODE)->getData(); + + // Enter SYNC state + swap.enterSystemState(SYSTATE_SYNC); + + // During 3 seconds, listen the network for possible commands whilst the LED blinks + for(i=0 ; i<6 ; i++) + { + digitalWrite(LED, HIGH); + delay(100); + digitalWrite(LED, LOW); + delay(400); + } + + // Transmit periodic Tx interval + swap.getRegister(REGI_TXINTERVAL)->getData(); + // Transmit power voltage + swap.getRegister(REGI_VOLTSUPPLY)->getData(); + // Switch to Rx OFF state + swap.enterSystemState(SYSTATE_RXOFF); +} + +/** + * loop + * + * Arduino main loop + */ +void loop() +{ +// digitalWrite(LED, HIGH); + // Transmit sensor data + swap.getRegister(REGI_SENSOR)->getData(); + // Transmit power voltage + swap.getRegister(REGI_VOLTSUPPLY)->getData(); +// digitalWrite(LED, LOW); + + // Sleep + swap.goToSleep(); +} + diff --git a/examples/template/.svn/all-wcprops b/examples/template/.svn/all-wcprops new file mode 100644 index 0000000..6c13ddb --- /dev/null +++ b/examples/template/.svn/all-wcprops @@ -0,0 +1,29 @@ +K 25 +svn:wc:ra_dav:version-url +V 49 +/svn/!svn/ver/351/trunk/arduino/sketches/template +END +template.ino +K 25 +svn:wc:ra_dav:version-url +V 62 +/svn/!svn/ver/351/trunk/arduino/sketches/template/template.ino +END +product.h +K 25 +svn:wc:ra_dav:version-url +V 59 +/svn/!svn/ver/351/trunk/arduino/sketches/template/product.h +END +regtable.ino +K 25 +svn:wc:ra_dav:version-url +V 62 +/svn/!svn/ver/351/trunk/arduino/sketches/template/regtable.ino +END +regtable.h +K 25 +svn:wc:ra_dav:version-url +V 60 +/svn/!svn/ver/351/trunk/arduino/sketches/template/regtable.h +END diff --git a/examples/template/.svn/entries b/examples/template/.svn/entries new file mode 100644 index 0000000..07d8228 --- /dev/null +++ b/examples/template/.svn/entries @@ -0,0 +1,164 @@ +10 + +dir +770 +https://panstamp.googlecode.com/svn/trunk/arduino/sketches/template +https://panstamp.googlecode.com/svn + + + +2012-01-02T18:09:17.986421Z +351 +dberenguer@usapiens.com + + + + + + + + + + + + + + +d77f469f-0316-7e13-d0a9-da7f422f7f4c + +product.h +file + + + + +2012-07-26T09:30:28.000000Z +beb8634d55fe7ee4c08aaf9e2441bbe1 +2012-01-02T18:09:17.986421Z +351 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +1235 + +regtable.ino +file + + + + +2012-07-26T09:30:28.000000Z +0ccc5051fd17a89016d211b82d0902d1 +2012-01-02T18:09:17.986421Z +351 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +1506 + +regtable.h +file + + + + +2012-07-26T09:30:28.000000Z +f92083137e9b15be3287be3aac5c13f0 +2012-01-02T18:09:17.986421Z +351 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +1117 + +template.ino +file + + + + +2012-07-26T09:30:28.000000Z +20477803c4197d163bbd0769da9d3296 +2012-01-02T18:09:17.986421Z +351 +dberenguer@usapiens.com + + + + + + + + + + + + + + + + + + + + + +1172 + diff --git a/examples/template/.svn/text-base/product.h.svn-base b/examples/template/.svn/text-base/product.h.svn-base new file mode 100644 index 0000000..2405b91 --- /dev/null +++ b/examples/template/.svn/text-base/product.h.svn-base @@ -0,0 +1,51 @@ +/** + * product.h + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panLoader 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 panLoader; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 12/28/2011 + */ + +#ifndef _PRODUCT_H +#define _PRODUCT_H + +/** + * Hardware version + */ +#define HARDWARE_VERSION 0x00000000 + +/** + * Firmware version + */ +#define FIRMWARE_VERSION 0x00000000 + +/** + * Manufacturer SWAP ID + */ +#define SWAP_MANUFACT_ID 0x00000000 + +/** + * Product SWAP ID + */ +#define SWAP_PRODUCT_ID 0x00000000 + +#endif + diff --git a/examples/template/.svn/text-base/regtable.h.svn-base b/examples/template/.svn/text-base/regtable.h.svn-base new file mode 100644 index 0000000..f9d6a84 --- /dev/null +++ b/examples/template/.svn/text-base/regtable.h.svn-base @@ -0,0 +1,42 @@ +/** + * regtable.h + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panLoader 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 panLoader; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 12/28/2011 + */ + +#ifndef _REGTABLE_H +#define _REGTABLE_H + +#include "Arduino.h" +#include "register.h" +#include "commonregs.h" + +/** + * Register indexes + */ +DEFINE_REGINDEX_START() + // First index here = 11 +DEFINE_REGINDEX_END() + +#endif + diff --git a/examples/template/.svn/text-base/regtable.ino.svn-base b/examples/template/.svn/text-base/regtable.ino.svn-base new file mode 100644 index 0000000..9b19a62 --- /dev/null +++ b/examples/template/.svn/text-base/regtable.ino.svn-base @@ -0,0 +1,62 @@ +/** + * regtable + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panLoader 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 panLoader; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 12/28/2011 + */ + +#include +#include "product.h" +#include "panstamp.h" +#include "regtable.h" + +/** + * Declaration of common callback functions + */ +DECLARE_COMMON_CALLBACKS() + +/** + * Definition of common registers + */ +DEFINE_COMMON_REGISTERS() + +/* + * Definition of custom registers + */ +// Your custom registers come here + +/** + * Initialize table of registers + */ +DECLARE_REGISTERS_START() + // Put here pointers to your custom registers +DECLARE_REGISTERS_END() + +/** + * Definition of common getter/setter callback functions + */ +DEFINE_COMMON_CALLBACKS() + +/** + * Definition of custom getter/setter callback functions + */ + diff --git a/examples/template/.svn/text-base/template.ino.svn-base b/examples/template/.svn/text-base/template.ino.svn-base new file mode 100644 index 0000000..535db02 --- /dev/null +++ b/examples/template/.svn/text-base/template.ino.svn-base @@ -0,0 +1,52 @@ +/* + * template + * + * Copyright (c) 2011 Daniel Berenguer + * + * This file is part of the panStamp project. + * + * panLoader 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 2 of the License, or + * any later version. + * + * panLoader 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 panLoader; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 12/28/2011 + */ + +#include "regtable.h" +#include "panstamp.h" + +/** + * setup + * + * Arduino setup function + */ +void setup() +{ + // Init panStamp + panstamp.init(); + + // Transmit product code + getRegister(REGI_PRODUCTCODE)->getData(); +} + +/** + * loop + * + * Arduino main loop + */ +void loop() +{ +} + diff --git a/examples/template/product.h b/examples/template/product.h new file mode 100644 index 0000000..774a2e4 --- /dev/null +++ b/examples/template/product.h @@ -0,0 +1,51 @@ +/** + * product.h + * + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panLoader 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 panLoader; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 12/28/2011 + */ + +#ifndef _PRODUCT_H +#define _PRODUCT_H + +/** + * Hardware version + */ +#define HARDWARE_VERSION 0x00000000L + +/** + * Firmware version + */ +#define FIRMWARE_VERSION 0x00000000L + +/** + * Manufacturer SWAP ID + */ +#define SWAP_MANUFACT_ID 0x00000000L + +/** + * Product SWAP ID + */ +#define SWAP_PRODUCT_ID 0x00000000L + +#endif + diff --git a/examples/template/regtable.h b/examples/template/regtable.h new file mode 100644 index 0000000..7abb4b3 --- /dev/null +++ b/examples/template/regtable.h @@ -0,0 +1,42 @@ +/** + * regtable.h + * + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panLoader 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 panLoader; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 12/28/2011 + */ + +#ifndef _REGTABLE_H +#define _REGTABLE_H + +#include "Arduino.h" +#include "register.h" +#include "commonregs.h" + +/** + * Register indexes + */ +DEFINE_REGINDEX_START() + // First index here = 11 +DEFINE_REGINDEX_END() + +#endif + diff --git a/examples/template/regtable.ino b/examples/template/regtable.ino new file mode 100644 index 0000000..406c2bb --- /dev/null +++ b/examples/template/regtable.ino @@ -0,0 +1,55 @@ +/** + * regtable + * + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panStamp 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 2 of the License, or + * any later version. + * + * panLoader 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 panLoader; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 12/28/2011 + */ + +#include "product.h" +#include "regtable.h" + +/** + * Definition of common registers + */ +DEFINE_COMMON_REGISTERS() + +/* + * Definition of custom registers + */ +// Your custom registers come here + +/** + * Initialize table of registers + */ +DECLARE_REGISTERS_START() + // Put here pointers to your custom registers +DECLARE_REGISTERS_END() + +/** + * Definition of common getter/setter callback functions + */ +DEFINE_COMMON_CALLBACKS() + +/** + * Definition of custom getter/setter callback functions + */ + diff --git a/examples/template/template.ino b/examples/template/template.ino new file mode 100644 index 0000000..ea93bf6 --- /dev/null +++ b/examples/template/template.ino @@ -0,0 +1,52 @@ +/* + * template + * + * Copyright (c) 2014 panStamp + * + * This file is part of the panStamp project. + * + * panLoader 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 2 of the License, or + * any later version. + * + * panLoader 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 panLoader; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * Author: Daniel Berenguer + * Creation date: 12/28/2011 + */ + +#include "regtable.h" +#include "swap.h" + +/** + * setup + * + * Arduino setup function + */ +void setup() +{ + // Init SWAP stack + swap.init(); + + // Transmit product code + swap.getRegister(REGI_PRODUCTCODE)->getData(); +} + +/** + * loop + * + * Arduino main loop + */ +void loop() +{ +} +