Level 2 Project Range Device

Dependencies:   N5110 SDFileSystem SRF02 TMP102 mbed

Fork of Ranger by Philip Thompson

main.cpp

Committer:
el15pjt
Date:
2016-04-10
Revision:
3:8782b8b8658b
Parent:
2:329597081c06
Child:
4:673930f04866

File content as of revision 3:8782b8b8658b:

/*
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;
Ticker ticker_srf02;

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

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

volatile int g_timer_flag = 0;
volatile int g_timer_flag_srf02 = 0;
volatile int g_sw2_flag = 0;
//volatile int g_sw1_flag = 0;

int alert;
int distance;
float bright = 1;

// 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
};
void lcdoutput();
void timer_isr(); // timed interuprt
void timer_isr_srf02();
void backlight();
void init_K64F(); // set up board
void sw2_isr();
//void sw1_isr();
void setalert();
void setleds();

int main()
{
    lcd.init();
    init_K64F();
    ticker.attach(&timer_isr,0.35);
    ticker_srf02.attach(&timer_isr_srf02,0.02);
    // sw1.fall(&sw1_isr);
    sw2.fall(&sw2_isr);
    while(1) {
        if (g_sw2_flag) {
            g_sw2_flag = 0;  // if it has, clear the flag
            backlight();
        }
        if (g_timer_flag_srf02) {
            g_timer_flag_srf02 = 0;  // if it has, clear the flag
            int distance = srf02.getDistanceCm();
        }
        lcdoutput();
        setalert();
        setleds();
    }
}

//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);
    // sw1.mode(PullNone);
}
void lcdoutput()
{
    int length;
    int length1;
    char buffer[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
    char buffer1[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)


    length = sprintf(buffer,"D = %i Cm",distance);
    length1 = sprintf(buffer1,"Bright = %f Cm",bright);

    if (length <= 14)
        lcd.printString(buffer,0,0);
    lcd.printString(buffer1,0,1);

    // draw a line across the display at y = 40 pixels (origin top-left)
    for (int i = 0; i < WIDTH; i++) {
        lcd.setPixel(i,45);
    }

    lcd.refresh(); // need to refresh display after setting pixels
}

void setalert()
{
    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
    }
}

void setleds()
{
    // If statments to determin the output of each LED

    if (Alertlevel[alert].frr_led == HIGH) {
        if (g_timer_flag) {
            g_timer_flag = 0;  // if it has, clear the flag
            rr_led = !rr_led;
        }
    } else {
        rr_led = Alertlevel[alert].srr_led;
    }

    if (Alertlevel[alert].fa_led == HIGH) {
        if (g_timer_flag) {
            g_timer_flag = 0;  // if it has, clear the flag
            a_led = !a_led;
        }
    } else {
        a_led = Alertlevel[alert].sa_led;
    }

    if (Alertlevel[alert].fgg_led == HIGH) {
        if (g_timer_flag) {
            g_timer_flag = 0;  // if it has, clear the flag
            gg_led = !gg_led;
        }
    } else {
        gg_led = Alertlevel[alert].sgg_led;
    }
}

void backlight ()
{

    if (bright == 1.0) {
        bright = 0;
    } else {
        bright += 0.2;
    }
    lcd.setBrightness(bright);
}

void sw2_isr()
{
    g_sw2_flag = 1;   // set flag in ISR
}

/*void sw1_isr()
{
    g_sw1_flag = 1;   // set flag in ISR
}*/


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

void timer_isr_srf02()
{
    g_timer_flag_srf02 = 1;   // set flag in ISR
}