Interface layer for the mbed boards ready for the JAVA library

Dependencies:   C12832 LM75B MMA7660 mbed FXOS8700Q

Fork of frdm_serial by Michael Berry

Committer:
Condo2k4
Date:
Fri Jan 06 11:08:56 2017 +0000
Revision:
8:d70e3e3690fd
Parent:
6:adf2837c1e7f
Extended LCD functionality.; Temporarily disable LEDs

Who changed what in which revision?

UserRevisionLine numberNew 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 6:adf2837c1e7f 19 void DataSerial::sendBool(bool b) {
Condo2k4 6:adf2837c1e7f 20 putc(1); putc(b ? 't' : 'f');
Condo2k4 6:adf2837c1e7f 21 }
Condo2k4 6:adf2837c1e7f 22
Condo2k4 4:39e949908fc5 23 float DataSerial::readFloat() {
Condo2k4 4:39e949908fc5 24 float f;
Condo2k4 4:39e949908fc5 25 readData(&f,sizeof(float));
Condo2k4 4:39e949908fc5 26 return f;
Condo2k4 4:39e949908fc5 27 }
Condo2k4 4:39e949908fc5 28
Condo2k4 4:39e949908fc5 29 uint16_t DataSerial::readInt16() {
Condo2k4 4:39e949908fc5 30 uint16_t i;
Condo2k4 4:39e949908fc5 31 readData(&i,sizeof(uint16_t));
Condo2k4 4:39e949908fc5 32 return i;
Condo2k4 4:39e949908fc5 33 }
Condo2k4 4:39e949908fc5 34
Condo2k4 4:39e949908fc5 35 char DataSerial::readChar() {
Condo2k4 5:d9f8c2f63323 36 if(getc()!=1) readFailureMode();
Condo2k4 4:39e949908fc5 37 return getc();
Condo2k4 4:39e949908fc5 38 }
Condo2k4 4:39e949908fc5 39
Condo2k4 4:39e949908fc5 40 void DataSerial::sendData(void* data, uint8_t len) {
Condo2k4 4:39e949908fc5 41 putc(len);
Condo2k4 4:39e949908fc5 42 for(char* c = (char*)data;len--;)
Condo2k4 4:39e949908fc5 43 putc(*(c++));
Condo2k4 4:39e949908fc5 44 }
Condo2k4 4:39e949908fc5 45
Condo2k4 5:d9f8c2f63323 46 uint8_t DataSerial::readString(char * s) {
Condo2k4 5:d9f8c2f63323 47 char l = getc();
Condo2k4 5:d9f8c2f63323 48 uint8_t len = (uint8_t)l;
Condo2k4 5:d9f8c2f63323 49 while(l>0) {
Condo2k4 5:d9f8c2f63323 50 *s = getc();
Condo2k4 5:d9f8c2f63323 51 s++;
Condo2k4 5:d9f8c2f63323 52 l--;
Condo2k4 5:d9f8c2f63323 53 }
Condo2k4 5:d9f8c2f63323 54 return len;
Condo2k4 5:d9f8c2f63323 55 }
Condo2k4 5:d9f8c2f63323 56
Condo2k4 4:39e949908fc5 57 void DataSerial::readData(void* data, uint8_t len) {
Condo2k4 5:d9f8c2f63323 58 char readLen = getc();
Condo2k4 5:d9f8c2f63323 59 if(readLen!=len) readFailureMode();
Condo2k4 4:39e949908fc5 60 char* c = (char*)data;
Condo2k4 4:39e949908fc5 61 while(len>0) {
Condo2k4 4:39e949908fc5 62 *c = getc();
Condo2k4 4:39e949908fc5 63 c++;
Condo2k4 4:39e949908fc5 64 len--;
Condo2k4 4:39e949908fc5 65 }
Condo2k4 4:39e949908fc5 66 }
Condo2k4 4:39e949908fc5 67
Condo2k4 4:39e949908fc5 68 void DataSerial::sendSpecialCommand(char char1, char char2) {
Condo2k4 4:39e949908fc5 69 putc(0); putc(char1); putc(char2);
Condo2k4 5:d9f8c2f63323 70 }
Condo2k4 5:d9f8c2f63323 71
Condo2k4 5:d9f8c2f63323 72 void DataSerial::readFailureMode() {
Condo2k4 5:d9f8c2f63323 73 DigitalOut lr(PTB22), lg(PTE26), lb(PTB21);
Condo2k4 5:d9f8c2f63323 74 lr=lg=1; lb=0;
Condo2k4 5:d9f8c2f63323 75 for(;;) {
Condo2k4 5:d9f8c2f63323 76 wait_ms(1000);
Condo2k4 5:d9f8c2f63323 77 lb=!lb;
Condo2k4 5:d9f8c2f63323 78 }
Condo2k4 5:d9f8c2f63323 79
Condo2k4 4:39e949908fc5 80 }