-
Notifications
You must be signed in to change notification settings - Fork 15
/
MouseSensor.cpp
68 lines (58 loc) · 1.38 KB
/
MouseSensor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* MouseSensor.cpp - Reads data from MouseSensor
* Author: Dimitris Platis (based on the Smartcar project by Team Pegasus)
* License: GNU GPL v3 http://www.gnu.org/licenses/gpl-3.0.html
*/
#include "AndroidCar.h"
MouseSensor::MouseSensor(){
}
void MouseSensor::attach(HardwareSerial *mouseSerial){
_serial = mouseSerial;
_serial->begin(115200);
_serial->setTimeout(50);
}
boolean MouseSensor::available(){
return _serial->available();
}
String MouseSensor::readLine(){
if (available()){
return readRawSerialLine();
}else{
return "error";
}
}
/* read whatever is in the buffer untill you reach the last available line and return it */
String MouseSensor::readLastLine(){
if (available()){
String input = "";
while (available()) input = readRawSerialLine();
return input;
}else{
return "error";
}
}
int MouseSensor::getX(String mouseInput){
String x = mouseInput.substring(mouseInput.indexOf(':') + 1,mouseInput.indexOf(' '));
int out = x.toInt();
if (out || x.equals("0")){
return out;
}else{
return -9999;
}
}
int MouseSensor::getY(String mouseInput){
mouseInput.trim();
String y = mouseInput.substring(mouseInput.lastIndexOf(':') + 1);
int out = y.toInt();
if (out || y.equals("0")){
return out;
}else{
return -9999;
}
}
String MouseSensor::readRawSerialLine(){
return _serial->readStringUntil('\n');
}
void MouseSensor::clear(){
_serial->print("c");
}