Table controller for piswarm-office
Fork of PiSwarmTableController by
Diff: main.cpp
- Revision:
- 2:c81f4ef63132
- Parent:
- 1:e4a0d424ac8d
- Child:
- 3:bc7a0f14b28a
diff -r e4a0d424ac8d -r c81f4ef63132 main.cpp --- a/main.cpp Wed May 21 12:47:29 2014 +0000 +++ b/main.cpp Thu May 22 14:25:10 2014 +0000 @@ -11,10 +11,14 @@ */ #include "mbed.h" +#include "main.h" +#include "communications.h" #include "display.h" //Display driver for the Midas 16x2 I2C Display + PwmOut ir_pwm_out(p21); //PWM Output for the IR LED driver DigitalOut ir_led(LED1); +DigitalOut tx_led(LED3); DigitalOut pir_led(LED4); AnalogIn input_1(p20); AnalogIn input_2(p19); @@ -22,15 +26,31 @@ DigitalIn input_4(p17); DigitalIn input_5(p16); DigitalIn input_6(p15); +Serial pc (USBTX, USBRX); Display display; +Alpha433 rf; Timer system_timer; //System timer is used for timer the on-off periods for the LEDs Ticker polling_ticker; //Ticker for polling the input sensors int off_period = 950000; //Off-period for the IR LEDs in microseconds int on_period = 50000; //On-period for the IR LEDs in microseconds -char power = 0; //Output power for the IR LEDs : 0=25%, 1=50%, 2=75%, 3=100% (700mA) -char use_ir_leds = 0; //Set to 0 to disable IR LEDs, 1 to enable +char power = 1; //Output power for the IR LEDs : 0=25%, 1=50%, 2=75%, 3=100% (700mA) [NB The LEDs are rated 20mA and are in parallel runs of 20, so a maximum power of 50% is recommended for long term use] +char use_ir_leds = 1; //Set to 0 to disable IR LEDs, 1 to enable + +char input_3_active = 0; +char input_4_active = 0; +char input_5_active = 0; +char input_6_active = 0; + +int input_3_window = 0; +int input_4_window = 0; +int input_5_window = 0; +int input_6_window = 0; + +float polling_rate = 0.1; // Period in seconds to poll sensors +int polling_decay = 300; // Number of polls to decrement counters +int polling_count = 0; void init() { @@ -58,7 +78,81 @@ } void polling(){ - pir_led=input_5.read(); + polling_count ++; + char pir_active = 0; + char state_changed = 0; + if(input_3.read()==1){ + pir_active = 1; + if(input_3_active == 0){ + state_changed = 1; + input_3_active = 1; + input_3_window ++; + } + }else { + input_3_active = 0; + if(polling_count == polling_decay){ + input_3_window --; + if(input_3_window < 0) input_3_window = 0; + } + } + + if(input_4.read()==1){ + pir_active = 1; + if(input_4_active == 0){ + state_changed = 1; + input_4_active = 1; + input_4_window ++; + } + }else { + input_4_active = 0; + if(polling_count == polling_decay){ + input_4_window --; + if(input_4_window < 0) input_4_window = 0; + } + } + + if(input_5.read()==1){ + pir_active = 1; + if(input_5_active == 0){ + state_changed = 1; + input_5_active = 1; + input_5_window ++; + } + }else { + input_5_active = 0; + if(polling_count == polling_decay){ + input_5_window --; + if(input_5_window < 0) input_5_window = 0; + } + } + + if(input_6.read()==1){ + pir_active = 1; + if(input_6_active == 0){ + state_changed = 1; + input_6_active = 1; + input_6_window ++; + } + }else { + input_6_active = 0; + if(polling_count == polling_decay){ + input_6_window --; + if(input_6_window < 0) input_6_window = 0; + } + } + + pir_led=pir_active; + if(polling_count == polling_decay) { + polling_count = 0; + state_changed = 1; + } + if(state_changed == 1){ + display.clear_display(); + display.set_position(0,0); + char text [17]; + snprintf(text,17,"3:%i 4:%i 5:%i 6:%i",input_3_window,input_4_window,input_5_window,input_6_window); + display.write_string(text,16); + } } int main() @@ -70,7 +164,6 @@ ir_pwm_out.pulsewidth_us(0); polling_ticker.attach(&polling,0.1); - //Setup interrupts for while(1) { if(phase==0){ @@ -90,4 +183,21 @@ } } } -} \ No newline at end of file +} + + +void handleUserRFCommand(char sender, char broadcast_message, char request_response, char id, char is_command, char function, char * data, char length){ +} + +void handleUserRFResponse(char sender, char broadcast_message, char success, char id, char is_command, char function, char * data, char length){ +} + +void processRawRFData(char * rstring, char cCount){ +} + + +void handleData(char * data, char length) +{ + display.set_position(1,1); + display.write_string(data,length); +}