Interface layer for the mbed boards ready for the JAVA library
Dependencies: C12832 LM75B MMA7660 mbed FXOS8700Q
Fork of frdm_serial by
comms.cpp@5:d9f8c2f63323, 2015-12-04 (annotated)
- Committer:
- Condo2k4
- Date:
- Fri Dec 04 14:42:01 2015 +0000
- Revision:
- 5:d9f8c2f63323
- Parent:
- 4:39e949908fc5
- Child:
- 6:adf2837c1e7f
Final changes to MBed library.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Condo2k4 | 4:39e949908fc5 | 1 | #include "comms.h" |
Condo2k4 | 4:39e949908fc5 | 2 | |
Condo2k4 | 4:39e949908fc5 | 3 | DataSerial::DataSerial(PinName tx, PinName rx) : Serial(tx,rx,NULL) {} |
Condo2k4 | 4:39e949908fc5 | 4 | |
Condo2k4 | 4:39e949908fc5 | 5 | DataSerial::~DataSerial() {} |
Condo2k4 | 4:39e949908fc5 | 6 | |
Condo2k4 | 4:39e949908fc5 | 7 | void DataSerial::sendFloat(float &f) { |
Condo2k4 | 4:39e949908fc5 | 8 | sendData(&f,sizeof(float)); |
Condo2k4 | 4:39e949908fc5 | 9 | } |
Condo2k4 | 4:39e949908fc5 | 10 | |
Condo2k4 | 4:39e949908fc5 | 11 | void DataSerial::sendInt16(uint16_t &i) { |
Condo2k4 | 4:39e949908fc5 | 12 | sendData(&i,sizeof(uint16_t)); |
Condo2k4 | 4:39e949908fc5 | 13 | } |
Condo2k4 | 4:39e949908fc5 | 14 | |
Condo2k4 | 4:39e949908fc5 | 15 | void DataSerial::sendChar(char c) { |
Condo2k4 | 4:39e949908fc5 | 16 | putc(1); putc(c); |
Condo2k4 | 4:39e949908fc5 | 17 | } |
Condo2k4 | 4:39e949908fc5 | 18 | |
Condo2k4 | 4:39e949908fc5 | 19 | float DataSerial::readFloat() { |
Condo2k4 | 4:39e949908fc5 | 20 | float f; |
Condo2k4 | 4:39e949908fc5 | 21 | readData(&f,sizeof(float)); |
Condo2k4 | 4:39e949908fc5 | 22 | return f; |
Condo2k4 | 4:39e949908fc5 | 23 | } |
Condo2k4 | 4:39e949908fc5 | 24 | |
Condo2k4 | 4:39e949908fc5 | 25 | uint16_t DataSerial::readInt16() { |
Condo2k4 | 4:39e949908fc5 | 26 | uint16_t i; |
Condo2k4 | 4:39e949908fc5 | 27 | readData(&i,sizeof(uint16_t)); |
Condo2k4 | 4:39e949908fc5 | 28 | return i; |
Condo2k4 | 4:39e949908fc5 | 29 | } |
Condo2k4 | 4:39e949908fc5 | 30 | |
Condo2k4 | 4:39e949908fc5 | 31 | char DataSerial::readChar() { |
Condo2k4 | 5:d9f8c2f63323 | 32 | if(getc()!=1) readFailureMode(); |
Condo2k4 | 4:39e949908fc5 | 33 | return getc(); |
Condo2k4 | 4:39e949908fc5 | 34 | } |
Condo2k4 | 4:39e949908fc5 | 35 | |
Condo2k4 | 4:39e949908fc5 | 36 | void DataSerial::sendData(void* data, uint8_t len) { |
Condo2k4 | 4:39e949908fc5 | 37 | putc(len); |
Condo2k4 | 4:39e949908fc5 | 38 | for(char* c = (char*)data;len--;) |
Condo2k4 | 4:39e949908fc5 | 39 | putc(*(c++)); |
Condo2k4 | 4:39e949908fc5 | 40 | } |
Condo2k4 | 4:39e949908fc5 | 41 | |
Condo2k4 | 5:d9f8c2f63323 | 42 | uint8_t DataSerial::readString(char * s) { |
Condo2k4 | 5:d9f8c2f63323 | 43 | char l = getc(); |
Condo2k4 | 5:d9f8c2f63323 | 44 | uint8_t len = (uint8_t)l; |
Condo2k4 | 5:d9f8c2f63323 | 45 | while(l>0) { |
Condo2k4 | 5:d9f8c2f63323 | 46 | *s = getc(); |
Condo2k4 | 5:d9f8c2f63323 | 47 | s++; |
Condo2k4 | 5:d9f8c2f63323 | 48 | l--; |
Condo2k4 | 5:d9f8c2f63323 | 49 | } |
Condo2k4 | 5:d9f8c2f63323 | 50 | return len; |
Condo2k4 | 5:d9f8c2f63323 | 51 | } |
Condo2k4 | 5:d9f8c2f63323 | 52 | |
Condo2k4 | 4:39e949908fc5 | 53 | void DataSerial::readData(void* data, uint8_t len) { |
Condo2k4 | 5:d9f8c2f63323 | 54 | char readLen = getc(); |
Condo2k4 | 5:d9f8c2f63323 | 55 | if(readLen!=len) readFailureMode(); |
Condo2k4 | 4:39e949908fc5 | 56 | char* c = (char*)data; |
Condo2k4 | 4:39e949908fc5 | 57 | while(len>0) { |
Condo2k4 | 4:39e949908fc5 | 58 | *c = getc(); |
Condo2k4 | 4:39e949908fc5 | 59 | c++; |
Condo2k4 | 4:39e949908fc5 | 60 | len--; |
Condo2k4 | 4:39e949908fc5 | 61 | } |
Condo2k4 | 4:39e949908fc5 | 62 | } |
Condo2k4 | 4:39e949908fc5 | 63 | |
Condo2k4 | 4:39e949908fc5 | 64 | void DataSerial::sendSpecialCommand(char char1, char char2) { |
Condo2k4 | 4:39e949908fc5 | 65 | putc(0); putc(char1); putc(char2); |
Condo2k4 | 5:d9f8c2f63323 | 66 | } |
Condo2k4 | 5:d9f8c2f63323 | 67 | |
Condo2k4 | 5:d9f8c2f63323 | 68 | void DataSerial::readFailureMode() { |
Condo2k4 | 5:d9f8c2f63323 | 69 | DigitalOut lr(PTB22), lg(PTE26), lb(PTB21); |
Condo2k4 | 5:d9f8c2f63323 | 70 | lr=lg=1; lb=0; |
Condo2k4 | 5:d9f8c2f63323 | 71 | for(;;) { |
Condo2k4 | 5:d9f8c2f63323 | 72 | wait_ms(1000); |
Condo2k4 | 5:d9f8c2f63323 | 73 | lb=!lb; |
Condo2k4 | 5:d9f8c2f63323 | 74 | } |
Condo2k4 | 5:d9f8c2f63323 | 75 | |
Condo2k4 | 4:39e949908fc5 | 76 | } |