This program consists of the software developed for the ELEC5870M Individual Project. It runs on the mbed LPC1768. It uses the mbed RTOS to perform the following tasks: - Implements intuitive GUI with buttons, LCD TFT Display and LEDs. - Serial Communication with the RPi - I2C communication with INA219 voltage current sensors - Power control at the USB ports

Dependencies:   Adafruit_GFX Adafruit_ST7735 INA219 MODSERIAL MbedJSONValue mbed-rtos mbed

main.cpp

Committer:
OHstin
Date:
2016-07-18
Revision:
2:104bec169fb6
Parent:
1:1354a7ebd0c6
Child:
3:7666de697752

File content as of revision 2:104bec169fb6:

#include "mbed.h"
#include "Adafruit_ST7735.h"

Adafruit_ST7735 tft(p11, p12, p13, p10, p8, p9); // MOSI, MISO, SCLK, SSEL, TFT_DC, TFT_RST
#include "BatteryIcon.h"
#include "SolarIcon.h"
#include "OutputIcon.h"
#include "SettingsIcon.h"


//LEDs used to display output

DigitalIn enable1(p21);
DigitalIn enable2(p22);
DigitalIn enable3(p23);

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);

AnalogIn ain(p20);
AnalogIn ain1(p19);
AnalogIn ain2(p18);

DigitalOut myled(LED1);

void testdrawtext(char *text, uint16_t color);

// battery icon coordinates
int startingWidth = 15;
int startingHeight = 7;

// solar icon coordinates
int xCoord = 10;
int yCoord = 65;

// output icon coordinates
int xCoord2 = 100;
int yCoord2 = 10;

// settings icon coordinates
int xCoord3 = 100;
int yCoord3 = 60;

int main()
{
    // Use this initializer if you're using a 1.8" TFT
    tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab

    uint16_t backgroundColor = ST7735_BLACK;
    tft.fillScreen(backgroundColor);

    // landscape view
    tft.setRotation(1);
    char current_text[40];
    
    int batteryLevel = ain2*32.5;
    int percentage = ain2*100;
    bool radiationReceived = false;
    bool outputStatus = false;

    sprintf(current_text, "W: %ldp H: %ldp ", tft.height(), tft.width());
    //testdrawtext(current_text, ST7735_WHITE);

    // determine if the battery is charging
    // and if solar radiation is received
    bool batteryCharging;
    if (ain1 > 0.5) {
        batteryCharging = true;
        radiationReceived = true;
    } else {
        batteryCharging = false;
        radiationReceived = false;
    }

    // create and initialise battery icon project
    BatteryIcon batteryIcon(startingWidth, startingHeight,backgroundColor,percentage,batteryCharging);
    // draw the battery icon
    batteryIcon.drawBatteryIcon();

    // create and initialise solar icon
    SolarIcon solarIcon(xCoord, yCoord, backgroundColor);
    // draw solar icon
    solarIcon.drawSolarIcon();
    
    // create and initalise output icon
    OutputIcon outputIcon(xCoord2, yCoord2, backgroundColor);
    // draw output icon
    outputIcon.drawOutputIcon();

    // create and initalise output icon
    SettingsIcon settingsIcon(xCoord3, yCoord3, backgroundColor);
    // draw settings icon
    settingsIcon.drawSettingsIcon();

    while (1) {

        // determine if the battery is charging
        percentage = ain2*100;
        if (ain1 > 0.5) {
            radiationReceived = true;
            batteryCharging = true;
        } else {
            batteryCharging = false;
            radiationReceived = false;
        }
        
        if (ain > 0.5){
            outputStatus = true;    
        } else {
            outputStatus =  false;    
        }

        // set the battery percentage accordingly
        batteryIcon.setBatteryPercentage(percentage,batteryCharging);

        // animate the solar icon accordingly
        solarIcon.animateSolarIcon(radiationReceived);
        
        // animate the output icon accordingly
        outputIcon.animateOutputIcon(outputStatus);

        // wait half a second
        wait_ms(500);
    }

}

void testdrawtext(char *text, uint16_t color)
{
    tft.setCursor(0, 0);
    tft.setTextColor(color);
    tft.setTextWrap(true);
    tft.printf("%s",text);
}