Renamed

Dependencies:   mbed

Revision:
5:062962db7a48
Parent:
3:6237f9e5e798
--- a/main.cpp	Wed Feb 23 12:24:16 2022 +0000
+++ b/main.cpp	Tue Mar 29 10:06:20 2022 +0000
@@ -1,62 +1,123 @@
+/********************************************
+Send File to Sharp PC-1403(H)
+==========================================
+
+Author: Fabio Fumi
+Date:   04.03.2022
+
+This software comes with the GNU public licence (GPL).
+
+It is an adaptation to the Mbed OS of the 
+img2wav from the "Pocket Tools" suite
+https://www.peil-partner.de/ifhe.de/sharp/
+*********************************************/
 #include "mbed.h" 
 #include "send_pc1403.h"
-#include "bin_file.h" // in-memory BIN file (until I integrate an SD-Card to store it...=
-#include "bit_send.h"
+//#ifdef FILE_BUF_SIZE
+//volatile char debugOut[1024];
+//#endif
+RawSerial   pc(USBTX,USBRX); // better than Serial, when using interrupts
+DigitalOut  my_led(LED1);
+DigitalIn   my_button(USER_BUTTON);
+InterruptIn btn(USER_BUTTON);
+Timer       total_time;
+Ticker      blink;
+
+ // bin-to-wav conversion information (from Pocket Tools code)
+int       send_err;
+
+void ledBlink () {
+    my_led = !my_led;
+}
 
-Serial pc(USBTX,USBRX);
-DigitalOut led(LED1);
-Timer total_time;
-DigitalIn    my_button(USER_BUTTON);
+#ifdef FILE_BUF_SIZE 
+void printInfo() 
+{
+    int i ;
+    // printout info
+    pc.printf("totBytesReceived %d\n\r", totBytesReceived);
+    pc.printf("fileBufReceivePtr %d\n\r", fileBufReceivePtr);
+    for ( i=0; i<10; i++ ) 
+        pc.printf("0x%.2X ", fileOverSerialBuffer[i] );
+    pc.printf("...\n\r");
+    pc.printf("fileBufSendPtr %d\n\r", fileBufSendPtr);
+    //pc.printf("<%s>\n\r", debugOut);
+    pc.printf("fileReceiveComplete %d\n\r", fileReceiveComplete);
+    pc.printf("fileError %d\n\r", fileError);
+    pc.printf("fileInfo.total %d\n\r", fileInfo.total);
+    pc.printf("time (ms): %d [send_err %d]\n\r", total_time.read_us()/1000, send_err);
+    
+}
 
+// invoked at each character received over serial
+void serial_rx() {
+    
+    // getting data from serial
+    if ( totBytesReceived == 0 ) {
+        timeout.start(); // new file: enable a watchdog
+        timeout.reset();
+        blink.attach( &ledBlink, 0.05 ); // fast blink: receiving
+    }
+    // push character on the circular buffer
+    filePushData ( pc.getc() );
+    totBytesReceived++;
+    timeout.reset();
 
+}
+#endif
 
+ 
 int main()
 {
-
-    int i, error;
-    char inVal;
-    FileInfo  info ; // PC bin-to-wav conversion information
-    // flashing led on startup
-    for (i=0; i<10; i+=1) {
-        wait (0.1);
-        led = !led;
-    }
-    // init vars
-    led = 0;
-     
-    pc.baud(57600);
-
-    // welcome message 
-    pc.printf("\n\r ** MBED ** send to PC1403 ** \n\r");
+    
+    my_led = 0;
+    pc.baud(600); // file-over-serial can't go faster (sending to Sharp is about 500baud)
     fflush(stdout);
-    
+#ifdef FILE_BUF_SIZE 
+    btn.rise(&printInfo);
+    pc.attach(&serial_rx, Serial::RxIrq);
+    fileReceiveInit( );
+#endif
     // main loop
     while(true) {
-     
-        pc.printf(" (push user button to send) \n\r");
-        // wait for button pressed
+        
+        blink.attach( &ledBlink, 1.0 ); // slow blink - waiting for file
+        
+#ifdef FILE_BUF_SIZE 
+        // File-over-serial
+        // serial interrupt - pushing to buffer
+        // main thread - pulling from buffer
+        fileInfo.debug = 0x1000; // disable verbose printf while using serial
+        fileReceiveInit( ); 
+        // wait for data available, before starting to send  
+        while ( !totBytesReceived  ) 
+            wait (.1);
+#else
+        // hardcoded BIN file
+        pc.printf("Push user button to start sending \n\r"); 
+        // wait for button pressed, only for the hardcoded BIN file
         while ( my_button == 1 )
             wait ( .1 );
+#endif
         
+        // start sending new file to Sharp
+        fileInfo.ident = IDENT_NEW_BAS;
+        fileInfo.debug = 0x0040; // 0x0000 disable printf; 0x0040 DEBUG
+        bitHandlerInit(); // new bit stream being sent
         total_time.reset();
         total_time.start();
-    
-        ///////////////////////////////////////////////
-        // FILE SENDING
-        bitHandlerInit(); // before a new bit stream to be sent
-        info.ident = IDENT_NEW_BAS;
-        info.debug = 0x0040; // 0x0000 disable printf ;  0x0040 DEBUG
-        error = FileSend ( bin_file_name, bin_file_bytes, BIN_FILE_SIZE, &info ) ; 
-        pc.printf("\n\rFileSend completed - (%d)\n\r", error);
-        bitHandlerStop(); // stop consumer after last bit
-        while( bitHandlerRunning() )  // time for handler to complete
+        send_err = FileSend ( ) ;  // removed for debug !
+        bitHandlerStop(); // stop bit consumer, after last bit
+        while( bitHandlerRunning() )  // time for bit handler to complete
             wait (.1); 
-        ///////////////////////////////////////////////
-       
         total_time.stop();
-        pc.printf("DONE - time (ms): %d [send_err %d]\n\r", total_time.read_us()/1000, error);
+#ifdef FILE_BUF_SIZE 
+        printInfo() ;
+#endif
+        pc.printf("DONE - time (ms): %d [send_err %d]\n\r", total_time.read_us()/1000, send_err);
 
         // doing nothing...
-        wait(.1);              
+        wait(.1);       
+               
     }
 }
\ No newline at end of file