Christian Weiß
/
AnalogIn
AnalogIn
Revision 0:077d31186892, committed 2018-11-15
- Comitter:
- Wizo
- Date:
- Thu Nov 15 17:22:45 2018 +0000
- Commit message:
- AnalogIn
Changed in this revision
diff -r 000000000000 -r 077d31186892 ProcVisDemo.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ProcVisDemo.cpp Thu Nov 15 17:22:45 2018 +0000 @@ -0,0 +1,77 @@ +#include "mbed.h" +#include "Serial_HL.h" + +SerialBLK pc(USBTX, USBRX); +SvProtocol ua0(&pc); + +// V2.0 +// BusOut leds(LED1,LED2,LED3,LED4); Bertl14 +// M0-Board +BusOut leds(P1_13,P1_12,P1_7,P1_6,P1_4,P1_3,P1_1,P1_0,LED4,LED3,LED2,LED1); + +AnalogIn a1(p19), a2(p20); + +void CommandHandler(); + +int main(void) +{ + pc.format(8,SerialBLK::None,1); + pc.baud(115200); + leds = 9; + + ua0.SvMessage("AinDemo"); // Meldung zum PC senden + + int16_t sv1=0, sv2=100; float sv3=0; + Timer stw; + stw.start(); + while(1) { + CommandHandler(); + if( ua0.acqON && (stw.read_ms()>100) ) { // 10Hz + // dieser Teil wird mit 10Hz aufgerufen + stw.reset(); + sv1++; + sv2++; + sv3+=0.1; + if( ua0.acqON ) { + // nur wenn vom PC aus das Senden eingeschaltet wurde + // wird auch etwas gesendet + // >> 4 Korrektur der MBed-Lib für 12-Bit ADC + ua0.WriteSvI16(1, a1.read_u16() >> 4); + ua0.WriteSvI16(2, a2.read_u16() >> 4); + + } + } + } + return 1; +} + +void CommandHandler() +{ + uint8_t cmd; + int16_t idata1, idata2; + + // Fragen ob überhaupt etwas im RX-Reg steht + if( !pc.IsDataAvail() ) + return; + + // wenn etwas im RX-Reg steht + // Kommando lesen + cmd = ua0.GetCommand(); + + if( cmd==2 ) { + // cmd2 hat 2 int16 Parameter + idata1 = ua0.ReadI16(); + idata2 = ua0.ReadI16(); + // für die Analyse den Wert einfach nur zum PC zurücksenden + ua0.SvPrintf("Command2 %d %d", idata1, idata2); + } + + if( cmd==3 ) + { + leds = ua0.ReadI16(); + + ua0.SvMessage("SetLeds"); + } +} + +
diff -r 000000000000 -r 077d31186892 SerialHL/Serial_HL.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SerialHL/Serial_HL.cpp Thu Nov 15 17:22:45 2018 +0000 @@ -0,0 +1,130 @@ + +#include "Serial_HL.h" +#include <stdarg.h> +#include <stdio.h> + +namespace mbed { + +SerialBLK::SerialBLK(PinName tx, PinName rx) { + serial_init(&_serial, tx, rx); + _baud = 9600; + serial_irq_handler(&_serial, SerialBLK::_irq_handler, (uint32_t)this); +} + +void SerialBLK::baud(int baudrate) { + serial_baud(&_serial, baudrate); + _baud = baudrate; +} + +void SerialBLK::format(int bits, Parity parity, int stop_bits) { + serial_format(&_serial, bits, (SerialParity)parity, stop_bits); +} + +int SerialBLK::readable() { + return serial_readable(&_serial); +} + +int SerialBLK::writeable() { + return serial_writable(&_serial); +} + +void SerialBLK::attach(void (*fptr)(void), IrqType type) { + if (fptr) { + _irq[type].attach(fptr); + serial_irq_set(&_serial, (SerialIrq)type, 1); + } else { + serial_irq_set(&_serial, (SerialIrq)type, 0); + } +} + +void SerialBLK::_irq_handler(uint32_t id, SerialIrq irq_type) { + SerialBLK *obj = (SerialBLK*)id; + obj->_irq[irq_type].call(); +} + +int SerialBLK::GetChar() { + return serial_getc(&_serial); +} + +void SerialBLK::PutChar(int aCh) { + serial_putc(&_serial, aCh); +} + +void SerialBLK::Write(void* aData, uint32_t aLenBytes) +{ + uint8_t* ptr = (uint8_t*)aData; + for(int i=0; i<aLenBytes; i++) { + this->PutChar(*ptr); ptr++; + } +} + +void SerialBLK::Read(void* aData, uint32_t aLenBytes) +{ + uint8_t* ptr = (uint8_t*)aData; + for(int i=0; i<aLenBytes; i++) { + *ptr=this->GetChar(); ptr++; + } +} + + + + +void SvProtocol::Puts(char* aCStr) +{ + while( *aCStr != '\0' ) + { + if( *aCStr=='\n' ) + _st->PutChar('\r'); + _st->PutChar(*aCStr); + aCStr++; + } + _st->PutChar(0); // terminate with 0 +} + +static char sBuffer[50]; + +void SvProtocol::Printf(const char *format, ...) +{ + va_list vArgs; + va_start(vArgs, format); + vsprintf(sBuffer, (char const *)format, vArgs); + va_end(vArgs); + Puts(sBuffer); +} + +void SvProtocol::SvPrintf(const char *format, ...) +{ + va_list vArgs; + va_start(vArgs, format); + vsprintf(sBuffer, (char const *)format, vArgs); + va_end(vArgs); + if( !svMessageON ) return; + _st->PutChar(10); + Puts(sBuffer); +} + +void SvProtocol::WriteSV3p13(int aId, float aData) { + // int16_t val = To3p13(aData); + // PutChar(aId); Write(&val,2); +} + +int SvProtocol::GetCommand() +{ + uint8_t cmd = _st->GetChar(); + if( cmd==1 ) + { + this->acqON = _st->GetChar(); + if( this->acqON ) + this->SvMessage("AcqON"); + else + this->SvMessage("AcqOFF"); + return 0; + } + return cmd; +} + +} // namespace mbed + + + +
diff -r 000000000000 -r 077d31186892 SerialHL/Serial_HL.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SerialHL/Serial_HL.h Thu Nov 15 17:22:45 2018 +0000 @@ -0,0 +1,69 @@ + +#ifndef Serial_HL_h +#define Serial_HL_h + +#include "platform.h" +// #include "Stream.h" +#include "FunctionPointer.h" +#include "serial_api.h" +#include "SvProtocol.h" + +namespace mbed +{ + +class SerialBLK : public IStreamHL +{ +public: + SerialBLK(PinName tx, PinName rx); + void baud(int baudrate); + + virtual void PutChar(int aCh); + virtual int GetChar(); + virtual void Write(void* aData, uint32_t aLenBytes); + virtual void Read(void* aData, uint32_t aLenBytes); + + enum Parity { + None = 0, + Odd, + Even, + Forced1, + Forced0 + }; + enum IrqType { + RxIrq = 0, + TxIrq + }; + + void format(int bits=8, Parity parity=SerialBLK::None, int stop_bits=1); + + int readable(); + int writeable(); + int IsDataAvail() { + return readable(); + } + + // fptr A pointer to a void function, or 0 to set as none + // type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) + void attach(void (*fptr)(void), IrqType type=RxIrq); + + // tptr pointer to the object to call the member function on + // mptr pointer to the member function to be called + // type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) + template<typename T> + void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { + if((mptr != NULL) && (tptr != NULL)) { + _irq[type].attach(tptr, mptr); + serial_irq_set(&_serial, (SerialIrq)type, 1); + } + } + + static void _irq_handler(uint32_t id, SerialIrq irq_type); +protected: + serial_t _serial; + FunctionPointer _irq[2]; + int _baud; +}; + +} // namespace mbed + +#endif
diff -r 000000000000 -r 077d31186892 SerialHL/SvProtocol.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SerialHL/SvProtocol.h Thu Nov 15 17:22:45 2018 +0000 @@ -0,0 +1,83 @@ + +#ifndef SvProtocol_h +#define SvProtocol_h + +#include "stdint.h" + +namespace mbed { + +class IStreamHL { + public: + virtual void PutChar(int aCh) = 0; + virtual int GetChar() = 0; + virtual void Write(void* aData, uint32_t aLenBytes) = 0; + virtual void Read(void* aData, uint32_t aLenBytes) = 0; +}; + +class SvProtocol { + public: + IStreamHL* _st; + uint8_t acqON; + uint8_t svMessageON; + public: + SvProtocol(IStreamHL* aStrm) { + acqON=0; svMessageON=1; + _st=aStrm; + } + + // Check's first for acqOn/Off Command + // ret 0 if acqOn/Off was handled in GetCommand + int GetCommand(); + + void Puts(char* aCStr); // Terminate with 0 + + // \r\n is appended automatically + void Printf(const char* format, ...); + + void SvPrintf(const char *format, ...); + + void WriteSV(int aId, char* aData) { + if( !svMessageON ) return; + _st->PutChar(aId); Puts(aData); + } + + void SvMessage(char* aTxt) { + if( !svMessageON ) return; + _st->PutChar(10); Puts(aTxt); + } + + void VectHeader(int aId, int aNVals) + { _st->PutChar(aId); _st->PutChar(aNVals); } + + void WriteSvI16(int aId, int16_t aData) + { _st->PutChar(aId+10); _st->Write(&aData,2); } + + void WriteSvI32(int aId, int32_t aData) + { _st->PutChar(aId); _st->Write(&aData,4); } + + void WriteSV(int aId, float aData) + { _st->PutChar(aId+20); _st->Write(&aData,4); } + + // float in 3.13 Format + void WriteSV3p13(int aId, float aData); + + int16_t ReadI16() + { int16_t ret; _st->Read(&ret,2); return ret; } + + int32_t ReadI32() + { int32_t ret; _st->Read(&ret,4); return ret; } + + float ReadF() + { float ret; _st->Read(&ret,4); return ret; } +}; + +} // namespace mbed + +// SV-Id Ranges and DataTypes for SvVis3 Visualisation-Tool +//---------------------------------------------------------- +// Id = 10 : string +// Id = 1 .. 9 : format 3.13 2 Bytes +// Id = 11 .. 20 : short 2 Bytes +// Id = 21 .. 30 : float 4 Bytes + +#endif
diff -r 000000000000 -r 077d31186892 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Nov 15 17:22:45 2018 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/9ad691361fac \ No newline at end of file