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.
Dependencies: mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP
Fork of SystemManagement by
Libs/Xbee/Xbee.cpp@30:91af74a299e1, 2014-11-13 (annotated)
- Committer:
- pspatel321
- Date:
- Thu Nov 13 10:53:10 2014 +0000
- Revision:
- 30:91af74a299e1
Parth's edits for the week.; DC-DC completed and fixed, IMD updated, LatchMonitor and Temperature added. Serial dashboard updated. File structure changed Everything tested. Compiles and runs.; Still need to write CAN in/out interface.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| pspatel321 | 30:91af74a299e1 | 1 | #include "Xbee.h" |
| pspatel321 | 30:91af74a299e1 | 2 | |
| pspatel321 | 30:91af74a299e1 | 3 | Xbee::Xbee(PinName tx, PinName rx, int baudrate, int bufferSize, char _delim) : xbee(tx, rx, bufferSize) { |
| pspatel321 | 30:91af74a299e1 | 4 | xbee.baud(baudrate); |
| pspatel321 | 30:91af74a299e1 | 5 | charCounter = 0; |
| pspatel321 | 30:91af74a299e1 | 6 | numMessagesIn = 0; |
| pspatel321 | 30:91af74a299e1 | 7 | numMessagesOut = 0; |
| pspatel321 | 30:91af74a299e1 | 8 | delim = _delim; |
| pspatel321 | 30:91af74a299e1 | 9 | |
| pspatel321 | 30:91af74a299e1 | 10 | // Attach callback when a new message delimiter character arrives |
| pspatel321 | 30:91af74a299e1 | 11 | xbee.autoDetectChar(delim); |
| pspatel321 | 30:91af74a299e1 | 12 | xbee.attach(this, &Xbee::newMsg, MODSERIAL::RxAutoDetect); |
| pspatel321 | 30:91af74a299e1 | 13 | } |
| pspatel321 | 30:91af74a299e1 | 14 | bool Xbee::send(CANMessage& msg) { |
| pspatel321 | 30:91af74a299e1 | 15 | if (!xbee.writeable()) return false; // Exit, txBuffer full |
| pspatel321 | 30:91af74a299e1 | 16 | |
| pspatel321 | 30:91af74a299e1 | 17 | // Format as string |
| pspatel321 | 30:91af74a299e1 | 18 | char str[100]; |
| pspatel321 | 30:91af74a299e1 | 19 | |
| pspatel321 | 30:91af74a299e1 | 20 | // Will it fit? |
| pspatel321 | 30:91af74a299e1 | 21 | if (xbee.txBufferGetSize(0) - xbee.txBufferGetCount() > strlen(str)){ |
| pspatel321 | 30:91af74a299e1 | 22 | xbee.printf("%s\n", str); |
| pspatel321 | 30:91af74a299e1 | 23 | numMessagesOut++; |
| pspatel321 | 30:91af74a299e1 | 24 | return true; |
| pspatel321 | 30:91af74a299e1 | 25 | |
| pspatel321 | 30:91af74a299e1 | 26 | // Don't send |
| pspatel321 | 30:91af74a299e1 | 27 | } else { |
| pspatel321 | 30:91af74a299e1 | 28 | return false; |
| pspatel321 | 30:91af74a299e1 | 29 | } |
| pspatel321 | 30:91af74a299e1 | 30 | } |
| pspatel321 | 30:91af74a299e1 | 31 | bool Xbee::receive(CANMessage& msg) { |
| pspatel321 | 30:91af74a299e1 | 32 | char str[100]; |
| pspatel321 | 30:91af74a299e1 | 33 | unsigned int i = 0; |
| pspatel321 | 30:91af74a299e1 | 34 | |
| pspatel321 | 30:91af74a299e1 | 35 | if (charCounter == 0) return false; // No messages yet |
| pspatel321 | 30:91af74a299e1 | 36 | else { |
| pspatel321 | 30:91af74a299e1 | 37 | while (xbee.readable()) { // Build string until buffer is empty or delimiter char is found |
| pspatel321 | 30:91af74a299e1 | 38 | char c = xbee.getc(); |
| pspatel321 | 30:91af74a299e1 | 39 | str[i] = c; |
| pspatel321 | 30:91af74a299e1 | 40 | i++; |
| pspatel321 | 30:91af74a299e1 | 41 | if (c == delim) { |
| pspatel321 | 30:91af74a299e1 | 42 | charCounter--; |
| pspatel321 | 30:91af74a299e1 | 43 | str[i-1] = '\0'; // Null terminator in place of delimiter char |
| pspatel321 | 30:91af74a299e1 | 44 | break; |
| pspatel321 | 30:91af74a299e1 | 45 | } |
| pspatel321 | 30:91af74a299e1 | 46 | } |
| pspatel321 | 30:91af74a299e1 | 47 | } |
| pspatel321 | 30:91af74a299e1 | 48 | |
| pspatel321 | 30:91af74a299e1 | 49 | // Process string into CAN Message |
| pspatel321 | 30:91af74a299e1 | 50 | int len = strlen(str); |
| pspatel321 | 30:91af74a299e1 | 51 | |
| pspatel321 | 30:91af74a299e1 | 52 | numMessagesIn++; |
| pspatel321 | 30:91af74a299e1 | 53 | return true; |
| pspatel321 | 30:91af74a299e1 | 54 | } |
| pspatel321 | 30:91af74a299e1 | 55 | void Xbee::newMsg(MODSERIAL_IRQ_INFO *q) { |
| pspatel321 | 30:91af74a299e1 | 56 | charCounter++; |
| pspatel321 | 30:91af74a299e1 | 57 | } |
