PES 4 - Smart Medication Dispenser / PES4_ProgrammeforDesignReview2

Dependencies:   SDFileSystem mbed

Fork of PES4_Programme by PES 4 - Smart Medication Dispenser

Revision:
63:be13158bb4dd
Parent:
41:f054a83f9556
Child:
69:ec1cfc0cd161
--- a/source/main.cpp	Wed Mar 28 13:51:49 2018 +0000
+++ b/source/main.cpp	Thu Apr 05 09:27:23 2018 +0000
@@ -1,11 +1,37 @@
 #include "main.h"
 
 
-int state = 47;
+int state = 10;
 int oldState;
 
 Serial pc(USBTX, USBRX); // tx, rx
 
+
+// Circular buffers for serial TX and RX data - used by interrupt routines
+const int buffer_size = 255;
+// might need to increase buffer size for high baud rates
+char tx_buffer[buffer_size+1];
+char rx_buffer[buffer_size+1];
+// Circular buffer pointers
+// volatile makes read-modify-write atomic
+volatile int tx_in=0;
+volatile int tx_out=0;
+volatile int rx_in=0;
+volatile int rx_out=0;
+// Line buffers for sprintf and sscanf
+char tx_line[80];
+char rx_line[80];
+
+int i=0;
+int rx_i=0;
+
+// Setup a serial interrupt function to receive data
+pc.attach(&Rx_interrupt, Serial::RxIrq);
+// Setup a serial interrupt function to transmit data
+pc.attach(&Tx_interrupt, Serial::TxIrq);
+
+
+
 /* PWM Test Tool */
 int channel;
 int dutyCycle;
@@ -33,6 +59,38 @@
                 break;
 
             case 10:
+                // Formatted IO test using send and receive serial interrupts
+                // with sprintf and sscanf
+                while (1) {
+                    // Loop to generate different test values - send value in hex, decimal, and octal and  then read back
+                    for (i=0; i<0xFFFF; i++) {
+                        // Print ASCII number to tx line buffer in hex
+                        sprintf(tx_line,"%x\r\n",i);
+                        // Copy tx line buffer to large tx buffer for tx interrupt routine
+                        send_line();
+                        // Print ASCII number to tx line buffer in decimal
+                        sprintf(tx_line,"%d\r\n",i);
+                        // Copy tx line buffer to large tx buffer for tx interrupt routine
+                        send_line();
+                        // Print ASCII number to tx line buffer in octal
+                        sprintf(tx_line,"%o\r\n",i);
+                        // Copy tx line buffer to large tx buffer for tx interrupt routine
+                        send_line();
+
+                        // Read a line from the large rx buffer from rx interrupt routine
+                        read_line();
+                        // Read ASCII number from rx line buffer
+                        sscanf(rx_line,"%x",&rx_i);
+                        // Read a line from the large rx buffer from rx interrupt routine
+                        read_line();
+                        // Read ASCII number from rx line buffer
+                        sscanf(rx_line,"%d",&rx_i);
+                        // Read a line from the large rx buffer from rx interrupt routine
+                        read_line();
+                        // Read ASCII number from rx line buffer
+                        sscanf(rx_line,"%o",&rx_i);
+                    }
+                }
                 break;
             case 11:
                 break;