-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.cpp
91 lines (68 loc) · 1.45 KB
/
receiver.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: t -*-
#include "receiver.h"
bool Receiver :: init()
{
pinMode(yawPin, INPUT);
pinMode(pitchPin, INPUT);
pinMode(throttlePin, INPUT);
pinMode(rollPin, INPUT);
pinMode(rSwitchPin, INPUT);
pinMode(lSwitchPin, INPUT);
return true;
}
bool Receiver :: armMotors()
{
return true;
}
unsigned long Receiver :: readYaw()
{
// yaw is reading in reverse for some reason..
return readPulse(yawPin, HIGH, 25000);
}
unsigned long Receiver :: readPitch()
{
return readPulse(pitchPin, HIGH, 25000);
}
unsigned long Receiver :: readThrottle()
{
return readPulse(throttlePin, HIGH, 25000);
}
unsigned long Receiver :: readRoll()
{
return readPulse(rollPin, HIGH, 25000);
}
unsigned long Receiver :: readRSwitch()
{
return readPulse(rSwitchPin, HIGH, 5000);
}
unsigned long Receiver :: readLSwitch()
{
return readPulse(lSwitchPin, HIGH, 5000);
}
unsigned long Receiver :: readPulse(int pin, int signal, unsigned long timeout)
{
unsigned long current, killTime, ptime;
current = micros();
killTime = current + timeout;
while(digitalRead(pin) == signal)
{
delayMicroseconds(4);
current = micros();
if(current >= killTime){
return 0UL;
}
}
while(digitalRead(pin) != signal)
{
delayMicroseconds(4);
current = micros();
if(current >= killTime){
return 0UL;
}
}
ptime = micros();
while(digitalRead(pin) == signal){
delayMicroseconds(4);
}
return micros() - ptime;
}