Smart-Home-TX Test

Dependencies:   4DGL-uLCD-SE PinDetect

Fork of Xbee-Smart-Home-Outside by prana koirala

main.cpp

Committer:
pkoirala3
Date:
2017-04-26
Revision:
2:b549ccada3c3
Parent:
1:7d069ab3f551
Child:
3:80ca319bd924

File content as of revision 2:b549ccada3c3:

#include "mbed.h"
#include <string>
#include <stdio.h>
#include "uLCD_4DGL.h"
#include "rtos.h"
// #include "jpegutil.h"

Serial pc(USBTX, USBRX);
uLCD_4DGL lcd(p28,p27,p24);
Serial xbee(p9, p10);
DigitalOut reset(p8);
DigitalOut led1(LED1);
DigitalOut led2(LED2);
InterruptIn getStatus(p16);
InterruptIn toggleLight(p17);

// LocalFileSystem local("local");  /

volatile bool statusReq = false;
volatile bool flipLed = false;

Thread t1;
// Mutex serialMutex;

char buffer[50];

void statusRequest()
{
    led1 = 1;
    statusReq = true;
}

void flipLight()
{
    led2 = 1;
    flipLed = true;
}

void sendcommand(char out)
{
    while(xbee.writeable()) {
        led2 = 1;
        char outbuf = out;
        xbee.putc(outbuf);
        pc.putc(outbuf);
        led2 = 0;
    }
}

void getcommand()
{
    while(1) {
        if(xbee.readable()) {
            led1 = 1;
            int x = 0;
            while(xbee.readable()) {
                buffer[x] = xbee.getc();
                x++;
            }
            led1 = 0;
            const char s[2] = "|";
            char *token;
            token = strtok(buffer, s); // get the first token 
            int j = 2;
            while( token != NULL ) { // walk through other tokens 
                lcd.locate(11,j);   // Print in correct place of LCD
                lcd.printf( "%s", token );
                token = strtok(NULL, s);
                j++;
            }
        }
        Thread::wait(1000);
    }
}

int main()
{
    reset = 0;
    wait_ms(1);
    reset = 1;
    wait_ms(1);
    getStatus.mode(PullDown);
    toggleLight.mode(PullDown);
    getStatus.rise(&statusRequest); //attach address of function on rising edge
    toggleLight.rise(&flipLight);
    // lcd.baudrate(300000);
    lcd.cls();
    lcd.locate(0,0);
    lcd.line(0, 5, 128, 5, 0xFF0000);
    lcd.printf("\r\nStatus:");
    lcd.printf("\r\n");
    lcd.printf("Temp degC:\r\n");
    lcd.printf("Humidity %:\r\n");
    lcd.printf("Lights: \r\n");
    lcd.line(0, 45, 128, 45, 0xFF0000);
    // xbee.baud(115200); // May need to latter
    t1.start(getcommand);

    while(1) {
        if (statusReq == true) {
            sendcommand('s');
            statusReq = false;
        } else if (flipLed == true) {
            sendcommand('l');
            flipLed = false;
        }
        Thread::wait(2000);

    }
}

/*
        string file = "/local/PICT003.jpg";
        lcd.locate(0, 6);
        ReadJPEGFromFile(file.c_str());
        lcd.printf("Image Displaying:\r\n");
        lcd.BLIT(0, 60, 60, 80, &color[0]);
        led1 = 1;

        BLIT(int x, int y, int w, int h, int *colors)
        x   is the left-edge of the region.
        y   is the top-edge of the region.
        w   specifies the width of the region.
        h   specifies the height of the region.
        colour  is a pointer to the array with size = w * h.
*/