-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote.cpp
More file actions
163 lines (149 loc) · 4.22 KB
/
remote.cpp
File metadata and controls
163 lines (149 loc) · 4.22 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "remote.h"
#include "menu.h"
#include "display.h"
#include "motorfunctions.h"
#include "Lpf2Hub.h"
#include <stdint.h>
static Lpf2Hub Remote[2];
static byte RemotePortLeft[2] = { (byte)PoweredUpRemoteHubPort::LEFT, (byte)PoweredUpRemoteHubPort::LEFT};
static byte RemotePortRight[2] = { (byte)PoweredUpRemoteHubPort::RIGHT, (byte)PoweredUpRemoteHubPort::RIGHT};
// callback function to handle updates of remote buttons
void remoteCallback(void *hub, byte portNumber, DeviceType deviceType, uint8_t *pData)
{
Lpf2Hub *myRemoteHub = (Lpf2Hub *)hub;
// Serial.print("remote callback port: ");
// Serial.println(portNumber, DEC);
if (deviceType == DeviceType::REMOTE_CONTROL_BUTTON)
{
ButtonState buttonState = myRemoteHub->parseRemoteButton(pData);
Serial.print("Buttonstate: ");
Serial.println((byte)buttonState, HEX);
switch (getState())
{
case Init:
break;
case Menu1:
switch (buttonState)
{
case ButtonState::STOP:
controlMotorFuntions (portNumber, Stop, true);
break;
case ButtonState::UP:
controlMotorFuntions (portNumber, Forward, true);
break;
case ButtonState::DOWN:
controlMotorFuntions (portNumber, Backward, true);
break;
case ButtonState::RELEASED:
controlMotorFuntions (portNumber, Backward, false);
break;
default:
break;
}
break;
default:
if (portNumber == 0 && buttonState == ButtonState::UP)
updateSubState (true);
else if (portNumber == 0 && buttonState == ButtonState::DOWN)
updateSubState ();
else if (portNumber == 1 && buttonState == ButtonState::UP)
{
updateMotorFunction (getRemoteButton(), getPortId());
}
else if (portNumber == 1 && buttonState == ButtonState::DOWN)
{
updateMotorFunction (getRemoteButton(), getPortId(), true);
}
display_redrawFunctions();
display_resetAnimationCounter();
display_counterDelay(250);
break;
}
}
}
// Wrapper for second Remote
void remoteCallback2(void *hub, byte portNumber, DeviceType deviceType, uint8_t *pData)
{
remoteCallback(hub, portNumber + 2, deviceType, pData);
}
void configureRemote (uint8_t id)
{
Serial.println("Registering remote:" + String(static_cast<uint8_t>(id)));
switch (id)
{
case 0:
Remote[id].activatePortDevice(RemotePortLeft[id], remoteCallback);
delay(50);
Remote[id].activatePortDevice(RemotePortRight[id], remoteCallback);
delay(50);
Remote[id].setLedColor(WHITE);
break;
case 1:
Remote[id].activatePortDevice(RemotePortLeft[id], remoteCallback2);
delay(50);
Remote[id].activatePortDevice (RemotePortRight[id], remoteCallback2);
delay(50);
Remote[id].setLedColor(YELLOW);
break;
default:
Serial.println("Error, unknown remote:");
break;
}
}
void tryConnectRemotes ()
{
if (Remote[0].isConnecting())
{
if (Remote[0].getHubType() == HubType::POWERED_UP_REMOTE)
{
//This is the right device
if (!Remote[0].connectHub())
{
Serial.println("Unable to connect to hub");
}
else
{
Remote[0].setLedColor(GREEN);
Serial.println("Remote 0 connected.");
}
}
Remote[0].setLedColor(GREEN);
display_set(0, 1, 0, 255, 0);
display_set(0, 2, 0, 255, 0, true);
setStateMax(Menu3);
// enable registration for 2. Remote
Remote[1].init();
// todo: connectedRemote(1);
}
if (Remote[1].isConnecting())
{
if (Remote[1].getHubType() == HubType::POWERED_UP_REMOTE)
{
//This is the right device
if (!Remote[1].connectHub())
{
Serial.println("Unable to connect to hub");
}
else
{
Remote[1].setLedColor(YELLOW);
Serial.println("Remote 1 connected.");
}
}
Remote[1].setLedColor(GREEN);
display_set(0, 3, 0, 255, 0);
display_set(0, 4, 0, 255, 0, true);
setStateMax(Menu5);
}
}
void connectedRemote(uint8_t i)
{
if (!Remote[i].isConnected())
{
Remote[i].init();
}
}
Lpf2Hub *getRemote(uint8_t port)
{
return &Remote[port];
}