-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathElegooBluetoothReceiver.h
80 lines (62 loc) · 1.8 KB
/
ElegooBluetoothReceiver.h
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
#ifndef __ELEGOO_BLUETOOTH_RECEIVER_H__
#define __ELEGOO_BLUETOOTH_RECEIVER_H__
#include <Arduino.h>
#include "ElegooBluetoothConfigInterface.h"
#include "ElegooCarConfig.h"
#include "ElegooCommand.h"
#include "ElegooReceiver.h"
class ElegooBluetoothReceiver: public ElegooReceiver
{
private:
ElegooCarConfig::BluetoothReceiverConfig & config;
ElegooBluetoothConfigInterface ** bluetoothConfigs;
int numBluetoothConfigs = 0;
public:
ElegooBluetoothReceiver(ElegooCarConfig::BluetoothReceiverConfig & pConfig) :
config(pConfig)
{
int size = config.MAX_NUM_RECEIVERS;
bluetoothConfigs = new ElegooBluetoothConfigInterface*[size];
}
void setup()
{
}
void registerBluetoothConfig(ElegooBluetoothConfigInterface * bluetoothConfig)
{
bluetoothConfigs[numBluetoothConfigs] = bluetoothConfig;
numBluetoothConfigs++;
}
// May also return UNKNOWN_CMD or NO_COMMAND
ElegooCommand readCommand()
{
int intInput = Serial.read();
if (intInput == -1)
{
return ElegooCommand::NO_COMMAND;
}
char charInput = (char) intInput;
ElegooCommand cmd = checkBluetoothProviders(charInput);
return cmd;
}
private:
// May return UNKNOWN_CMD, will never return NO_COMMAND
ElegooCommand checkBluetoothProviders(char charInput)
{
for (int i = 0; i < numBluetoothConfigs; i++)
{
ElegooBluetoothConfigInterface * bluetoothConfig = bluetoothConfigs[i];
if (bluetoothConfig->isSupportedCharacter(charInput))
{
Serial.print("Bluetooth result: ");
Serial.println(charInput);
}
ElegooCommand cmd = bluetoothConfig->checkCommand(charInput);
if (cmd != ElegooCommand::UNK_COMMAND)
{
return cmd; // return as soon as we have found something known
}
}
return ElegooCommand::UNK_COMMAND;
}
};
#endif