Code for Browonout Generator box

main.cpp

Committer:
el16ba
Date:
2020-02-13
Revision:
3:16478dd3d1f3
Parent:
2:018630ec52d1
Child:
4:44760dafcab7

File content as of revision 3:16478dd3d1f3:


#include "mbed.h"
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <FileHandle.h>
#include <vector>

DigitalOut led1(LED1, 0);
DigitalOut led2(LED2, 0);
DigitalOut led3(LED3, 0);
DigitalOut led4(LED4, 0);
DigitalOut do21(p21, 0);
InterruptIn di22(p22,PullDown); //removing di22 and its related calls and functions causes a major fault for unknown reasons so it has been left in dispite not being used.
Ticker tickOn;
Ticker tickOff;

Serial pc(USBTX, USBRX);  // for PC debug
LocalFileSystem fs("local");

#define NUMCONFIGPARAM  3

bool flg_start = 0;
bool flg_END = false;
long double configData[NUMCONFIGPARAM];
string strData[NUMCONFIGPARAM];

//function prototypes
void init();
bool is_num_char(char c);
bool print_config();
void get_config_data();




//returns if the given character is a number or decimal point character
bool is_num_char(char c)
{
    bool isChar = false;
    if((c >= '0' and c <= '9') or c == '.') {
        isChar = true;
    }
    return isChar;
}

//From: https://os.mbed.com/questions/83075/Is-it-possible-to-read-a-small-text-file/
//Reads config.txt from local drive
bool print_config()
{
    bool flg_error;
    pc.printf("Opening file 'config.txt'... ");
    FILE*   fp = fopen("/local/config.txt", "r");
    pc.printf("%s\n\r", (!fp ? "Fail :(" : "OK"));
    if (!fp) {
        error("error: %s (%d)\n", strerror(errno), -errno);
        flg_error = 1;
    }
    while (!feof(fp)) {
        int c = fgetc(fp);
        pc.printf("%c", c);
    }

    fclose(fp);
    pc.printf("\r\n===========\r\n");
    return flg_error;
}

//Reads reads and converts data from config file
//Once a timed function (e.g. wait(), sleep_for(), ticker, timer e.t.c) is called opening another File pointer will cause the LPC1768 to crash with a Hard Fault.
void get_config_data()
{
    pc.printf("Get Config Data \r\n");
    char c; //Current character being read
    char strData_index = 0;
    char* pEnd;
    bool isReading = false;
    bool isNumChar;
    string currentStr;

    FILE*   fp = fopen("/local/config.txt", "r");

    while (!feof(fp)) {                                                         //loop till end of file

        c = fgetc(fp);
        isNumChar = is_num_char(c);
        if(isNumChar) {                                                         //at number character?
            currentStr.push_back(c);
            isReading = true;
        } else if(!isNumChar and isReading) {                                   //at end of number string?
            isReading = false;
            strData[strData_index] = currentStr;
            configData[strData_index] = strtod(currentStr.c_str(), &pEnd);
            strData_index++;
            currentStr = "";
        }
        if (strData_index >= NUMCONFIGPARAM ) {                                 //Read all nessesary data?
            break;
        }

    }
    fclose(fp);
    ThisThread::sleep_for(5000);
    pc.printf("Data: %Lf, %Lf, %Lf\r\n",configData[0],configData[1],configData[2]);
}

void init()
{
    pc.printf("Initializing \r\n");
    do21 = 0;
    led1 = 0;
    pc.baud(9600);
    print_config();
    get_config_data();
}

void power(char pwr = 0)
{
    do21 = pwr;
    led1 = pwr;
}

void cycle_wait(long double tOn = configData[0], long double tOff = configData[2])
{
    power(1);
    ThisThread::sleep_for(tOn);
    power(0);
    ThisThread::sleep_for(tOff);
    power(1);
}

void cycle_loop(long double tOn = configData[0], long double tOff = configData[2])
{
    while(1) {
        cycle_wait(tOn,tOff);
    }
}

void serial_mode()
{
    pc.printf("Serial Mode \r\n");

    bool flg_read = true;
    char c;
    string str = "";

    while(flg_read) {
        if (c == '\n') {
            if(str == "cyclew\r"){
                cycle_wait();
            }else if(str == "loop\r") {
                cycle_loop();
            }else{
                pc.printf("Unknown command, see README for commands \r\n");
            }
            str = "";
        } else {
            str.push_back(c);
        }
    }

}

int main()
{
    init();
    while(!flg_END) {
        switch((int)configData[1]) {
            case(1):                                                            //Cut the power to the machine once
                cycle_wait();
                pc.printf("cycled \r\n");
                break;

            case(2):                                                            //Cut the power to the machine in a loop
                pc.printf("Begining Power cycle loop, to end loop press restart button \r\n");
                cycle_loop();
                break;

            case(3):                                                            //Launch in serial command mode
                serial_mode();
                break;
        }

    }

    pc.printf("Program End \r\n");



    return 0;
}