Treehouse Mbed Team / Mbed 2 deprecated 1U5_proto_X

Dependencies:   mbed

src/serial.cpp

Committer:
Slord2142
Date:
2022-01-27
Revision:
0:b3410a1e9843
Child:
1:bc3509459a27

File content as of revision 0:b3410a1e9843:

#include "mbed.h"
#include "serial.h"
#include "stdio.h"
#include "string.h"
#include "globals.h"
#include "all_io.h"

/************* GLOBAL VARIABLES *****************************/
struct serialStatusBits serialStatus;
char strbuf[150] = {0};
char rxbuf[MAXRXBUF+1] = {0};
volatile unsigned int bufloc = 0;

/************* FORWARD DECLARATIONS *************************/
void Rx_IRQHandler(void);


// set up serial port for 115200 baud
//Serial dcm1(USBTX, USBRX, 115200);
//Serial dcm1(USBTX, USBRX, 921600);
Serial dcm1(PC_10, PC_11, 115200);


// initialize the serial port variables
void initSerial(void)
{
   // create the RxIRQ callback
   dcm1.attach(&Rx_IRQHandler, Serial::RxIrq);
   
}
   

// this is the interrupt request handler (IRQ) for ALL Recoev interrupts
void Rx_IRQHandler(void)
{
   // reinit the repeat flag
   serialStatus.repeat = FALSE;

   // check if the USART1 receive interrupt flag was set
   while (dcm1.readable())
   {
      //led_usb = 0;
      rxbuf[bufloc] = dcm1.getc();

      if (rxbuf[bufloc] == CR)        // check for end of command
      {
         serialStatus.command = TRUE;
         rxbuf[bufloc] = 0;
         bufloc = 0;
         return;
      }
      else if (rxbuf[bufloc] == '$')  // check for computer command
      {
         serialStatus.computer = TRUE;
         bufloc = 0; // reset the buffer if computer command
      }
      else if (rxbuf[bufloc] == ')')  // check for repeat command
      {
         serialStatus.repeat = TRUE;
         rxbuf[bufloc] = 0;
      }

      // Echo character back out the serial port
      if (!serialStatus.computer)
      {
         dcm1.putc((unsigned char)rxbuf[bufloc]);
      }

      // handle backspace
      if (rxbuf[bufloc] != 0x08)
      {
         bufloc++; // increment the location
      }
      else
      {
         if (bufloc > 0)
         {
            rxbuf[--bufloc] = 0;  // set to null
         }
         else
         {
            rxbuf[bufloc] = 0;  // set to null
         }
      }
      
      // check for overflow
      if (bufloc >= MAXRXBUF)
      {
         bufloc = 0;
         rxbuf[0] = 0;
      }
      
      //led_usb = 1;
   }
}


// sends carriage return and linefeed and prompt character
/*
void sendCRLF(void)
{
 char strMenu[30] ={0};

    menu_type = MENU_DCM1;

    switch(menuType)
    {
      case  MENU_DCM1: strcpy(strMenu,"DCM1"); break;
      case  MENU_DIFFERENTIAL: strcpy(strMenu,"DIFF"); break;
      case  MENU_SINGLE: strcpy(strMenu,"SINGLE"); break;
      case  MENU_DUAL: strcpy(strMenu,"DUAL"); break;
      case  MENU_INVERT: strcpy(strMenu,"INVERT"); break;
      case  MENU_SETTINGS: strcpy(strMenu,"SET"); break;
      case  MENU_SETTINGS_CHAN1: strcpy(strMenu,"SET CHAN1"); break;
      case  MENU_SETTINGS_CHAN2: strcpy(strMenu,"SET CHAN2"); break;
      case  MENU_SETTINGS_CLOCK: strcpy(strMenu,"SET CLOCK"); break;
      case  MENU_CALIBRATE: strcpy(strMenu,"CAL"); break;
      case  MENU_TEST: strcpy(strMenu,"TEST"); break;
      case  MENU_MAIN: strcpy(strMenu,"MAIN"); break;
      default: strcpy(strMenu,"UNDF"); break;
    }

    // append the CR,LF and the ready character
     if(serialStatus.computer)
     {
         sendSerial("\r");
     }
     else
     {
        // adds a carrot so you know you are talking in terminal
        sprintf(strbuf,"\n\r%s>",strMenu);
        sendSerial(strbuf);
        // sendSerial("\n\r>");
     }
}*/

// send buffer to the serial port
void sendSerial(char *buf)
{
    //led_usb = 0;
    dcm1.printf(buf);
    //led_usb = 1;
}

/************************************************************
* Routine: showRangeError
* Input:   error -- 0 = in range, 1 = out of range
* Output:  none
* Description:
* Sends a floating point number (value) over the serial port
* if it is being retrieved (GET)
*
**************************************************************/
void showRangeError(int intValue, int iBadValue, float fBadValue)
{
   if (intValue){ 
      sprintf(strbuf, " Out of Range: %d",    iBadValue);
   }else{
      sprintf(strbuf, " Out of Range: %0.3f", fBadValue);
   }

   sendSerial(strbuf);
}