9 bool isRTCEnabled = false;
12 bool isButtonEnabled = false;
14 bool isBuzzerEnabled = false;
18 bool beepState = false;
19 bool beepStateRequested = false;
21 int beepToneRequested;
22 int beepLengthRequested;
28 void beep(int tone, int length, int beep, int silent) {
29 beepStateRequested = true;
30 beepToneRequested = tone;
31 beepLengthRequested = length;
32 if (!silent && beep>length) {
40 unsigned long first_click_millis = 0;
42 void buttonHandler(Button2& btn) {
43 if (!isButtonEnabled) return;
44 if (beepStateRequested) {
45 beepStateRequested = false;
48 clickType click = btn.read();
49 Serial.println(btn.clickToString(click));
52 screenModeRequested++;
53 if (screenModeRequested == mBarrier) { screenModeRequested = mDefault; }
54 if (screenModeRequested == mLast) { screenModeRequested = mDefault; }
57 screenModeRequested--;
58 if (screenModeRequested <= mDefault) { screenModeRequested = mBarrier - 1; }
61 static int click_counter = 0;
62 if (millis() - first_click_millis > 6000) {
63 first_click_millis = millis();
65 beep(400,800,200,100);
66 message(F("Длинное - точка доступа, тройное - сброс"));
68 message(F("Еще раз для сброса"));
69 beep(800,800,200,100);
71 if (click_counter>2) {
73 Serial.println(F("Три тройных нажатия - сбрасываю конфигурацию"));
81 void buttonLongClickHandler(Button2& btn) {
82 if (!isButtonEnabled) return;
83 if (beepStateRequested) {
84 beepStateRequested = false;
87 if (millis() - first_click_millis < 6000) {
95 bool enable_alarm = cfg.getBoolValue(F("enable_alarm"));
96 enable_alarm = !enable_alarm;
97 cfg.setValue(F("enable_alarm"), enable_alarm);
98 Serial.println(F("Alarm = ")); Serial.println(enable_alarm);
100 message(F("Будильник включен"));
101 reportChange(F("enable_alarm"));
103 message(F("Будильник выключен"));
104 reportChange(F("enable_alarm"));
109 void setupHardware() {
110 int pin_sda = cfg.getIntValue(F("pin_sda"));
111 int pin_scl = cfg.getIntValue(F("pin_scl"));
112 if (pin_sda && pin_scl && cfg.getBoolValue(F("enable_rtc"))) {
113 Serial.println(F("Нестандартные пины i2c"));
114 Wire.begin(pin_sda,pin_scl);
116 int i2c_speed = cfg.getIntValue(F("i2c_speed"));
117 if (i2c_speed) Wire.setClock(i2c_speed);
118 if (cfg.getBoolValue(F("enable_rtc"))) {
120 time_t rtc = RTC.now().unixtime();
121 timeval tv = { rtc, 0 };
122 settimeofday(&tv, nullptr);
124 Serial.println(F("Время установлено по встроенным часам"));
126 if (cfg.getBoolValue(F("enable_button"))) {
127 int button_pin = cfg.getIntValue(F("button_pin"));
128 btn.begin(button_pin, INPUT_PULLUP, !cfg.getBoolValue("button_inversed"));
129 btn.setLongClickTime(700);
130 btn.setDoubleClickTime(500);
131 btn.setClickHandler(&buttonHandler);
132 btn.setDoubleClickHandler(&buttonHandler);
133 btn.setTripleClickHandler(&buttonHandler);
134 btn.setLongClickDetectedHandler(&buttonLongClickHandler);
135 isButtonEnabled = true;
137 isButtonEnabled = false;
139 if (cfg.getBoolValue(F("enable_buzzer"))) {
140 isBuzzerEnabled = true;
141 buzzer_pin = cfg.getIntValue(F("buzzer_pin"));
142 pinMode(buzzer_pin,OUTPUT);
143 buzzer_passive = cfg.getBoolValue(F("buzzer_passive"));
145 isBuzzerEnabled = true;
149 void doBeep(int note, int duration) {
150 tone(buzzer_pin, note, duration);
153 void tickHardware() {
154 if (isButtonEnabled) {
157 if (isBuzzerEnabled) {
158 static unsigned long stopBeepMillis = 0;
159 if (stopBeepMillis && millis() >= stopBeepMillis) {
160 beepStateRequested = false;
163 if (beepStateRequested != beepState && beepStateRequested) {
164 stopBeepMillis = millis() + beepLengthRequested;
166 if (buzzer_passive) {
167 static unsigned long nextmillis;
168 if (beepStateRequested != beepState) {
169 beepState = beepStateRequested;
173 if (millis() > nextmillis) {
174 doBeep(beepToneRequested, beepMs);
175 nextmillis = millis() + beepMs + silentMs;
179 static unsigned long nextonmillis;
180 static unsigned long nextoffmillis;
181 if (beepStateRequested != beepState) {
182 beepState = beepStateRequested;
187 static unsigned pinState = 0;
188 if (millis() > nextonmillis) {
189 digitalWrite(buzzer_pin, HIGH);
190 nextoffmillis = nextonmillis + beepMs;
191 nextonmillis = nextonmillis + beepMs + silentMs;
192 } else if (millis() > nextoffmillis) {
193 digitalWrite(buzzer_pin, LOW);
194 nextoffmillis = nextonmillis;
197 digitalWrite(buzzer_pin, LOW);