-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
357 lines (333 loc) · 9.76 KB
/
Copy pathmain.c
File metadata and controls
357 lines (333 loc) · 9.76 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
#include "Strust4EmbeddedConf.h"
#include "ch.h"
#include "hal.h"
#include "dbgtrace.h"
#include "Strust4Embedded.h"
#include "ssd1306.h"
#include "ButtonLEDs.h"
#include "PotReader.h"
#include "MQTTClient.h"
#include "audio.h"
#include "AudioPlayerThd.h"
#include "PPMFrameDecoder.h"
#include "EByteLora.h"
#include "gui.h"
#include "uGFXThread.h"
#include "IRReceiver.h"
#include "ccportab.h"
#ifdef USE_USBCFG
#include "usbcfg.h"
static void initUSBCFG(void);
#endif
AudioPlayerDriverITF_Typedef *pAudioPlayerDriverITF;
thread_t *mainThd;
static void initDrivers(void);
#if HAL_USE_SERIAL != 0
/* VCP Serial configuration. */
static const SerialConfig myserialcfg = {
115200,
0,
USART_CR2_STOP1_BITS,
0
};
#endif
#if DEBUG_TRACE_PRINT != 0
BaseSequentialStream *GlobalDebugChannel = (BaseSequentialStream *)&PORTAB_SD;
#endif
#define LCD_COLOR_DARKYELLOW ((uint32_t) 0xFF808000)
#define LCD_COLOR_RED ((uint32_t) 0xFFFF0000)
#define LCD_COLOR_YELLOW ((uint32_t) 0xFFFFFF00)
#define LCD_COLOR_WHITE ((uint32_t) 0xFFFFFFFF)
void initMain(void);
#ifndef __cplusplus
void initMain(void){
halInit();
chSysInit();
}
#endif
int main(void) {
initMain();
#if HAL_USE_SERIAL != 0
#ifdef USE_USBCFG
initUSBCFG();
#else
sdStart(&PORTAB_SD, &myserialcfg);
#endif
#endif
#if S4E_USE_ETHERNET != 0
initMQTTClient();
#endif
initDrivers();
initActonEventThd();
initButtonsLEDs();
#if S4E_USE_WIFI_MODULE_THD != 0
initWifiCommunicationThd();
#endif
#if defined(LINE_LED_RED)
initBlinkerThd(pRedLedPAL);
#else
initBlinkerThd(pGreenLedPAL);
#endif
initAudioPlayerThd();
#if USE_LCD_TFT
inituGFXThd();
#endif
#if S4E_USE_EBYTE_LORA != 0
initEByteLoraThread();
#endif
#if S4E_USE_PPM_FRAME_DECODER != 0
initPPMFrameDecoder();
#endif
#if S4E_USE_IR_RECEIVER != 0
initIRReeceiver();
#endif
while (true) {
chThdSleepMilliseconds(1500);
#if S4E_USE_MQTT != 0
if ( !isDefaultMQTTBrokerConnected() )
reconnectDefaultMQTTBroker();
#endif
}
}
static NameValuePairStaticTypeDef readFilesFromFolder= {.key=READ_FILES_FROM_FOLDER, .value="/music"};
static void initDrivers(void){
pAudioPlayerDriverITF = getAudioPlayerDriver();
mainThd = chThdGetSelfX();
initStruts4EmbeddedFramework();
putSysProperty(&readFilesFromFolder);
#if S4E_USE_POT != 0
initPotReader();
#endif
#if S4E_USE_SSD1306_LCD != 0
ssd130InitAndConfig("MP3Player w/ STM32F7");
#endif
#if HAL_USE_RTC != 0
RTCInit();
#endif
return;
}
static bool pauseMsgSent = false;
void updateUI(AudioPlayerDriverITF_Typedef *pAudioPlayerDriver){
int forMinutes = pAudioPlayerDriver->pAudioFileInfo->secondsRemaining / 60;
int remainingSec = pAudioPlayerDriver->pAudioFileInfo->secondsRemaining % 60;
char payload[120] = {0};
if ( pAudioPlayerDriver->pAudioFileInfo != NULL &&
(!IS_PAUSING(pAudioPlayerDriver) || (IS_PAUSING(pAudioPlayerDriver) && !pauseMsgSent)) ){
updateLCD();
#if S4E_USE_WIFI_MODULE_THD != 0
chsnprintf(payload,sizeof(payload),"%s{\"track\":\"%s\",\"status\":\"%s\",\"vol\":%d,\"seconds_remaining\":%d,\"timeRemaining\":\"%d:%d\"}%s",
PAYLOAD_PREFIX,
pAudioPlayerDriver->pAudioFileInfo->filename,
getAPStateName(pAudioPlayerDriver->state),
getCurrentVolume(),
pAudioPlayerDriver->pAudioFileInfo->secondsRemaining,
forMinutes,
remainingSec,
PAYLOAD_SUFFIX);
sendWifiModuleMsg(payload);
#endif
#if S4E_USE_MQTT != 0
pauseMsgSent = IS_PAUSING(pAudioPlayerDriver)? true: false;
chsnprintf(payload,sizeof(payload),"{\"track\":\"%s\",\"status\":\"%s\",\"vol\":%d,\"seconds_remaining\":%d,\"timeRemaining\":\"%d:%d\"}",
pAudioPlayerDriver->pAudioFileInfo->filename,
getAPStateName(pAudioPlayerDriver->state),
getCurrentVolume(),
pAudioPlayerDriver->pAudioFileInfo->secondsRemaining,
forMinutes,
remainingSec);
sendToDefaultMQTTQueue(payload);
#endif
}
return;
}
//This is a weak/virtual method that overrides the default in the BlinkerThd.c file, see for details
void periodicSysTrigger(uint32_t i){
#if S4E_USE_POT != 0
checkOnPotVolumeChange();
#endif
if ( i % 2 != 0 )
return;
if ( i> 0 && i % 2 == 0 )
updateUI(pAudioPlayerDriverITF);
if (IS_PLAYING(pAudioPlayerDriverITF))
--pAudioPlayerDriverITF->pAudioFileInfo->secondsRemaining;
return;
}
#if HAL_USE_RTC != 0
void onRTCSleep(void){
dbgprintf("Going sleep in 10ms...:goingToSleepCnt:%d\r\n",getGoingToSleepCnt());
audioPlayerPrepareGoingToSleep();
#if USE_LCD_TFT != 0
turnOffLCD();
#endif
chThdSleepMilliseconds(10);
}
#endif
#ifdef USE_USBCFG
static void initUSBCFG(void){
/* Configuring PG14 as AF8 assigning it to USART6_TX. */
// palSetPadMode(GPIOG, 14, PAL_MODE_ALTERNATE(8) | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_PUSHPULL);
// /* Configuring PG9 as AF8 assigning it to USART6_RX. */
// palSetPadMode(GPIOG, 9, PAL_MODE_ALTERNATE(8) | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_PUSHPULL);
/*
* Initializes a serial-over-USB CDC driver.
*/
sduObjectInit(&PORTAB_SDU);
sduStart(&PORTAB_SDU, &serusbcfg);
chThdSleepMilliseconds(300);
usbDisconnectBus(serusbcfg.usbp);
chThdSleepMilliseconds(1500);
usbStart(serusbcfg.usbp, &usbcfg);
usbConnectBus(serusbcfg.usbp);
sdStart(&PORTAB_SD_VCP, &myserialcfg);
}
#endif
//overriding the default handling of IR Events
#if S4E_USE_IR_RECEIVER != 0
static ActionEvent_Typedef *pSetVolumeUpAE = NULL;
static ActionEvent_Typedef *pSetVolumeDownAE = NULL;
static ActionEvent_Typedef *pTogglePauseAE = NULL;
static ActionEvent_Typedef *pToggleMuteAE = NULL;
static ActionEvent_Typedef *pToggleSleepAE = NULL;
static ActionEvent_Typedef *pNextTrackAE = NULL;
static ActionEvent_Typedef *pPrevTrackAE = NULL;
static ActionEvent_Typedef *pRepositionToTrackAE = NULL;
static bool irInit = false;
void handlIREvent(IR_CMD_t command, bool repeatFlag){
if ( !irInit){
pSetVolumeUpAE = getActionEvent(VOLUME_UP_AE_NAME);
pSetVolumeDownAE = getActionEvent(VOLUME_DOWN_AE_NAME);
pTogglePauseAE = getActionEvent(TOGGLE_PAUSE_AE_NAME);
pToggleMuteAE = getActionEvent(TOGGLE_MUTE_AE_NAME);
pToggleSleepAE = getActionEvent(GO_TO_SLEEP_AE_NAME);
pNextTrackAE = getActionEvent(NEXT_TRACK_AE_NAME);
pPrevTrackAE = getActionEvent(PREV_TRACK_AE_NAME);
irInit = true;
}
switch(command){
case BTN_POWER:
dbgprintf("PWR");
if(repeatFlag)
dbgprintf(" RPT");
else
triggerActionEvent(pToggleSleepAE->name, NULL, 0, SOURCE_EVENT_IR);
break;
case BTN_MODE:
dbgprintf("MODE");
if(repeatFlag)
dbgprintf(" RPT");
break;
case BTN_MUTE:
dbgprintf("MUTE");
if(repeatFlag)
dbgprintf(" RPT");
else
triggerActionEvent(pToggleMuteAE->name, NULL, 0, SOURCE_EVENT_IR);
break;
case BTN_PREV:
dbgprintf("PREV");
if(repeatFlag)
dbgprintf(" RPT");
else
triggerActionEvent(pPrevTrackAE->name, NULL, 0, SOURCE_EVENT_IR);
break;
case BTN_NEXT:
dbgprintf("NEXT");
if(repeatFlag)
dbgprintf(" RPT");
else
triggerActionEvent(pNextTrackAE->name, NULL, 0, SOURCE_EVENT_IR);
break;
case BTN_PLAY_PAUSE:
dbgprintf("PLAY/PAUSE");
if(repeatFlag)
dbgprintf(" RPT");
else
triggerActionEvent(pTogglePauseAE->name, NULL, 0, SOURCE_EVENT_IR);
break;
case BTN_VOL_DOWN:
dbgprintf("VOL-");
if(repeatFlag)
dbgprintf(" RPT");
else
triggerActionEvent(pSetVolumeDownAE->name, NULL, 0, SOURCE_EVENT_IR);
break;
case BTN_VOL_UP:
dbgprintf("VOL+");
if(repeatFlag)
dbgprintf(" RPT");
else
triggerActionEvent(pSetVolumeUpAE->name, NULL, 0, SOURCE_EVENT_IR);
break;
case BTN_EQ:
dbgprintf("EQ");
if(repeatFlag)
dbgprintf(" RPT");
break;
case BTN_0:
dbgprintf("0");
if(repeatFlag)
dbgprintf(" RPT");
break;
case BTN_LOOP:
dbgprintf("100+");
if(repeatFlag)
dbgprintf(" RPT");
break;
case BTN_USD:
dbgprintf("200+");
if(repeatFlag)
dbgprintf(" RPT");
break;
case BTN_1:
dbgprintf("1");
if(repeatFlag)
dbgprintf(" RPT");
break;
case BTN_2:
dbgprintf("2");
if(repeatFlag)
dbgprintf(" RPT");
break;
case BTN_3:
dbgprintf("3");
if(repeatFlag)
dbgprintf(" RPT");
break;
case BTN_4:
dbgprintf("4");
if(repeatFlag)
dbgprintf(" RPT");
break;
case BTN_5:
dbgprintf("5");
if(repeatFlag)
dbgprintf(" RPT");
break;
case BTN_6:
dbgprintf("6");
if(repeatFlag)
dbgprintf(" RPT");
break;
case BTN_7:
dbgprintf("7");
if(repeatFlag)
dbgprintf(" RPT");
break;
case BTN_8:
dbgprintf("8");
if(repeatFlag)
dbgprintf(" RPT");
break;
case BTN_9:
dbgprintf("9");
if(repeatFlag)
dbgprintf(" RPT");
break;
default:
dbgprintf("Unknown");
break;
}
dbgprintf("\n\r");
}
#endif