-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathinterface.cpp
More file actions
406 lines (350 loc) · 11.8 KB
/
Copy pathinterface.cpp
File metadata and controls
406 lines (350 loc) · 11.8 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "core/powerSave.h"
#include "core/utils.h"
#include <Wire.h>
#include <bq27220.h>
#include <globals.h>
#include <interface.h>
// Rotary encoder
#include <RotaryEncoder.h>
extern RotaryEncoder *encoder;
RotaryEncoder *encoder = nullptr;
IRAM_ATTR void checkPosition() { encoder->tick(); }
// GPIO expander
#include <ExtensionIOXL9555.hpp>
ExtensionIOXL9555 io;
// Charger chip
#define XPOWERS_CHIP_BQ25896
#include <XPowersLib.h>
#include <esp32-hal-dac.h>
XPowersPPM PPM;
// Battery libs
#ifdef USE_BQ27220_VIA_I2C
#define BATTERY_DESIGN_CAPACITY 1500
#include <bq27220.h>
BQ27220 bq;
#endif
#include "core/i2c_finder.h"
#include "modules/rf/rf_utils.h"
// Keyboard
#include <Adafruit_TCA8418.h>
Adafruit_TCA8418 *keyboard;
// Haptic
#include "SensorDRV2605.hpp"
SensorDRV2605 drv;
void hapticTest(uint8_t effect);
uint8_t effect = 1;
// Audio
#include "AudioBoard.h"
DriverPins PinsAudioBoardES8311;
AudioBoard board(AudioDriverES8311, PinsAudioBoardES8311);
// Keyboard
bool fn_key_pressed = false;
bool shift_key_pressed = false;
bool caps_lock = false;
#define KB_ROWS 4
#define KB_COLS 10
struct KeyValue_t {
const char value_first;
const char value_second;
const char value_third;
};
const KeyValue_t _key_value_map[KB_ROWS][KB_COLS] = {
{{'q', 'Q', '1'},
{'w', 'W', '2'},
{'e', 'E', '3'},
{'r', 'R', '4'},
{'t', 'T', '5'},
{'y', 'Y', '6'},
{'u', 'U', '7'},
{'i', 'I', '8'},
{'o', 'O', '9'},
{'p', 'P', '0'}},
{{'a', 'A', '*'},
{'s', 'S', '/'},
{'d', 'D', '+'},
{'f', 'F', '-'},
{'g', 'G', '='},
{'h', 'H', ':'},
{'j', 'J', '\''},
{'k', 'K', '"'},
{'l', 'L', '@'},
{KEY_ENTER, KEY_ENTER, '&'}},
{{KEY_FN, KEY_FN, KEY_FN},
{'z', 'Z', '_'},
{'x', 'X', '$'},
{'c', 'C', ';'},
{'v', 'V', '?'},
{'b', 'B', '!'},
{'n', 'N', ','},
{'m', 'M', '.'},
{KEY_SHIFT, KEY_SHIFT, CAPS_LOCK},
{KEY_BACKSPACE, KEY_BACKSPACE, '#'}},
{{' ', ' ', ' '}}
};
char getKeyChar(uint8_t k) {
char keyVal;
if (fn_key_pressed) {
keyVal = _key_value_map[k / 10][k % 10].value_third;
} else if (shift_key_pressed ^ caps_lock) {
keyVal = _key_value_map[k / 10][k % 10].value_second;
} else {
keyVal = _key_value_map[k / 10][k % 10].value_first;
}
return keyVal;
}
int handleSpecialKeys(uint8_t k, bool pressed) {
char keyVal = _key_value_map[k / 10][k % 10].value_first;
switch (keyVal) {
case KEY_FN: fn_key_pressed = !fn_key_pressed; return 1;
case KEY_SHIFT: {
shift_key_pressed = pressed;
if (fn_key_pressed && shift_key_pressed) { caps_lock = !caps_lock; }
return 1;
}
default: break;
}
return 0;
}
/***************************************************************************************
** Function name: _setup_gpio()
** Description: initial setup for the device
***************************************************************************************/
void _setup_gpio() {
pinMode(SEL_BTN, INPUT);
pinMode(BK_BTN, INPUT);
pinMode(TFT_CS, OUTPUT);
digitalWrite(TFT_CS, HIGH);
pinMode(SDCARD_CS, OUTPUT);
digitalWrite(SDCARD_CS, HIGH);
pinMode(NFC_CS, OUTPUT);
digitalWrite(NFC_CS, HIGH);
pinMode(LORA_CS, OUTPUT);
digitalWrite(LORA_CS, HIGH);
pinMode(LORA_RST, OUTPUT);
digitalWrite(LORA_RST, HIGH);
// Power management
bool pmu_ret = false;
pmu_ret = PPM.init(Wire, GROVE_SDA, GROVE_SCL, BQ25896_SLAVE_ADDRESS);
if (pmu_ret) {
// https://github.com/Xinyuan-LilyGO/LilyGoLib/blob/a64fc6ca94757baa5401ad71b39fb7f92cd1a7e9/src/LilyGo_LoRa_Pager.cpp#L442-L452
PPM.resetDefault();
PPM.setChargeTargetVoltage(4288);
PPM.setChargerConstantCurr(704);
PPM.enableMeasure(PowersBQ25896::CONTINUOUS);
}
// Battery gauge
if (bq.getDesignCap() != BATTERY_DESIGN_CAPACITY) { bq.setDesignCap(BATTERY_DESIGN_CAPACITY); }
// IO Expander
// TODO: Needs updating to use the same interface as the other IO Expanders (io_expander ioExpander)
// if (ioExpander.init(IO_EXPANDER_ADDRESS, &Wire)) {
// const uint8_t expands[] = {
// EXPANDS_KB_RST,
// EXPANDS_KB_EN,
// EXPANDS_SD_EN,
// EXPANDS_DRV_EN,
// EXPANDS_AMP_EN, // Audio
// };
// for (auto pin : expands) {
// ioExpander.pinMode(pin, OUTPUT);
// ioExpander.digitalWrite(pin, HIGH);
// delay(1);
// }
// ioExpander.pinMode(EXPANDS_SD_PULLEN, INPUT);
// ioExpander.digitalWrite(EXPANDS_DRV_EN, LOW);
// } else {
// Serial.println("Initializing expander failed");
// }
if (io.begin(Wire, IO_EXPANDER_ADDRESS)) {
const uint8_t expands[] = {
EXPANDS_KB_RST,
EXPANDS_KB_EN,
EXPANDS_SD_EN,
EXPANDS_DRV_EN,
EXPANDS_AMP_EN, // Audio
EXPANDS_LORA_EN,
EXPANDS_GPS_EN,
EXPANDS_GPS_RST,
EXPANDS_NFC_EN,
};
for (auto pin : expands) {
io.pinMode(pin, OUTPUT);
io.digitalWrite(pin, HIGH);
delay(1);
}
io.pinMode(EXPANDS_SD_PULLEN, INPUT);
} else {
Serial.println("Initializing expander failed");
}
// Initialise keyboard
keyboard = new Adafruit_TCA8418();
if (!keyboard->begin(KB_I2C_ADDRESS, &Wire)) {
Serial.println("Failed to find Keyboard");
} else {
Serial.println("Initializing Keyboard succeeded");
}
keyboard->matrix(KB_ROWS, KB_COLS);
keyboard->flush();
// Start with default IR, RF, GPS and RFID Configs, replace old
bruceConfigPins.rfModule = CC1101_SPI_MODULE;
bruceConfigPins.rfidModule = ST25R3916_SPI_MODULE;
bruceConfigPins.irRx = 1;
bruceConfigPins.gpsBaudrate = 38400;
// Encoder
pinMode(ENCODER_KEY, INPUT);
encoder = new RotaryEncoder(ENCODER_INA, ENCODER_INB, RotaryEncoder::LatchMode::FOUR3);
attachInterrupt(digitalPinToInterrupt(ENCODER_INA), checkPosition, CHANGE);
attachInterrupt(digitalPinToInterrupt(ENCODER_INB), checkPosition, CHANGE);
// Haptic driver
if (!drv.begin(Wire, SDA, SCL)) {
Serial.println("Failed to find DRV2605.");
} else {
Serial.println("Init DRV2605 Sensor success!");
drv.selectLibrary(1);
drv.setMode(SensorDRV2605::MODE_INTTRIG);
drv.useERM();
// Startup buzz
drv.setWaveform(0, 70);
drv.setWaveform(1, 0);
drv.run();
}
// Audio
// https://github.com/meshtastic/firmware/blob/ee6449746bf8c5358b8adbde05b96e2b2d04f450/src/platform/extra_variants/t_lora_pager/variant.cpp
// AudioDriverLogger.begin(Serial, AudioDriverLogLevel::Debug);
// I2C: function, scl, sda
PinsAudioBoardES8311.addI2C(PinFunction::CODEC, Wire);
// I2S: function, mclk, bck, ws, data_out, data_in
PinsAudioBoardES8311.addI2S(
PinFunction::CODEC, AUDIO_I2S_MCLK, AUDIO_I2S_SCK, AUDIO_I2S_WS, AUDIO_I2S_SDOUT, AUDIO_I2S_SDIN
);
// configure codec
CodecConfig cfg;
cfg.input_device = ADC_INPUT_LINE1;
cfg.output_device = DAC_OUTPUT_ALL;
cfg.i2s.bits = BIT_LENGTH_16BITS;
cfg.i2s.rate = RATE_44K;
board.begin(cfg);
}
/***************************************************************************************
** Function name: getBattery()
** Description: Delivers the battery value from 1-100
***************************************************************************************/
int getBattery() {
static float smoothed = -1;
constexpr float alpha = 0.2f;
int pct = bq.getChargePcnt();
if (pct >= 0 && pct <= 100) {
if (smoothed < 0) {
smoothed = pct;
} else {
smoothed = alpha * pct + (1 - alpha) * smoothed;
}
}
return static_cast<int>(std::ceil(smoothed));
}
/*********************************************************************
** Function: setBrightness
** set brightness value
**********************************************************************/
void _setBrightness(uint8_t brightval) {
if (brightval == 0) {
analogWrite(TFT_BL, brightval);
analogWrite(KEYBOARD_BL, brightval);
} else {
int bl = MINBRIGHT + round(((255 - MINBRIGHT) * brightval / 100));
analogWrite(TFT_BL, bl);
analogWrite(KEYBOARD_BL, bl);
}
}
/*********************************************************************
** Function: InputHandler
** Handles the variables PrevPress, NextPress, SelPress, AnyKeyPress and EscPress
**********************************************************************/
void InputHandler(void) {
static unsigned long tm = millis();
static int posDifference = 0;
static int lastPos = 0;
bool sel = !BTN_ACT;
bool esc = !BTN_ACT;
uint8_t keyValue = 0;
uint8_t keyVal = '\0';
if (millis() - tm < 500) return;
int newPos = encoder->getPosition();
if (newPos != lastPos) {
posDifference += (newPos - lastPos);
lastPos = newPos;
}
sel = digitalRead(SEL_BTN);
esc = digitalRead(BK_BTN);
if (keyboard->available() > 0) {
int keyValue = keyboard->getEvent();
bool pressed = keyValue & 0x80;
keyValue &= 0x7F;
keyValue--;
if (keyValue / 10 < 4) {
if (handleSpecialKeys(keyValue, pressed) > 0) goto END;
keyVal = getKeyChar(keyValue);
}
if (pressed && !wakeUpScreen() && keyVal != '\0') {
KeyStroke.Clear();
KeyStroke.hid_keys.push_back(keyVal);
if (keyVal == KEY_BACKSPACE) {
KeyStroke.del = true;
EscPress = true;
}
if (keyVal == KEY_ENTER) {
KeyStroke.enter = true;
SelPress = true;
}
if (keyVal == KEY_FN) KeyStroke.fn = true;
KeyStroke.word.push_back(keyVal);
KeyStroke.pressed = true;
// Haptic feedback
drv.setWaveform(0, 81);
drv.setWaveform(1, 0);
drv.run();
}
} else KeyStroke.Clear();
if (posDifference != 0 || sel == BTN_ACT || esc == BTN_ACT || KeyStroke.enter) {
if (!wakeUpScreen()) {
AnyKeyPress = true;
// Haptic feedback
drv.setWaveform(0, 1);
drv.setWaveform(1, 0);
drv.run();
if (posDifference < 0) {
PrevPress = true;
posDifference++;
}
if (posDifference > 0) {
NextPress = true;
posDifference--;
}
if (sel == BTN_ACT) SelPress = true;
if (esc == BTN_ACT) EscPress = true;
} else goto END;
}
END:
if (sel == BTN_ACT || esc == BTN_ACT) tm = millis();
}
void powerOff() { PPM.shutdown(); }
/***************************************************************************************
** Function name: isCharging()
** Description: Determines if the device is charging
***************************************************************************************/
#ifdef USE_BQ27220_VIA_I2C
bool isCharging() { return bq.getIsCharging(); }
#else
bool isCharging() { return false; }
#endif
/*********************************************************************
** Function: _setup_codec_speaker
** location: modules/others/audio.cpp
** Handles audio CODEC to enable/disable speaker
**********************************************************************/
void _setup_codec_speaker(bool enable) {}
/*********************************************************************
** Function: _setup_codec_mic
** location: modules/others/mic.cpp
** Handles audio CODEC to enable/disable microphone
**********************************************************************/
void _setup_codec_mic(bool enable) {}