Function Generator for TINF 2021

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Serial_HL.cpp Source File

Serial_HL.cpp

00001 #include "Serial_HL.h"
00002 #include <stdarg.h>
00003 #include <stdio.h>
00004 
00005 namespace mbed {
00006 
00007 SerialBLK::SerialBLK(PinName tx, PinName rx) {
00008   serial_init(&_serial, tx, rx);
00009   _baud = 9600;
00010   serial_irq_handler(&_serial, SerialBLK::_irq_handler, (uint32_t)this);
00011 }
00012 
00013 void SerialBLK::baud(int baudrate) {
00014   serial_baud(&_serial, baudrate);
00015   _baud = baudrate;
00016 }
00017 
00018 void SerialBLK::format(int bits, Parity parity, int stop_bits) {
00019   serial_format(&_serial, bits, (SerialParity)parity, stop_bits);
00020 }
00021 
00022 int SerialBLK::readable() {
00023   return serial_readable(&_serial);
00024 }
00025 
00026 int SerialBLK::writeable() {
00027   return serial_writable(&_serial);
00028 }
00029 
00030 void SerialBLK::attach(void (*fptr)(void), IrqType type) {
00031   if (fptr) {
00032       _irq[type].attach(fptr);
00033       serial_irq_set(&_serial, (SerialIrq)type, 1);
00034   } else {
00035       serial_irq_set(&_serial, (SerialIrq)type, 0);
00036     }
00037 }
00038 
00039 void SerialBLK::_irq_handler(uint32_t id, SerialIrq irq_type) {
00040   SerialBLK *obj = (SerialBLK*)id;
00041   obj->_irq[irq_type].call();
00042 }
00043 
00044 int SerialBLK::GetChar() {
00045     return serial_getc(&_serial);
00046 }
00047 
00048 void SerialBLK::PutChar(int aCh) {
00049   serial_putc(&_serial, aCh);
00050 }
00051 
00052 void SerialBLK::Write(void* aData, uint32_t aLenBytes)
00053 {
00054     uint8_t* ptr = (uint8_t*)aData;
00055     for(int i=0; i<aLenBytes; i++) {
00056         this->PutChar(*ptr); ptr++;
00057     }
00058 }
00059 
00060 void SerialBLK::Read(void* aData, uint32_t aLenBytes)
00061 {
00062     uint8_t* ptr = (uint8_t*)aData;
00063   for(int i=0; i<aLenBytes; i++) {
00064     *ptr=this->GetChar(); ptr++;
00065   }
00066 }
00067 
00068 
00069 
00070 
00071 void SvProtocol::Puts(char* aCStr)
00072 {
00073   while( *aCStr != '\0' )
00074   {
00075     if( *aCStr=='\n' )
00076       _st->PutChar('\r');
00077     _st->PutChar(*aCStr);
00078     aCStr++;
00079   }
00080     _st->PutChar(0); // terminate with 0
00081 }
00082 
00083 static char sBuffer[50];
00084 
00085 void SvProtocol::Printf(const char *format, ...)
00086 {
00087   va_list vArgs;
00088   va_start(vArgs, format);
00089   vsprintf(sBuffer, (char const *)format, vArgs);
00090   va_end(vArgs);
00091   Puts(sBuffer);
00092 }
00093 
00094 void SvProtocol::SvPrintf(const char *format, ...)
00095 {
00096   va_list vArgs;
00097   va_start(vArgs, format);
00098   vsprintf(sBuffer, (char const *)format, vArgs);
00099   va_end(vArgs);
00100   if( !svMessageON ) return;
00101   _st->PutChar(10);
00102   Puts(sBuffer);
00103 }
00104 
00105 void SvProtocol::WriteSV3p13(int aId, float aData) {
00106   // int16_t val = To3p13(aData);
00107   // PutChar(aId); Write(&val,2);
00108 }
00109 
00110 int SvProtocol::GetCommand()
00111 {
00112   uint8_t cmd = _st->GetChar();
00113   if( cmd==1 )
00114   {
00115     this->acqON = _st->GetChar();
00116     if( this->acqON )
00117       this->SvMessage("AcqON");
00118     else
00119       this->SvMessage("AcqOFF");
00120     return 0;
00121   }
00122   return cmd;
00123 }
00124 
00125 } // namespace mbed