Christian Weiß / Mbed 2 deprecated Diplomarbeit_MW_CW

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Serial_HL.cpp Source File

Serial_HL.cpp

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