Signal Generator

Dependencies:   IniManager RA8875 Watchdog mbed-rtos mbed

Fork of speaker_demo_Analog by jim hamblen

main.cpp

Committer:
WiredHome
Date:
2017-01-13
Revision:
1:dd07e1deec6c
Parent:
0:1c8118ee4106
Child:
2:8f71b71fce1b

File content as of revision 1:dd07e1deec6c:


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

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;
SignalGenerator 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;
 
/* SignalGenerator usage
 */
void SynchronousUserInput(); //<! Ticker callback
Ticker g_synchronousUserInput;



// 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();
    int bl = ini.ReadLongInt("Settings", "Backlight", 80);
    lcd.Backlight_u8(bl);
    InitTS();
    
    // Bring the signal generator online
    ui.Init();
    
    //volatile SignalGenerator::SignalGeneratorType _choice; //<! User selects the desired signal type
    //volatile int frequency; //<! Signal frequency
 
    wait(1); // Needed after startup
    
    // Launch available indicator
    g_availableLed = 1;
    g_available.attach(&AvailableLedIndicator, 2.0); // Never detached

    ui.ShowMenu();
    // Start infinite loop
    while(true)
    {
//        wd.Service();
        if (pc.readable()) {
            int c = pc.getc();
            ui.Poll(c);
        } else {
            ui.Poll();
        }
#if 0
        // Acquire settings
        //_choice = DisplaySignalGeneratorTestMenuAndGetChoice();
        printf("\r\nEnter signal frequency (< 1MHz/# of samples): ");
        scanf("%d", &frequency);
        
        // Prepare the signal
        g_signal.SetSignalFrequency(_choice, frequency);

        // Launch execution
        if (g_signal.BeginRunAsync() == -1) {
            // Synchronous mode
            g_synchronousUserInput.attach(&SynchronousUserInput, 0.005); // 5ms
            g_signal.Run(); // Never stopped
            printf("\r\nSignal Generator terminated\r\n");
            g_synchronousUserInput.detach();
        } else {
            // Asynchronous mode
            printf("\r\n\r\nPress 'q' to terminate\r\n");
            while (getchar() != 'q');
            g_signal.EndRunAsync();
        }
#endif
    } // End of 'while' statement
} // End of main program
 
/** Callbak used by CPU availabe ticker to indicate the program is alive
 */
void AvailableLedIndicator() {
    g_availableLed != g_availableLed;
} // End of function AvailableLedIndicator
 
/** Callbak used when SignalLibrary is used in synchronous mode (Run() method)
 */
void SynchronousUserInput() {
    if (pc.readable()) {
        g_signal.Stop();
        getchar();
    }
} // End of function SynchronousUserInput