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.
Arduino.cpp@0:d93ce07131fc, 2018-04-07 (annotated)
- Committer:
- ruke1ire
- Date:
- Sat Apr 07 04:11:19 2018 +0000
- Revision:
- 0:d93ce07131fc
- Child:
- 1:d96590319bcf
arduino;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ruke1ire | 0:d93ce07131fc | 1 | #include "mbed.h" |
ruke1ire | 0:d93ce07131fc | 2 | #include "Arduino.h" |
ruke1ire | 0:d93ce07131fc | 3 | |
ruke1ire | 0:d93ce07131fc | 4 | |
ruke1ire | 0:d93ce07131fc | 5 | Arduino::Arduino(PinName tx, PinName rx) : _tx(tx), _rx(rx){ |
ruke1ire | 0:d93ce07131fc | 6 | _arduino.baud(9600); |
ruke1ire | 0:d93ce07131fc | 7 | } |
ruke1ire | 0:d93ce07131fc | 8 | void Arduino::_sendCommand(char com){ |
ruke1ire | 0:d93ce07131fc | 9 | _arduino.clc(); |
ruke1ire | 0:d93ce07131fc | 10 | _arduino.printf("&c", com); |
ruke1ire | 0:d93ce07131fc | 11 | } |
ruke1ire | 0:d93ce07131fc | 12 | |
ruke1ire | 0:d93ce07131fc | 13 | int Arduino::_recieveData(){ |
ruke1ire | 0:d93ce07131fc | 14 | char value[100]; |
ruke1ire | 0:d93ce07131fc | 15 | int index=0; |
ruke1ire | 0:d93ce07131fc | 16 | char ch; |
ruke1ire | 0:d93ce07131fc | 17 | do |
ruke1ire | 0:d93ce07131fc | 18 | { |
ruke1ire | 0:d93ce07131fc | 19 | if (_arduino.readable()) // if there is an character to read from the device |
ruke1ire | 0:d93ce07131fc | 20 | { |
ruke1ire | 0:d93ce07131fc | 21 | ch = _arduino.getc(); // read it |
ruke1ire | 0:d93ce07131fc | 22 | if (index<99) // just to avoid buffer overflow |
ruke1ire | 0:d93ce07131fc | 23 | value[index++]=ch; // put it into the value array and increment the index |
ruke1ire | 0:d93ce07131fc | 24 | } |
ruke1ire | 0:d93ce07131fc | 25 | } while (ch!='\n'); // loop until the '\n' character |
ruke1ire | 0:d93ce07131fc | 26 | |
ruke1ire | 0:d93ce07131fc | 27 | value[index]='\x0'; // add un 0 to end the c string |
ruke1ire | 0:d93ce07131fc | 28 | return atoi(value); |
ruke1ire | 0:d93ce07131fc | 29 | } |
ruke1ire | 0:d93ce07131fc | 30 | |
ruke1ire | 0:d93ce07131fc | 31 | int Arduino::getLdrY(){ |
ruke1ire | 0:d93ce07131fc | 32 | _sendCommand('y'); |
ruke1ire | 0:d93ce07131fc | 33 | return _recieveData(); |
ruke1ire | 0:d93ce07131fc | 34 | } |
ruke1ire | 0:d93ce07131fc | 35 | |
ruke1ire | 0:d93ce07131fc | 36 | int Arduino::getLdrX(){ |
ruke1ire | 0:d93ce07131fc | 37 | _sendCommand('x'); |
ruke1ire | 0:d93ce07131fc | 38 | return _recieveData(); |
ruke1ire | 0:d93ce07131fc | 39 | } |
ruke1ire | 0:d93ce07131fc | 40 | |
ruke1ire | 0:d93ce07131fc | 41 | int Arduino::getColor(){ |
ruke1ire | 0:d93ce07131fc | 42 | _sendCommand('c'); |
ruke1ire | 0:d93ce07131fc | 43 | return _recieveData(); |
ruke1ire | 0:d93ce07131fc | 44 | } |
ruke1ire | 0:d93ce07131fc | 45 | |
ruke1ire | 0:d93ce07131fc | 46 | int Arduino::getOrientation(){ |
ruke1ire | 0:d93ce07131fc | 47 | _sendCommand('a'); |
ruke1ire | 0:d93ce07131fc | 48 | return _recieveData(); |
ruke1ire | 0:d93ce07131fc | 49 | } |
ruke1ire | 0:d93ce07131fc | 50 |