Signal Generator

Dependencies:   IniManager RA8875 Watchdog mbed-rtos mbed

Fork of speaker_demo_Analog by jim hamblen

main.cpp

Committer:
WiredHome
Date:
2017-01-15
Revision:
2:8f71b71fce1b
Parent:
1:dd07e1deec6c
Child:
3:d22f3e52d06a

File content as of revision 2:8f71b71fce1b:


#include "mbed.h"
#include "RA8875.h"
#include "Watchdog.h"           // ver 2
#include "IniManager.h"         // v19

#include "SignalGenDisplay.h"
#include "SignalGenDAC.h"

const char * PROG_MANF = "Smartware Computing";
const char * PROG_NAME = "Signal Generator";
const char * PROG_VERS = "0.01";
const char * BUILD_DATE = __DATE__ " " __TIME__;

RA8875 lcd(p5,p6,p7,p12, NC, "tft");             // SPI:{MOSI,MISO,SCK,/ChipSelect,/reset}, name
INI ini;
SignalGenDAC g_signal(p18);

SignalGenDisplay ui(&lcd, &g_signal, PROG_NAME, PROG_MANF, PROG_VERS, BUILD_DATE);

RawSerial pc(USBTX, USBRX);
LocalFileSystem local("local");
Watchdog wd;

/* CPU Available indicator
 */
DigitalOut g_availableLed(LED1); //<! Led used to indicate the program is alive
void AvailableLedIndicator(); //<! Ticker callback
Ticker g_available;
 

// Calibrate the resistive touch screen, and store the data on the
// local file system.
//
void CalibrateTS(void)
{
    FILE * fh;
    tpMatrix_t matrix;
    RetCode_t r;
    Timer testperiod;
 
    r = lcd.TouchPanelCalibrate("Calibrate the touch panel", &matrix);
    if (r == noerror) {
        fh = fopen("/local/tpcal.cfg", "wb");
        if (fh) {
            fwrite(&matrix, sizeof(tpMatrix_t), 1, fh);
            fclose(fh);
            printf("  tp cal written.\r\n");
            lcd.cls();
        } else {
            printf("  couldn't open tpcal file.\r\n");
        }
    } else {
        printf("error return: %d\r\n", r);
    }
    lcd.cls();
}

// Try to load a previous resistive touch screen calibration from storage. If it
// doesn't exist, activate the touch screen calibration process.
//
void InitTS(void)
{
    FILE * fh;
    tpMatrix_t matrix;

    fh = fopen("/local/tpcal.cfg", "rb");
    if (fh) {
        fread(&matrix, sizeof(tpMatrix_t), 1, fh);
        fclose(fh);
        lcd.TouchPanelSetMatrix(&matrix);
        printf("  tp cal loaded.\r\n");
    } else {
        CalibrateTS();
    }
}



/* Program Entry Point
 */
int main() {
    pc.baud(460800);
    pc.printf("\r\n%s %s\r\n", PROG_NAME, BUILD_DATE);

    if (wd.WatchdogCausedReset()) {
        pc.printf("**** Watchdog Event caused reset ****\r\n");
    }
    // Set very long timeout because the <PrintScreen> function is incredibly slow with local filesystem...
//    wd.Configure(120.0);   // This is forever for real-time embedded, but for a casual network appliance...
    ini.SetFile("/local/SigGen.ini", 2);

    // Bring the LCD online
    lcd.frequency(2000000);
    lcd.init(480,272,16, true, true, true);
    lcd.TouchPanelInit();
    InitTS();
    
    // Bring the signal generator online
    ui.Refresh();
    
    wait(1); // Needed after startup
    
    ui.ShowMenu();
    // Start infinite loop
    while(true)
    {
//        wd.Service();
        if (pc.readable()) {
            int c = pc.getc();
            ui.Poll(c);
        } else {
            ui.Poll();
        }
    } // End of 'while' statement
} // End of main program