team2 / Arduino
Revision:
0:d93ce07131fc
Child:
1:d96590319bcf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Arduino.cpp	Sat Apr 07 04:11:19 2018 +0000
@@ -0,0 +1,50 @@
+#include "mbed.h"
+#include "Arduino.h"
+
+
+Arduino::Arduino(PinName tx, PinName rx) : _tx(tx), _rx(rx){
+    _arduino.baud(9600);
+}
+void Arduino::_sendCommand(char com){
+    _arduino.clc();
+    _arduino.printf("&c",  com);
+}
+
+int Arduino::_recieveData(){
+    char value[100];
+    int index=0;
+    char ch;
+    do
+    { 
+    if (_arduino.readable())      // if there is an character to read from the device
+    {
+        ch = _arduino.getc();   // read it
+        if (index<99)               // just to avoid buffer overflow
+            value[index++]=ch;  // put it into the value array and increment the index
+        }
+    } while (ch!='\n');    // loop until the '\n' character
+ 
+    value[index]='\x0';  // add un 0 to end the c string
+    return atoi(value);
+}
+
+int Arduino::getLdrY(){
+    _sendCommand('y');
+    return _recieveData();
+}
+
+int Arduino::getLdrX(){
+    _sendCommand('x');
+    return _recieveData();
+}
+
+int Arduino::getColor(){
+    _sendCommand('c');
+    return _recieveData();
+}
+
+int Arduino::getOrientation(){
+    _sendCommand('a');
+    return _recieveData();
+}
+