Signal Generator

Dependencies:   IniManager RA8875 Watchdog mbed-rtos mbed

Fork of speaker_demo_Analog by jim hamblen

Revision:
1:dd07e1deec6c
Parent:
0:1c8118ee4106
Child:
2:8f71b71fce1b
--- a/main.cpp	Mon Jan 21 03:37:11 2013 +0000
+++ b/main.cpp	Fri Jan 13 12:33:37 2017 +0000
@@ -1,22 +1,168 @@
+
 #include "mbed.h"
-// Audio output demo for speaker
-// Speaker Class demo - plays a note on the analog output pin
-// 32 data points on one sine wave cycle are precomputed,
-// scaled, stored in an array and
-// continuously output to the Digital to Analog convertor
+#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;
+
+
 
-// add Speaker class and PlayNote
-// PlayNote args are frequency in hz (<=5000), duration in secs , and volume(0.0..1.0)
-#include  "Speaker.h"
-
-int main()
+// Calibrate the resistive touch screen, and store the data on the
+// local file system.
+//
+void CalibrateTS(void)
 {
-// setup instance of new Speaker class, mySpeaker
-// the pin must be the AnalogOut pin - p18
-    Speaker mySpeaker(p18);
-    // loops forever playing two notes on speaker using analog samples
-    while(1) {
-        mySpeaker.PlayNote(969.0, 0.5, 1.0);
-        mySpeaker.PlayNote(800.0, 0.5, 1.0);
+    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
+