Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of 2015robot_main by
Diff: communicate.h
- Revision:
- 44:315d5960f18c
- Parent:
- 26:8e6c736b6791
- Child:
- 51:cb430192b28b
--- a/communicate.h Thu Sep 24 06:08:11 2015 +0000 +++ b/communicate.h Thu Sep 24 07:02:06 2015 +0000 @@ -12,6 +12,163 @@ } } +/***IM920 Communication.***/ +/*char rcvBUFF_IM920[40], rcvBUFF_SBDBT[40]; +int rcvCOUNT_IM920 = 0, rcvCOUNT_SBDBT = 0; +int rcvFLAG = 0; +int sendFLAG = 0; +int deadCOUNT = 0; +int oldDATA; +unsigned int cm, a2, b, X, Y; +unsigned int sdDATA = 0; +int LED = 0; + +Serial IM920_USART(p9, p10); +DigitalIn BUSY(p8); + +Ticker DTimer; + +void DEVICE_TX(int DATA, int DEVICE = 0) { + char sDATA[2]; + if (DATA <= 15) + sprintf(sDATA, "0%X", DATA); + else + sprintf(sDATA, "%X", DATA); + + if (DEVICE == 1) { + IM920_USART.printf("TXDA %s\r\n", sDATA); + } + else if (DEVICE == 2) { + SBDBT_USART.printf("00000000000%s\r\n", sDATA); + } + else { + IM920_USART.printf("TXDA %s\r\n", sDATA); + SBDBT_USART.printf("00000000000%s\r\n", sDATA); + } +} + +int cv(int Buzzer, int LED) { + if (Buzzer > 1) + Buzzer = 1; + else if (Buzzer < 0) + Buzzer = 0; + if (LED < 0) + LED = 0; + else if (LED >= 16) + LED = 15; + return (Buzzer << 6) | (LED << 2); +} + +void cvrecvDATA(char *buffDATA, unsigned long *outputDATA) { + char s1[2], s2[2], s3[2], s4[2]; + strncpy(s1, buffDATA+11, 2); + strncpy(s2, buffDATA+14, 2); + strncpy(s3, buffDATA+17, 2); + strncpy(s4, buffDATA+20, 2); + outputDATA[0] = strtoul(s1, (char **) NULL, 16); + outputDATA[1] = strtoul(s2, (char **) NULL, 16); + outputDATA[2] = strtoul(s3, (char **) NULL, 16); + outputDATA[3] = strtoul(s4, (char **) NULL, 16); +} + +void cvrecvDATAsbdbt(char *buffDATA, unsigned long *outputDATA) { + char s1[2], s2[2]; + strncpy(s1, buffDATA+5, 2); + strncpy(s2, buffDATA+7, 2); + outputDATA[0] = strtoul(s1, (char **) NULL, 16); + outputDATA[1] = strtoul(s2, (char **) NULL, 16); +} + +void readDATA(unsigned long* outDATA) { + cm = (outDATA[0] & 128) >> 7; + a2 = (outDATA[0] & 64) >> 6; + X = (outDATA[0] & 56) >> 3; + Y = outDATA[0] & 7; + b = outDATA[1]; +} + +void SBDBT_RX() { + char rcvDATA; + rcvDATA = SBDBT_USART.getc(); + rcvBUFF_SBDBT[rcvCOUNT_SBDBT] = rcvDATA; + rcvCOUNT_SBDBT++; + if (rcvDATA == 0x0A) { + rcvCOUNT_SBDBT = 0; + rcvFLAG = 2; + } + else if (rcvCOUNT_SBDBT >= 40) { + rcvCOUNT_SBDBT = 0; + memset(rcvBUFF_SBDBT, '\0', 40); + } +} + +void IM920_RX() { + char rcvDATA; + rcvDATA = IM920_USART.getc(); + rcvBUFF_IM920[rcvCOUNT_IM920] = rcvDATA; + rcvCOUNT_IM920++; + if (rcvDATA == 0x0A) { + rcvCOUNT_IM920 = 0; + rcvFLAG = 1; + } + else if (rcvCOUNT_IM920 >= 40) { + rcvCOUNT_IM920 = 0; + memset(rcvBUFF_IM920, '\0', 40); + } +} + +void deadCheck() { + if (rcvFLAG == 0 && deadCOUNT >= 5) { + PC.printf("DEAD\r\n"); + deadCOUNT = 0; + } + else if (rcvFLAG == 0 && deadCOUNT >= 2) { + deadCOUNT++; + DEVICE_TX(); + } + else if (rcvFLAG == 0) { + deadCOUNT++; + } + sdDATA = cvsendDATA(0, LED); +} + +inline void initializeIM920(){ + IM920_USART.baud(9600); + IM920_USART.format(8, Serial::None, 1); + IM920_USART.attach(IM920_RX, Serial::RxIrq); + SBDBT_USART.baud(9600); + SBDBT_USART.format(8, Serial::None, 1); + SBDBT_USART.attach(SBDBT_RX, Serial::RxIrq); + DTimer.attach(deadCheck, 0.3); + sdDATA = cvsendDATA(0, 0); + unsigned long outDATA[4]; +} + +inline void readIM920(){ + if (rcvFLAG == 1) { + if (strlen(rcvBUFF_IM920) > 8) { + cvrecvDATA(rcvBUFF_IM920, outDATA); + readDATA(outDATA); + PC.printf("A2 = %d, X = %d, Y = %d, B = %d \r\n", a2, X, Y, b); + deadCOUNT = 0; + DEVICE_TX(sdDATA, 1); + } + memset(rcvBUFF_IM920, '\0', 40); + rcvFLAG = 0; + } + else if (rcvFLAG == 2) { + if (strlen(rcvBUFF_SBDBT) > 4) { + cvrecvDATAsbdbt(rcvBUFF_SBDBT, outDATA); + readDATA(outDATA); + PC.printf("A2 = %d, X = %d, Y = %d, B = %d \r\n", a2, X, Y, b); + deadCOUNT = 0; + DEVICE_TX(sdDATA, 2); + } + memset(rcvBUFF_SBDBT, '\0', 40); + rcvFLAG = 0; + } +}*/ + /***Get state ps3con.***/ #define SBDBT_TX p28 #define SBDBT_RX p27