Table controller for piswarm-office

Dependencies:   mbed

Fork of PiSwarmTableController by piswarm

main.cpp

Committer:
jah128
Date:
2014-05-21
Revision:
1:e4a0d424ac8d
Parent:
0:d88fd55a27a6
Child:
2:c81f4ef63132

File content as of revision 1:e4a0d424ac8d:

/* University of York Robot Lab
 *
 * Pi Swarm Table Controller Demo Code
 *
 * 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
 *
 * May 2014
 *
 */

#include "mbed.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 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
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

void init()
{
    display.init_display();
    display.set_position(0,2);
    display.write_string("YORK ROBOTICS",13);
    display.set_position(1,3);
    display.write_string("LABORATORY",10);
    wait(0.45);
    display.clear_display();
    display.set_position(0,1);
    display.write_string("Pi Swarm Table",14);
    display.set_position(1,3);
    display.write_string("Controller",10);
    wait(0.45);
}

int get_output_power(){
    switch(power){
        case 1: return 500;
        case 2: return 750;
        case 3: return 1000;   
    }   
    return 250;
}

void polling(){
    pir_led=input_5.read();
}

int main()
{
    init();
    char phase = 0;
    system_timer.start();
    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){
            system_timer.reset();
            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){
            system_timer.reset();
            ir_pwm_out.pulsewidth_us(0);
            ir_led=0;
            phase = 0;
          }   
        }
    }
}