Level 2 Project Range Device

Dependencies:   N5110 SDFileSystem SRF02 TMP102 mbed

Fork of Ranger by Philip Thompson

main.cpp

Committer:
el15pjt
Date:
2016-04-01
Revision:
2:329597081c06
Parent:
1:5b991c2302e1
Child:
3:8782b8b8658b

File content as of revision 2:329597081c06:

/*
Philip Thompsonn
EL15PJT
Embedded System Project
EL2645

*/

#include "mbed.h"
#include "SRF02.h"
#include "N5110.h"

#define LOW 0
#define HIGH 1

DigitalOut rr_led (PTA1);
DigitalOut a_led (PTC2);
DigitalOut gg_led(PTB23);

// Ranger object
SRF02 srf02(I2C_SDA,I2C_SCL);

//N5110 Object  VCC, SCE,RST, D/C, MOSI,SCLK, LED
N5110           lcd(PTE26,PTA0,PTC4,PTD0,PTD2,PTD1,PTC3);

Ticker ticker;

// K64F on-board LEDs
DigitalOut r_led(LED_RED);
DigitalOut g_led(LED_GREEN);
DigitalOut b_led(LED_BLUE);

// K64F on-board switches
InterruptIn sw2(SW2);
InterruptIn sw3(SW3);

volatile int g_timer_flag = 0;

int distance;
int length;
int alert;

char buffer[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)

// struct for state
struct Alertlevel {
    char srr_led;  // output value
    char sa_led;    // time in state
    char sgg_led;  // array of next states
    char frr_led;
    char fa_led;
    char fgg_led;
};
typedef const struct Alertlevel STyp;

STyp Alertlevel[8] = {
    {LOW,LOW,LOW,LOW,LOW,LOW}, // no output
    {LOW,LOW,LOW,LOW,LOW,HIGH}, //flash green
    {LOW,LOW,HIGH,LOW,LOW,LOW}, //steady green
    {LOW,LOW,HIGH,LOW,HIGH,LOW}, //flash amber
    {LOW,HIGH,HIGH,LOW,LOW,LOW}, //steady amber
    {LOW,HIGH,HIGH,HIGH,LOW,LOW}, //flash red
    {HIGH,HIGH,HIGH,LOW,LOW,LOW},// steady red
    {LOW,LOW,LOW,HIGH,HIGH,HIGH} // all flash
};
// timed interuprt
void timer_isr();

// set up board
void init_K64F();

int main()
{
    lcd.init();
    init_K64F();
    ticker.attach(&timer_isr,0.05);

    while(1) {

// read sensor distance in cm and print over serial port
        int distance = srf02.getDistanceCm();
        length = sprintf(buffer,"D = %i Cm",distance);
        if (length <= 14)
            lcd.printString(buffer,0,2);
// need to refresh display after setting pixels
        lcd.refresh();

// short delay before next measurement
        wait(0.25);

//Range Alert selection use to adjust alert boundarys
        if (distance >= 200 && distance < 250) {
            alert = 1; //flash green
        } else if (distance >= 150 && distance  < 200) {
            alert = 2; //steady green
        } else if (distance >= 100 && distance < 150) {
            alert = 3; //flashing amber
        } else if (distance >= 50 && distance < 100) {
            alert = 4; //steady amber
        } else if ( distance > 10 && distance < 50) {
            alert = 5; //flashing red
        } else if (distance > 1 && distance <= 10) {
            alert = 6; //steady red
        } else if (distance <=1) {
            alert = 7; //all flashing
        } else {
            alert = 0; //no output
        }
// Steady light outputs
        rr_led = Alertlevel[alert].srr_led;
        a_led = Alertlevel[alert].sa_led;
        gg_led = Alertlevel[alert].sgg_led;

//Flashing light outputs
        if (g_timer_flag) {
            g_timer_flag = 0;  // if it has, clear the flag

            if ( Alertlevel[alert].frr_led == HIGH) {
                rr_led = !rr_led;
            } else if ( Alertlevel[alert].fa_led == HIGH) {
                a_led = !a_led;
            } else if ( Alertlevel[alert].fgg_led == HIGH) {
                gg_led = !gg_led;
            }
        }
    }

}

//Set up board switches and LEDS
void init_K64F()
{
//on-board LEDs are active-low, so set pin high to turn them off.
    r_led = 1;
    g_led = 1;
    b_led = 1;

// since the on-board switches have external pull-ups, we should disable the internal pull-down
// resistors that are enabled by default using InterruptIn
    sw2.mode(PullNone);
    sw3.mode(PullNone);
}


void timer_isr()
{
    g_timer_flag = 1;   // set flag in ISR
}