-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBluetooth.cpp
More file actions
150 lines (130 loc) · 4.91 KB
/
Copy pathBluetooth.cpp
File metadata and controls
150 lines (130 loc) · 4.91 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
#include "Config.h"
#include "Bluetooth.h"
#include "CommandProcessor.h"
using LogBluetooth = Tiny::TILogTarget<TinyCon::BluetoothLogLevel>;
#if !NO_BLE
std::function<void(uint16_t, BLECharacteristic*, uint8_t*, uint16_t)> TinyCon::BluetoothController::HapticWriteCallback =
[](uint16_t, BLECharacteristic*, uint8_t*, uint16_t) {};
void TinyCon::BluetoothController::HapticWrite(uint16_t connection, BLECharacteristic *chr, uint8_t *data, uint16_t length)
{ HapticWriteCallback(connection, chr, data, length); }
void TinyCon::BluetoothController::Init()
{
Bluefruit.begin();
Bluefruit.setTxPower(-40);
Bluefruit.setName(TINYCON_PRODUCT);
Discovery.setManufacturer(TINYCON_VENDOR);
Discovery.setSoftwareRev(TinyConVersion);
Discovery.begin();
GamepadService.begin();
MpuService.begin();
Bluefruit.Advertising.addService(MpuService);
MpuCharacteristic.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY);
MpuCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
MpuCharacteristic.setFixedLen(40);
MpuCharacteristic.begin();
HapticService.begin();
Bluefruit.Advertising.addService(HapticService);
HapticCharacteristic.setProperties(CHR_PROPS_WRITE | CHR_PROPS_WRITE_WO_RESP);
HapticCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
HapticWriteCallback = [this](uint16_t connection, BLECharacteristic* chr, uint8_t* data, uint16_t length)
{
if (length >= 1) Processor.ProcessCommand({data, length});
};
HapticCharacteristic.setWriteCallback(HapticWrite);
HapticCharacteristic.begin();
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_HID_GAMEPAD);
Bluefruit.Advertising.addService(GamepadService);
Bluefruit.Advertising.addName();
Bluefruit.Advertising.setInterval(32, 244);
Bluefruit.Advertising.setFastTimeout(30);
LogBluetooth::Info("Bluetooth initialized", Tiny::TIEndl);
}
void TinyCon::BluetoothController::Update(uint32_t deltaTime)
{
LogBluetooth::Info("Bluetooth: ");
if (!Processor.GetBLEEnabled() || !Active)
{
if (!Processor.GetBLEEnabled()) LogBluetooth::Info("Disabled");
else LogBluetooth::Info("Inactive");
if (Active || Connected)
{
Active = false;
Connected = false;
ForceAdvertise = false;
AdvertisingStarted = false;
Bluefruit.Advertising.stop();
if (Bluefruit.connected())
{
LogBluetooth::Debug(", Disconnect");
Bluefruit.disconnect(ConnectionId);
}
if (Bluefruit.getTxPower() > -40) Bluefruit.setTxPower(-40);
LogBluetooth::Debug(", Stop");
}
}
else if (ForceAdvertise || AdvertisingTimeout > 0)
{
if (AdvertisingTimeout <= 0 || !AdvertisingStarted)
{
// Previous advertising has timed out, reinitialize
LogBluetooth::Debug("Enable Advertising, ");
Bluefruit.Advertising.start(0);
AdvertisingStarted = true;
}
if (ForceAdvertise)
{
// One-time trigger flag
LogBluetooth::Debug("Force Advertise, ");
ForceAdvertise = false;
AdvertisingTimeout = AdvertisingTime;
if (Bluefruit.connected())
{
LogBluetooth::Debug("Disconnect, ");
Bluefruit.disconnect(ConnectionId);
}
if (Bluefruit.getTxPower() < 4) Bluefruit.setTxPower(4);
LogBluetooth::Debug("Start");
}
else if (Bluefruit.connected())
{
// Connected during advertising, stop advertising
LogBluetooth::Debug("Connected, Stop Advertising");
Bluefruit.Advertising.stop();
AdvertisingStarted = false;
AdvertisingTimeout = 0;
}
else
{
AdvertisingTimeout -= deltaTime;
if (AdvertisingTimeout <= 0)
{
LogBluetooth::Debug("Timeout");
Bluefruit.Advertising.stop();
AdvertisingStarted = false;
Active = false;
}
else LogBluetooth::Info("Advertising");
}
}
else if (Bluefruit.connected())
{
Connected = true;
LogBluetooth::Debug("Controller");
ConnectionId = Bluefruit.connHandle();
auto report = Controller.MakeHidReport();
GamepadService.report(&report);
LogBluetooth::Debug(", MPU");
uint8_t data[GamepadController::MaxMpuControllers * 41];
std::size_t size = Controller.MakeMpuBuffer({data, sizeof(data)});
MpuCharacteristic.notify(data, size);
}
else
{
LogBluetooth::Debug("Disconnected");
ForceAdvertise = true;
}
LogBluetooth::Info(Tiny::TIEndl);
}
#endif