Table controller for piswarm-office

Dependencies:   mbed

Fork of PiSwarmTableController by piswarm

Revision:
1:e4a0d424ac8d
Parent:
0:d88fd55a27a6
Child:
2:c81f4ef63132
--- a/main.cpp	Wed Mar 19 15:09:11 2014 +0000
+++ b/main.cpp	Wed May 21 12:47:29 2014 +0000
@@ -1,77 +1,36 @@
 /* University of York Robot Lab
  *
- * Programmable IR Beacon with Display Example Code
+ * Pi Swarm Table Controller Demo Code
  *
- * This file is intended for use exclusively with the high-power IR Beacon with display
+ * This file is intended for use exclusively with the Pi Swarm Table Controller (PCB 1.0)
  * 
- * 
- *
  * (C) Dr James Hilder, Dept. Electronics & Computer Science, University of York
  *
- * March 2014
+ * May 2014
  *
  */
 
 #include "mbed.h"
 #include "display.h"        //Display driver for the Midas 16x2 I2C Display
 
-DigitalIn button1(p23);     //"SET" Push-button
-DigitalIn button2(p24);     //"SELECT" Push-button
-PwmOut out_narrow(p21);     //PWM Output for the narrow (high intensity) LED driver
-PwmOut out_wide(p22);       //PWM Output for the wide-angle LED driver
+PwmOut ir_pwm_out(p21);     //PWM Output for the IR LED driver
+DigitalOut ir_led(LED1);
+DigitalOut pir_led(LED4);    
+AnalogIn input_1(p20);
+AnalogIn input_2(p19);
+DigitalIn input_3(p18);
+DigitalIn input_4(p17);
+DigitalIn input_5(p16);
+DigitalIn input_6(p15);
 
 Display display;
 Timer system_timer;         //System timer is used for timer the on-off periods for the LEDs
-Timer button_timer;         //Button timer is used for debouncing the push buttons and pausing for entering 'SET' mode
-
-char adjust = 0;
-char mode = 0;
-char power = 1;
-char duty = 2;
-char frequency = 2;
-
-int on_period;
-int off_period;
-int total_period;
-char phase;
+Ticker polling_ticker;      //Ticker for polling the input sensors
 
-int get_period()
-{
-    switch(frequency) {
-        case 0:
-            return 1000000;
-        case 1:
-            return 500000;
-        case 2:
-            return 200000;
-        case 3:
-            return 100000;
-        case 4:
-            return 50000;
-        case 5:
-            return 33333;
-        case 6:
-            return 25000;
-        case 7:
-            return 20000;
-    }
-    return 0;
-}
-
-void update_cycles()
-{
-    total_period = get_period();
-    switch(duty){
-        case 0: on_period=total_period/10;break;
-        case 1: on_period=total_period/4;break;
-        case 2: on_period=total_period/2;break;
-        case 3: on_period=(total_period*3)/4;break;
-        case 4: on_period=total_period-(total_period/10);break;    
-    }
-    off_period = total_period - on_period;
-    phase = 0;
-    system_timer.reset();
-}
+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
 
 void init()
 {
@@ -82,256 +41,53 @@
     display.write_string("LABORATORY",10);
     wait(0.45);
     display.clear_display();
-    display.set_position(0,2);
-    display.write_string("Programmable",12);
+    display.set_position(0,1);
+    display.write_string("Pi Swarm Table",14);
     display.set_position(1,3);
-    display.write_string("IR Beacon",9);
+    display.write_string("Controller",10);
     wait(0.45);
 }
 
-char * mode_string()
-{
-    switch(mode) {
-        case 1:
-            return "WIDE  ";
-        case 2:
-            return "NARROW";
-    }
-    return "BOTH  ";
-}
-
-char * duty_string()
-{
-    switch(duty) {
-        case 0:
-            return " 10%";
-        case 1:
-            return " 25%";
-        case 2:
-            return " 50%";
-        case 3:
-            return " 75%";
-        case 4:
-            return " 90%";
-    }
-    return"???";
-}
-
-char * power_string()
-{
-    switch(power) {
-        case 0:
-            return " 25%";
-        case 1:
-            return " 50%";
-        case 2:
-            return " 75%";
-        case 3:
-            return "100%";
-    }
-    return"????";
-}
-
-char * freq_string()
-{
-    switch(frequency) {
-        case 0:
-            return " 1Hz";
-        case 1:
-            return " 2Hz";
-        case 2:
-            return " 5Hz";
-        case 3:
-            return "10Hz";
-        case 4:
-            return "20Hz";
-        case 5:
-            return "30Hz";
-        case 6:
-            return "40Hz";
-        case 7:
-            return "50Hz";
-    }
-    return "????";
-}
-
-// The normal mode display output: shows the frequency, power, duty-cycle and output mode (ie wide, narrow or both)
-void display_status()
-{
-    display.clear_display();
-    display.set_position(0,0);
-    char line1[15] ;
-    strcpy(line1, "F:");
-    strcat(line1, freq_string());
-    strcat(line1, " P:");
-    strcat(line1, power_string());
-    display.write_string(line1,13);
-    char line2[17];
-    strcpy(line2, "D:");
-    strcat(line2, duty_string());
-    strcat(line2, " M:");
-    strcat(line2, mode_string());
-    display.set_position(1,0);
-    display.write_string(line2,16);
+int get_output_power(){
+    switch(power){
+        case 1: return 500;
+        case 2: return 750;
+        case 3: return 1000;   
+    }   
+    return 250;
 }
 
-// The configuration mode; entered by holding the 'set' button for one second.  In this mode the user can through the options
-// by pressing 'set' and change a setting by pressing 'select'.  The final screen in the loop allows the user to return to
-// normal operation, this will also happen with no user input for 5 seconds.
-void change_settings()
-{
-    //Switch off LEDs whilst in configuration mode 
-    out_wide.pulsewidth_us(0);
-    out_narrow.pulsewidth_us(0);
-    display.clear_display();
-    display.set_position(0,0);
-    display.write_string("CONFIGURATION",13);
-    display.set_position(1,0);
-    display.write_string("MODE",4);
-    while(button1 == 1);
-    char screen = 0;
-    char active = 1;
-    Timer settings_timer;
-    settings_timer.start();
-    while(active) {
-        display.clear_display();
-        display.set_position(0,0);
-        char line2[17];
-        switch(screen) {
-            case 0:
-                display.write_string("Set Frequency:",14);
-                display.set_position(1,0);
-                strcpy(line2,freq_string());
-                display.write_string(line2,4);
-                break;
-            case 1:
-                display.write_string("Set Power:    ",14);
-                display.set_position(1,0);
-                strcpy(line2,power_string());
-                display.write_string(line2,4);
-                break;
-            case 2:
-                display.write_string("Set Duty Cycle:",15);
-                display.set_position(1,0);
-                strcpy(line2,duty_string());
-                display.write_string(line2,4);
-                break;
-            case 3:
-                display.write_string("Set Mode:     ",14);
-                display.set_position(1,0);
-                strcpy(line2,mode_string());
-                display.write_string(line2,6);
-                break;
-            case 4:
-                display.write_string("End Setup",9);
-                break;
-        }
-        char button_pressed = 0;
-        
-        while(button_pressed < 2 && settings_timer.read_ms() < 5000){
-            if(button1 == 1 || button2 == 1){
-                if(button_pressed == 1){
-                    if(button_timer.read_ms() > 50) button_pressed = 2;
-                } else {button_pressed = 1; button_timer.reset();}
-            }  else button_pressed = 0;
-        }
-        if(button1==1){
-            screen++;
-            if(screen == 5)screen=0;
-            while(button1 == 1);
-        }else{
-          if(button2==1){
-            switch(screen){
-               case 0: frequency ++;
-                       if(frequency == 8) frequency = 0;
-                       break;
-               case 1: power++;
-                       if(power == 4) power=0;
-                       break;
-               case 2: duty++;
-                       if(duty == 5) duty=0;
-                       break;
-               case 3: mode++;
-                       if(mode == 3) mode = 0;
-                       break;
-               case 4: active=0; break;
-            }
-            while(button2 == 1);   
-          }   
-        }
-        if(settings_timer > 5000) active=0;
-        settings_timer.reset();
-    }
+void polling(){
+    pir_led=input_5.read();
 }
 
 int main()
 {
     init();
-    display_status();
-    char b_p = 0;
+    char phase = 0;
     system_timer.start();
-    button_timer.start();
-    update_cycles();
-    out_narrow.period_us(1000);
-    out_wide.period_us(1000);
-    out_narrow.pulsewidth_us(0);
-    out_wide.pulsewidth_us(0);
-
+    ir_pwm_out.period_us(1000);
+    ir_pwm_out.pulsewidth_us(0);
+    polling_ticker.attach(&polling,0.1);
+    
+    //Setup interrupts for 
 
     while(1) {
         if(phase==0){
-          if(system_timer.read_us() > off_period){
+          if(system_timer.read_us() >= off_period){
             system_timer.reset();
-            int pw = 250;
-            if(power==1) pw=500;
-            if(power==2) pw=750;
-            if(power==3) pw=1000;
-            if(mode<2){
-                out_wide.pulsewidth_us(pw);
-            } 
-            if(mode!=1){
-                out_narrow.pulsewidth_us(pw);
-            }
+            int pw = get_output_power();
+            if(use_ir_leds) ir_pwm_out.pulsewidth_us(pw);
+            ir_led=1;
             phase = 1;
           }
         }else{
-          if(system_timer.read_us() > on_period){
+          if(system_timer.read_us() >= on_period){
             system_timer.reset();
+            ir_pwm_out.pulsewidth_us(0);
+            ir_led=0;
             phase = 0;
-            out_wide.pulsewidth_us(0);
-            out_narrow.pulsewidth_us(0);
           }   
         }
-        
-        
-        if(button1 == 1) {
-            if(b_p == 0) {
-                b_p=1;
-                button_timer.reset();
-            }
-            if(b_p == 1) {
-                if(button_timer.read_ms() > 100) {
-                    b_p=2;
-                    display.clear_display();
-                    display.set_position(0,0);
-                    display.write_string("Hold to change",14);
-                    display.set_position(1,0);
-                    display.write_string("settings",8);
-                }
-            }
-            if(b_p == 2) {
-                if(button_timer.read_ms() > 1000) {
-                    change_settings();
-                    update_cycles();
-                    display_status();
-                    b_p=0;
-                }
-            }
-        } else {
-            if (b_p > 0) {
-                if(b_p == 2) display_status();
-                b_p=0;
-            }
-        }
     }
 }
\ No newline at end of file