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@1:d96590319bcf, 2018-04-07 (annotated)
- Committer:
- sangrit
- Date:
- Sat Apr 07 07:40:13 2018 +0000
- Revision:
- 1:d96590319bcf
- Parent:
- 0:d93ce07131fc
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 | |
sangrit | 1:d96590319bcf | 5 | Arduino::Arduino(PinName tx, PinName rx) : _arduino(tx,rx){ |
ruke1ire | 0:d93ce07131fc | 6 | _arduino.baud(9600); |
ruke1ire | 0:d93ce07131fc | 7 | } |
sangrit | 1:d96590319bcf | 8 | void Arduino::sendCommand(char com){ |
sangrit | 1:d96590319bcf | 9 | //_arduino.clc(); |
ruke1ire | 0:d93ce07131fc | 10 | _arduino.printf("&c", com); |
ruke1ire | 0:d93ce07131fc | 11 | } |
ruke1ire | 0:d93ce07131fc | 12 | |
sangrit | 1:d96590319bcf | 13 | int Arduino::receiveData(){ |
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(){ |
sangrit | 1:d96590319bcf | 32 | sendCommand('y'); |
sangrit | 1:d96590319bcf | 33 | return this->receiveData(); |
ruke1ire | 0:d93ce07131fc | 34 | } |
ruke1ire | 0:d93ce07131fc | 35 | |
ruke1ire | 0:d93ce07131fc | 36 | int Arduino::getLdrX(){ |
sangrit | 1:d96590319bcf | 37 | sendCommand('x'); |
sangrit | 1:d96590319bcf | 38 | return receiveData(); |
ruke1ire | 0:d93ce07131fc | 39 | } |
ruke1ire | 0:d93ce07131fc | 40 | |
ruke1ire | 0:d93ce07131fc | 41 | int Arduino::getColor(){ |
sangrit | 1:d96590319bcf | 42 | sendCommand('c'); |
sangrit | 1:d96590319bcf | 43 | return receiveData(); |
ruke1ire | 0:d93ce07131fc | 44 | } |
ruke1ire | 0:d93ce07131fc | 45 | |
ruke1ire | 0:d93ce07131fc | 46 | int Arduino::getOrientation(){ |
sangrit | 1:d96590319bcf | 47 | sendCommand('a'); |
sangrit | 1:d96590319bcf | 48 | return receiveData(); |
ruke1ire | 0:d93ce07131fc | 49 | } |
ruke1ire | 0:d93ce07131fc | 50 |