Ultraschall Test Geschwindigkeitsregelung
Fork of BertlLib by
Serial_HL.cpp@7:01d2bc098b6d, 2016-04-25 (annotated)
- Committer:
- TFuchsbichler
- Date:
- Mon Apr 25 06:55:29 2016 +0000
- Revision:
- 7:01d2bc098b6d
- Parent:
- 0:e0131e671ea0
Ultraschallsensor Test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hollegha2 | 0:e0131e671ea0 | 1 | |
hollegha2 | 0:e0131e671ea0 | 2 | #include "Serial_HL.h" |
hollegha2 | 0:e0131e671ea0 | 3 | #include <stdarg.h> |
hollegha2 | 0:e0131e671ea0 | 4 | #include <stdio.h> |
hollegha2 | 0:e0131e671ea0 | 5 | |
hollegha2 | 0:e0131e671ea0 | 6 | namespace mbed { |
hollegha2 | 0:e0131e671ea0 | 7 | |
hollegha2 | 0:e0131e671ea0 | 8 | SerialBLK::SerialBLK(PinName tx, PinName rx) { |
hollegha2 | 0:e0131e671ea0 | 9 | serial_init(&_serial, tx, rx); |
hollegha2 | 0:e0131e671ea0 | 10 | _baud = 9600; |
hollegha2 | 0:e0131e671ea0 | 11 | serial_irq_handler(&_serial, SerialBLK::_irq_handler, (uint32_t)this); |
hollegha2 | 0:e0131e671ea0 | 12 | } |
hollegha2 | 0:e0131e671ea0 | 13 | |
hollegha2 | 0:e0131e671ea0 | 14 | void SerialBLK::baud(int baudrate) { |
hollegha2 | 0:e0131e671ea0 | 15 | serial_baud(&_serial, baudrate); |
hollegha2 | 0:e0131e671ea0 | 16 | _baud = baudrate; |
hollegha2 | 0:e0131e671ea0 | 17 | } |
hollegha2 | 0:e0131e671ea0 | 18 | |
hollegha2 | 0:e0131e671ea0 | 19 | void SerialBLK::format(int bits, Parity parity, int stop_bits) { |
hollegha2 | 0:e0131e671ea0 | 20 | serial_format(&_serial, bits, (SerialParity)parity, stop_bits); |
hollegha2 | 0:e0131e671ea0 | 21 | } |
hollegha2 | 0:e0131e671ea0 | 22 | |
hollegha2 | 0:e0131e671ea0 | 23 | int SerialBLK::readable() { |
hollegha2 | 0:e0131e671ea0 | 24 | return serial_readable(&_serial); |
hollegha2 | 0:e0131e671ea0 | 25 | } |
hollegha2 | 0:e0131e671ea0 | 26 | |
hollegha2 | 0:e0131e671ea0 | 27 | int SerialBLK::writeable() { |
hollegha2 | 0:e0131e671ea0 | 28 | return serial_writable(&_serial); |
hollegha2 | 0:e0131e671ea0 | 29 | } |
hollegha2 | 0:e0131e671ea0 | 30 | |
hollegha2 | 0:e0131e671ea0 | 31 | void SerialBLK::attach(void (*fptr)(void), IrqType type) { |
hollegha2 | 0:e0131e671ea0 | 32 | if (fptr) { |
hollegha2 | 0:e0131e671ea0 | 33 | _irq[type].attach(fptr); |
hollegha2 | 0:e0131e671ea0 | 34 | serial_irq_set(&_serial, (SerialIrq)type, 1); |
hollegha2 | 0:e0131e671ea0 | 35 | } else { |
hollegha2 | 0:e0131e671ea0 | 36 | serial_irq_set(&_serial, (SerialIrq)type, 0); |
hollegha2 | 0:e0131e671ea0 | 37 | } |
hollegha2 | 0:e0131e671ea0 | 38 | } |
hollegha2 | 0:e0131e671ea0 | 39 | |
hollegha2 | 0:e0131e671ea0 | 40 | void SerialBLK::_irq_handler(uint32_t id, SerialIrq irq_type) { |
hollegha2 | 0:e0131e671ea0 | 41 | SerialBLK *obj = (SerialBLK*)id; |
hollegha2 | 0:e0131e671ea0 | 42 | obj->_irq[irq_type].call(); |
hollegha2 | 0:e0131e671ea0 | 43 | } |
hollegha2 | 0:e0131e671ea0 | 44 | |
hollegha2 | 0:e0131e671ea0 | 45 | int SerialBLK::GetChar() { |
hollegha2 | 0:e0131e671ea0 | 46 | return serial_getc(&_serial); |
hollegha2 | 0:e0131e671ea0 | 47 | } |
hollegha2 | 0:e0131e671ea0 | 48 | |
hollegha2 | 0:e0131e671ea0 | 49 | void SerialBLK::PutChar(int aCh) { |
hollegha2 | 0:e0131e671ea0 | 50 | serial_putc(&_serial, aCh); |
hollegha2 | 0:e0131e671ea0 | 51 | } |
hollegha2 | 0:e0131e671ea0 | 52 | |
hollegha2 | 0:e0131e671ea0 | 53 | void SerialBLK::Write(void* aData, uint32_t aLenBytes) |
hollegha2 | 0:e0131e671ea0 | 54 | { |
hollegha2 | 0:e0131e671ea0 | 55 | uint8_t* ptr = (uint8_t*)aData; |
hollegha2 | 0:e0131e671ea0 | 56 | for(int i=0; i<aLenBytes; i++) { |
hollegha2 | 0:e0131e671ea0 | 57 | this->PutChar(*ptr); ptr++; |
hollegha2 | 0:e0131e671ea0 | 58 | } |
hollegha2 | 0:e0131e671ea0 | 59 | } |
hollegha2 | 0:e0131e671ea0 | 60 | |
hollegha2 | 0:e0131e671ea0 | 61 | void SerialBLK::Read(void* aData, uint32_t aLenBytes) |
hollegha2 | 0:e0131e671ea0 | 62 | { |
hollegha2 | 0:e0131e671ea0 | 63 | uint8_t* ptr = (uint8_t*)aData; |
hollegha2 | 0:e0131e671ea0 | 64 | for(int i=0; i<aLenBytes; i++) { |
hollegha2 | 0:e0131e671ea0 | 65 | *ptr=this->GetChar(); ptr++; |
hollegha2 | 0:e0131e671ea0 | 66 | } |
hollegha2 | 0:e0131e671ea0 | 67 | } |
hollegha2 | 0:e0131e671ea0 | 68 | |
hollegha2 | 0:e0131e671ea0 | 69 | |
hollegha2 | 0:e0131e671ea0 | 70 | |
hollegha2 | 0:e0131e671ea0 | 71 | |
hollegha2 | 0:e0131e671ea0 | 72 | void SvProtocol::Puts(char* aCStr) |
hollegha2 | 0:e0131e671ea0 | 73 | { |
hollegha2 | 0:e0131e671ea0 | 74 | while( *aCStr != '\0' ) |
hollegha2 | 0:e0131e671ea0 | 75 | { |
hollegha2 | 0:e0131e671ea0 | 76 | if( *aCStr=='\n' ) |
hollegha2 | 0:e0131e671ea0 | 77 | _st->PutChar('\r'); |
hollegha2 | 0:e0131e671ea0 | 78 | _st->PutChar(*aCStr); |
hollegha2 | 0:e0131e671ea0 | 79 | aCStr++; |
hollegha2 | 0:e0131e671ea0 | 80 | } |
hollegha2 | 0:e0131e671ea0 | 81 | _st->PutChar(0); // terminate with 0 |
hollegha2 | 0:e0131e671ea0 | 82 | } |
hollegha2 | 0:e0131e671ea0 | 83 | |
hollegha2 | 0:e0131e671ea0 | 84 | static char sBuffer[50]; |
hollegha2 | 0:e0131e671ea0 | 85 | |
hollegha2 | 0:e0131e671ea0 | 86 | void SvProtocol::Printf(const char *format, ...) |
hollegha2 | 0:e0131e671ea0 | 87 | { |
hollegha2 | 0:e0131e671ea0 | 88 | va_list vArgs; |
hollegha2 | 0:e0131e671ea0 | 89 | va_start(vArgs, format); |
hollegha2 | 0:e0131e671ea0 | 90 | vsprintf(sBuffer, (char const *)format, vArgs); |
hollegha2 | 0:e0131e671ea0 | 91 | va_end(vArgs); |
hollegha2 | 0:e0131e671ea0 | 92 | Puts(sBuffer); |
hollegha2 | 0:e0131e671ea0 | 93 | } |
hollegha2 | 0:e0131e671ea0 | 94 | |
hollegha2 | 0:e0131e671ea0 | 95 | void SvProtocol::SvPrintf(const char *format, ...) |
hollegha2 | 0:e0131e671ea0 | 96 | { |
hollegha2 | 0:e0131e671ea0 | 97 | va_list vArgs; |
hollegha2 | 0:e0131e671ea0 | 98 | va_start(vArgs, format); |
hollegha2 | 0:e0131e671ea0 | 99 | vsprintf(sBuffer, (char const *)format, vArgs); |
hollegha2 | 0:e0131e671ea0 | 100 | va_end(vArgs); |
hollegha2 | 0:e0131e671ea0 | 101 | if( !svMessageON ) return; |
hollegha2 | 0:e0131e671ea0 | 102 | _st->PutChar(10); |
hollegha2 | 0:e0131e671ea0 | 103 | Puts(sBuffer); |
hollegha2 | 0:e0131e671ea0 | 104 | } |
hollegha2 | 0:e0131e671ea0 | 105 | |
hollegha2 | 0:e0131e671ea0 | 106 | void SvProtocol::WriteSV3p13(int aId, float aData) { |
hollegha2 | 0:e0131e671ea0 | 107 | // int16_t val = To3p13(aData); |
hollegha2 | 0:e0131e671ea0 | 108 | // PutChar(aId); Write(&val,2); |
hollegha2 | 0:e0131e671ea0 | 109 | } |
hollegha2 | 0:e0131e671ea0 | 110 | |
hollegha2 | 0:e0131e671ea0 | 111 | int SvProtocol::GetCommand() |
hollegha2 | 0:e0131e671ea0 | 112 | { |
hollegha2 | 0:e0131e671ea0 | 113 | uint8_t cmd = _st->GetChar(); |
hollegha2 | 0:e0131e671ea0 | 114 | if( cmd==1 ) |
hollegha2 | 0:e0131e671ea0 | 115 | { |
hollegha2 | 0:e0131e671ea0 | 116 | this->acqON = _st->GetChar(); |
hollegha2 | 0:e0131e671ea0 | 117 | if( this->acqON ) |
hollegha2 | 0:e0131e671ea0 | 118 | this->SvMessage("AcqON"); |
hollegha2 | 0:e0131e671ea0 | 119 | else |
hollegha2 | 0:e0131e671ea0 | 120 | this->SvMessage("AcqOFF"); |
hollegha2 | 0:e0131e671ea0 | 121 | return 0; |
hollegha2 | 0:e0131e671ea0 | 122 | } |
hollegha2 | 0:e0131e671ea0 | 123 | return cmd; |
hollegha2 | 0:e0131e671ea0 | 124 | } |
hollegha2 | 0:e0131e671ea0 | 125 | |
hollegha2 | 0:e0131e671ea0 | 126 | } // namespace mbed |
hollegha2 | 0:e0131e671ea0 | 127 | |
hollegha2 | 0:e0131e671ea0 | 128 | |
hollegha2 | 0:e0131e671ea0 | 129 | |
hollegha2 | 0:e0131e671ea0 | 130 |