-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm5core2-visiting-card.ino
163 lines (130 loc) · 2.94 KB
/
m5core2-visiting-card.ino
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 "config.h"
#include <Arduino.h>
#ifdef BOARD_M5CORE
#include <M5Stack.h>
#endif
#ifdef BOARD_M5CORE2
#include <M5Core2.h>
#endif
#include <Ticker.h>
#define LGFX_AUTODETECT
#include <LovyanGFX.hpp>
#include <LGFX_AUTODETECT.hpp>
#include <Adafruit_NeoPixel.h>
#ifdef ENABLE_SHT31
#include <Adafruit_SHT31.h>
#endif
#include <ArduinoJson.h>
#include "settings.h"
#include "state-manager.h"
namespace {
LGFX lcd;
Ticker ticker;
StaticJsonDocument<4096> json_document;
Settings* pSettings = nullptr;
ImageState image_state;
QRState qr_state;
StateManager stateManager(image_state, qr_state);
constexpr uint8_t brightness_min = 63;
constexpr uint8_t brightness_step = 32;
constexpr uint8_t brightness_max = 255;
constexpr size_t neopixel_num = 10;
#ifdef BOARD_M5CORE
constexpr size_t neopixel_pin = 15;
#ifdef ENABLE_SHT31
Adafruit_SHT31 sht31(&Wire);
#endif
#endif
#ifdef BOARD_M5CORE2
constexpr size_t neopixel_pin = 2;
#ifdef ENABLE_SHT31
Adafruit_SHT31 sht31(&Wire1);
#endif
#endif
Adafruit_NeoPixel neopixel = Adafruit_NeoPixel(neopixel_num, neopixel_pin);
uint8_t display_brightness = 127;
}
void vibrateOn()
{
#ifdef BOARD_M5CORE2
M5.Axp.SetLDOEnable(3, true);
#endif
}
void vibrateOff()
{
#ifdef BOARD_M5CORE2
M5.Axp.SetLDOEnable(3, false);
#endif
}
void setLed(bool is_on)
{
#ifdef BOARD_M5CORE2
M5.Axp.SetLed(is_on);
#endif
}
void setup() {
M5.begin();
lcd.init();
lcd.setColorDepth(24);
File json_file = SD.open("/settings.json");
DeserializationError error = deserializeJson(json_document, json_file);
if (error != DeserializationError::Ok)
{
Serial.println("JSON Deserialization Error");
return;
}
// 設定/制御
pSettings = Settings::fromJson(json_document);
#ifdef ENABLE_SHT31
pSettings->begin(lcd, neopixel, sht31);
#else
pSettings->begin(lcd, neopixel);
#endif
// 状態管理
stateManager.begin(pSettings);
// 輝度default
lcd.setBrightness(display_brightness);
ticker.attach_ms(100, onTimerTicked);
}
void loop()
{
}
void onTimerTicked()
{
M5.update();
#ifdef BOARD_M5CORE2
// タッチ中LED消灯
if (M5.Touch.ispressed())
{
setLed(false);
vibrateOn();
}
else
{
setLed(true);
vibrateOff();
}
#endif
if (M5.BtnA.wasPressed())
{
// NeoPixel点灯/消灯
pSettings->toggleLED();
}
if (M5.BtnB.wasPressed())
{
// 輝度調整
display_brightness += brightness_step;
if (display_brightness >= brightness_max)
{
display_brightness = brightness_min;
}
lcd.setBrightness(display_brightness);
}
if (M5.BtnC.wasPressed())
{
// QRコード切替
stateManager.toggleState();
}
pSettings->update();
stateManager.update();
}