编程技术分享

分享编程知识,探讨技术创新

0%

简介:灵蛇福韵IOT挂饰以传统文化为根基,以现代科技为羽翼重新定义了家居装饰边界。它是文化传承承载者、生活趣味创造者、科技便捷助力者、绿色生活倡导者,引领我们探索一种既传统又现代、既环保又便捷的生活方式。

好的,我将以一名高级嵌入式软件开发工程师的角度,针对“灵蛇福韵IOT挂饰”这个项目,详细阐述最适合的代码设计架构,并提供具体的C代码实现,同时解释采用的各种技术和方法。
关注微信公众号,提前获取相关推文

项目概述:

灵蛇福韵IOT挂饰是一个集成了多种功能的智能家居装饰品,旨在将传统文化与现代科技相结合,为用户提供趣味、便捷、环保的生活体验。它包含以下主要功能:

  1. 多模灯光: 支持多种灯光模式(例如:呼吸灯、彩虹灯、常亮等),可根据用户喜好和场景切换。
  2. 联网授时: 通过网络自动同步时间,确保时间准确。
  3. 电子爆竹: 模拟爆竹声效,营造节日氛围。
  4. 蓝牙遥控: 用户可以通过手机APP或蓝牙遥控器控制挂饰。
  5. 环境感知: 监测环境温湿度等参数,可用于触发特定动作或数据记录。
  6. 旋律播放: 内置多种音乐或旋律,可用于助眠或营造氛围。
  7. 夜灯伴眠: 提供柔和的灯光,辅助用户入睡。
  8. 助眠音乐: 播放有助于放松和睡眠的音乐。

代码设计架构:

考虑到项目的复杂度和可维护性,我将采用以下分层架构:

  1. 硬件抽象层 (HAL):

    • 目的: 隔离硬件差异,提供统一的硬件操作接口。
    • 包含: GPIO操作、定时器操作、ADC操作、SPI/I2C/UART操作、蓝牙模块驱动、LED驱动、音频输出驱动等。
    • 优点: 提高代码可移植性,方便更换硬件平台。
  2. 设备驱动层 (DDL):

    • 目的: 针对特定设备提供驱动接口,如灯光驱动、传感器驱动、音频播放驱动、蓝牙通信驱动等。
    • 包含: 灯光模式控制、温湿度数据读取、音乐播放控制、蓝牙数据收发、电子爆竹音效控制等。
    • 优点: 将硬件细节进一步封装,方便上层逻辑调用。
  3. 中间件层 (MWL):

    • 目的: 提供通用的服务和功能模块,如网络协议栈、时间同步、数据存储、参数配置等。
    • 包含: Wi-Fi/蓝牙连接管理、NTP时间同步、Flash数据存储、配置文件解析、OTA升级等。
    • 优点: 提高代码复用性,减少重复开发。
  4. 应用层 (APP):

    • 目的: 实现具体的业务逻辑,如灯光模式切换、环境数据展示、音乐播放控制、用户交互等。
    • 包含: 用户界面逻辑、场景模式管理、事件处理、数据上报等。
    • 优点: 负责具体功能实现,代码逻辑清晰。

C代码实现:

由于篇幅限制,我将提供核心模块的C代码实现,并对关键部分进行注释。

1. 硬件抽象层 (HAL):

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
// hal_gpio.h - GPIO 抽象层头文件
#ifndef HAL_GPIO_H
#define HAL_GPIO_H

typedef enum {
GPIO_PIN_0,
GPIO_PIN_1,
// ... 其他 GPIO 引脚
GPIO_PIN_MAX
} gpio_pin_t;

typedef enum {
GPIO_MODE_INPUT,
GPIO_MODE_OUTPUT
} gpio_mode_t;

typedef enum {
GPIO_STATE_LOW,
GPIO_STATE_HIGH
} gpio_state_t;

void hal_gpio_init(gpio_pin_t pin, gpio_mode_t mode);
void hal_gpio_write(gpio_pin_t pin, gpio_state_t state);
gpio_state_t hal_gpio_read(gpio_pin_t pin);

#endif

// hal_gpio.c - GPIO 抽象层实现
#include "hal_gpio.h"
// 假设使用寄存器操作,需要包含对应的芯片头文件
//#include "stm32f10x.h" // Example for STM32

void hal_gpio_init(gpio_pin_t pin, gpio_mode_t mode) {
// 根据 pin 和 mode 配置 GPIO 寄存器
// 示例:
/*
if (pin == GPIO_PIN_0) {
if (mode == GPIO_MODE_OUTPUT) {
GPIOA->CRL &= ~(0xF << (0 * 4)); // 清除对应引脚配置
GPIOA->CRL |= (0x1 << (0 * 4)); // 配置为输出
} else {
GPIOA->CRL &= ~(0xF << (0 * 4)); // 清除对应引脚配置
GPIOA->CRL |= (0x4 << (0 * 4)); // 配置为输入
}
}
*/
}

void hal_gpio_write(gpio_pin_t pin, gpio_state_t state) {
// 根据 pin 和 state 设置 GPIO 输出
// 示例:
/*
if (pin == GPIO_PIN_0) {
if (state == GPIO_STATE_HIGH) {
GPIOA->BSRR = (1 << 0);
} else {
GPIOA->BRR = (1 << 0);
}
}
*/
}

gpio_state_t hal_gpio_read(gpio_pin_t pin) {
// 读取 GPIO 输入状态
// 示例:
/*
if (pin == GPIO_PIN_0) {
if (GPIOA->IDR & (1 << 0)) {
return GPIO_STATE_HIGH;
} else {
return GPIO_STATE_LOW;
}
}
*/
return GPIO_STATE_LOW; // 默认返回值
}

// hal_timer.h - 定时器抽象层头文件
#ifndef HAL_TIMER_H
#define HAL_TIMER_H

typedef enum {
TIMER_1,
TIMER_2,
// ...其他定时器
TIMER_MAX
} timer_t;

typedef void (*timer_callback_t)(void);

void hal_timer_init(timer_t timer, uint32_t period_ms, timer_callback_t callback);
void hal_timer_start(timer_t timer);
void hal_timer_stop(timer_t timer);

#endif

// hal_timer.c - 定时器抽象层实现
#include "hal_timer.h"
//#include "stm32f10x.h" // Example for STM32

// 假设使用通用定时器,以下为示例
void hal_timer_init(timer_t timer, uint32_t period_ms, timer_callback_t callback) {
// 根据定时器编号配置定时器寄存器
// 示例:
/*
if (timer == TIMER_1) {
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // 使能定时器时钟
TIM1->PSC = (SystemCoreClock / 1000000)-1; // 设置分频器,例如 SystemCoreClock为 72M, 预分频设为71 得到1MHz时钟
TIM1->ARR = (period_ms * 1000)-1; // 设置自动重载值,例如 1000ms, 则自动重载值为 1000000-1
TIM1->DIER |= TIM_DIER_UIE; // 使能更新中断
NVIC_EnableIRQ(TIM1_UP_IRQn); // 使能中断
}
*/
}

void hal_timer_start(timer_t timer) {
// 启动定时器
// 示例:
/*
if (timer == TIMER_1) {
TIM1->CR1 |= TIM_CR1_CEN;
}
*/
}

void hal_timer_stop(timer_t timer) {
// 停止定时器
// 示例:
/*
if (timer == TIMER_1) {
TIM1->CR1 &= ~TIM_CR1_CEN;
}
*/
}

// 定时器中断回调函数,示例
/*
void TIM1_UP_IRQHandler(void) {
if (TIM1->SR & TIM_SR_UIF) {
TIM1->SR &= ~TIM_SR_UIF; // 清除中断标志
// 执行用户回调函数
// 假设使用静态全局变量保存回调函数指针
static timer_callback_t callback_func;
if (callback_func != NULL) {
callback_func();
}
}
}
*/

2. 设备驱动层 (DDL):

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
// ddl_led.h - LED 驱动头文件
#ifndef DDL_LED_H
#define DDL_LED_H

typedef enum {
LED_MODE_OFF,
LED_MODE_ON,
LED_MODE_BREATH,
LED_MODE_RAINBOW
} led_mode_t;

void ddl_led_init(void);
void ddl_led_set_mode(led_mode_t mode);
void ddl_led_set_brightness(uint8_t brightness);

#endif

// ddl_led.c - LED 驱动实现
#include "ddl_led.h"
#include "hal_gpio.h"
#include "hal_timer.h"
#include <stdlib.h>

// 假设LED连接到GPIO_PIN_0,可以根据实际情况修改
#define LED_PIN GPIO_PIN_0
#define TIMER_LED TIMER_1

static led_mode_t current_led_mode = LED_MODE_OFF;
static uint8_t current_brightness = 100;
static uint16_t breath_counter = 0;

static void led_breath_timer_callback(void);


void ddl_led_init(void) {
hal_gpio_init(LED_PIN, GPIO_MODE_OUTPUT);
hal_gpio_write(LED_PIN, GPIO_STATE_LOW); // 初始关闭LED
hal_timer_init(TIMER_LED, 10, led_breath_timer_callback);
}

void ddl_led_set_mode(led_mode_t mode) {
current_led_mode = mode;

switch (current_led_mode) {
case LED_MODE_OFF:
hal_gpio_write(LED_PIN, GPIO_STATE_LOW);
hal_timer_stop(TIMER_LED);
break;
case LED_MODE_ON:
hal_gpio_write(LED_PIN, GPIO_STATE_HIGH);
hal_timer_stop(TIMER_LED);
break;
case LED_MODE_BREATH:
hal_timer_start(TIMER_LED);
break;
case LED_MODE_RAINBOW:
// 实现彩虹灯效果,可以使用PWM控制RGB LED
hal_timer_start(TIMER_LED);
break;
default:
break;
}
}

void ddl_led_set_brightness(uint8_t brightness) {
current_brightness = brightness;
// 根据亮度值调整LED的PWM输出
}

void led_breath_timer_callback(void) {
if (current_led_mode == LED_MODE_BREATH)
{
breath_counter++;
uint16_t duty_cycle = abs((100 - breath_counter % 200)); // 模拟呼吸灯效果
hal_gpio_write(LED_PIN, duty_cycle > 50? GPIO_STATE_HIGH:GPIO_STATE_LOW);
}
if (current_led_mode == LED_MODE_RAINBOW)
{

static uint8_t hue=0;
hue+=5;
//根据hue的值调整PWM输出,实现彩虹效果
//可以采用RGB灯,并转换HSV为RGB值
}
}
// ddl_sensor.h - 传感器驱动头文件
#ifndef DDL_SENSOR_H
#define DDL_SENSOR_H

typedef struct {
float temperature;
float humidity;
} sensor_data_t;

void ddl_sensor_init(void);
sensor_data_t ddl_sensor_read(void);

#endif

// ddl_sensor.c - 传感器驱动实现
#include "ddl_sensor.h"
#include "hal_gpio.h"
#include "hal_timer.h"
// 假设使用AHT20传感器,需要实现I2C通信
#include <stdio.h>
// 定义I2C通信的相关宏
#define AHT20_ADDRESS 0x38
#define AHT20_CMD_INIT 0xBE
#define AHT20_CMD_MEASURE 0xAC
#define AHT20_CMD_READ_STATUS 0x71

#define I2C_SCL_PIN GPIO_PIN_1
#define I2C_SDA_PIN GPIO_PIN_2
void delay_us(uint32_t us) {
// 实现微秒级延时函数
volatile uint32_t i;
for (i = 0; i < us; i++) {
__asm("nop");
}
}

void i2c_start(void)
{
hal_gpio_write(I2C_SDA_PIN, GPIO_STATE_HIGH);
hal_gpio_write(I2C_SCL_PIN, GPIO_STATE_HIGH);
delay_us(1);
hal_gpio_write(I2C_SDA_PIN, GPIO_STATE_LOW);
delay_us(1);
hal_gpio_write(I2C_SCL_PIN, GPIO_STATE_LOW);
}

void i2c_stop(void)
{
hal_gpio_write(I2C_SDA_PIN, GPIO_STATE_LOW);
hal_gpio_write(I2C_SCL_PIN, GPIO_STATE_HIGH);
delay_us(1);
hal_gpio_write(I2C_SDA_PIN, GPIO_STATE_HIGH);
delay_us(1);
}
void i2c_send_byte(uint8_t data)
{
uint8_t i;
for (i = 0; i < 8; i++)
{
if (data & 0x80)
{
hal_gpio_write(I2C_SDA_PIN, GPIO_STATE_HIGH);
}
else
{
hal_gpio_write(I2C_SDA_PIN, GPIO_STATE_LOW);
}
hal_gpio_write(I2C_SCL_PIN, GPIO_STATE_HIGH);
delay_us(1);
hal_gpio_write(I2C_SCL_PIN, GPIO_STATE_LOW);
delay_us(1);
data <<= 1;
}
}

uint8_t i2c_read_byte(uint8_t ack)
{
uint8_t i, data = 0;
hal_gpio_init(I2C_SDA_PIN,GPIO_MODE_INPUT);
for (i = 0; i < 8; i++)
{
hal_gpio_write(I2C_SCL_PIN, GPIO_STATE_HIGH);
delay_us(1);
data <<= 1;
if (hal_gpio_read(I2C_SDA_PIN)== GPIO_STATE_HIGH)
{
data |= 0x01;
}
hal_gpio_write(I2C_SCL_PIN, GPIO_STATE_LOW);
delay_us(1);
}
hal_gpio_init(I2C_SDA_PIN,GPIO_MODE_OUTPUT);
if (!ack)
{
hal_gpio_write(I2C_SDA_PIN, GPIO_STATE_HIGH);
}
else
{
hal_gpio_write(I2C_SDA_PIN, GPIO_STATE_LOW);
}
hal_gpio_write(I2C_SCL_PIN, GPIO_STATE_HIGH);
delay_us(1);
hal_gpio_write(I2C_SCL_PIN, GPIO_STATE_LOW);
delay_us(1);

return data;
}
void aht20_init() {
hal_gpio_init(I2C_SCL_PIN, GPIO_MODE_OUTPUT);
hal_gpio_init(I2C_SDA_PIN, GPIO_MODE_OUTPUT);
i2c_start();
i2c_send_byte(AHT20_ADDRESS << 1);
i2c_send_byte(AHT20_CMD_INIT);
i2c_send_byte(0x08);
i2c_send_byte(0x00);
i2c_send_byte(0x00);
i2c_stop();
delay_us(100);
}

static uint8_t aht20_read_status() {
uint8_t status;
i2c_start();
i2c_send_byte(AHT20_ADDRESS << 1);
i2c_send_byte(AHT20_CMD_READ_STATUS);
status = i2c_read_byte(0); // 读取状态寄存器
i2c_stop();
return status;
}

void aht20_start_measure() {
i2c_start();
i2c_send_byte(AHT20_ADDRESS << 1);
i2c_send_byte(AHT20_CMD_MEASURE);
i2c_send_byte(0x33);
i2c_send_byte(0x00);
i2c_stop();
delay_us(80);
}

sensor_data_t aht20_read_data() {

sensor_data_t data;
uint32_t raw_data;
uint8_t buffer[6];

// 等待测量完成
uint8_t status = aht20_read_status();
while((status & 0x80) != 0 ) {
delay_us(100);
status = aht20_read_status();
}
i2c_start();
i2c_send_byte((AHT20_ADDRESS << 1) | 0x01); // 发送读取命令
buffer[0]=i2c_read_byte(1);
buffer[1]=i2c_read_byte(1);
buffer[2]=i2c_read_byte(1);
buffer[3]=i2c_read_byte(1);
buffer[4]=i2c_read_byte(1);
buffer[5]=i2c_read_byte(0);
i2c_stop();
// 解析数据
raw_data = ((uint32_t)buffer[1] << 12) | ((uint32_t)buffer[2] << 4) | ((uint32_t)buffer[3] >> 4);
data.humidity= (float)(raw_data*100.0f/1048576.0f);
raw_data = ((uint32_t)(buffer[3] & 0x0f) << 16)| ((uint32_t)buffer[4] << 8) | ((uint32_t)buffer[5]);
data.temperature= (float)(raw_data*200.0f/1048576.0f)-50.0f;
return data;
}


void ddl_sensor_init(void) {
aht20_init();
}
sensor_data_t ddl_sensor_read(void) {
aht20_start_measure();
return aht20_read_data();
}
// ddl_audio.h - 音频驱动头文件
#ifndef DDL_AUDIO_H
#define DDL_AUDIO_H

typedef enum {
AUDIO_MODE_OFF,
AUDIO_MODE_PLAY,
AUDIO_MODE_STOP
} audio_mode_t;

typedef enum {
AUDIO_SOUND_BAMBOO,
AUDIO_SOUND_FESTIVAL,
AUDIO_SOUND_SLEEP,
AUDIO_SOUND_MAX
} audio_sound_t;


void ddl_audio_init(void);
void ddl_audio_set_mode(audio_mode_t mode);
void ddl_audio_play_sound(audio_sound_t sound);
void ddl_audio_set_volume(uint8_t volume);


#endif

// ddl_audio.c - 音频驱动实现
#include "ddl_audio.h"
#include "hal_gpio.h"

#define SPEAKER_PIN GPIO_PIN_3
#define TIMER_AUDIO TIMER_2
// 音频数据可以存储在Flash或者SD卡中,这里简化处理
static audio_mode_t current_audio_mode=AUDIO_MODE_OFF;
static uint16_t sound_index=0;

void ddl_audio_init(void) {
hal_gpio_init(SPEAKER_PIN, GPIO_MODE_OUTPUT);
hal_gpio_write(SPEAKER_PIN, GPIO_STATE_LOW); // 初始关闭音频输出
}
void ddl_audio_set_volume(uint8_t volume){
//根据音量大小调整PWM输出
}
void ddl_audio_set_mode(audio_mode_t mode){
current_audio_mode=mode;
switch(current_audio_mode){
case AUDIO_MODE_OFF:
hal_gpio_write(SPEAKER_PIN, GPIO_STATE_LOW);
break;
case AUDIO_MODE_PLAY:
// 使用定时器产生PWM波形
break;
case AUDIO_MODE_STOP:
hal_gpio_write(SPEAKER_PIN, GPIO_STATE_LOW);
break;
default:
break;
}
}
void ddl_audio_play_sound(audio_sound_t sound){
//根据选择的sound播放对应的音乐
switch(sound){
case AUDIO_SOUND_BAMBOO:
// 模拟竹筒声,采用不同频率的方波
//...
break;
case AUDIO_SOUND_FESTIVAL:
// 模拟爆竹声,采用短促的方波
//...
break;
case AUDIO_SOUND_SLEEP:
// 播放舒缓音乐
//...
break;
default:
break;
}
}
// ddl_bluetooth.h - 蓝牙驱动头文件
#ifndef DDL_BLUETOOTH_H
#define DDL_BLUETOOTH_H

#include <stdint.h>
typedef void (*bluetooth_data_callback_t)(uint8_t *data, uint16_t len);

void ddl_bluetooth_init(bluetooth_data_callback_t callback);
void ddl_bluetooth_send_data(uint8_t *data, uint16_t len);

#endif

// ddl_bluetooth.c - 蓝牙驱动实现
#include "ddl_bluetooth.h"
#include "hal_gpio.h"
#include "hal_timer.h"
#include <stdio.h>
// 假设使用串口连接蓝牙模块
// 定义蓝牙模块连接的串口引脚
#define BLUETOOTH_TX_PIN GPIO_PIN_4
#define BLUETOOTH_RX_PIN GPIO_PIN_5

static bluetooth_data_callback_t bluetooth_callback;

void ddl_bluetooth_init(bluetooth_data_callback_t callback) {
hal_gpio_init(BLUETOOTH_TX_PIN, GPIO_MODE_OUTPUT);
hal_gpio_init(BLUETOOTH_RX_PIN, GPIO_MODE_INPUT);
bluetooth_callback=callback;
// 初始化串口通信
// 根据硬件设置串口波特率等
// 开启串口中断
}

void ddl_bluetooth_send_data(uint8_t *data, uint16_t len) {
// 发送数据
for(int i=0;i<len;i++) {
// 模拟串口发送数据
}
}
// 假设接收到串口数据,触发中断
/*
void USART1_IRQHandler(void){
if(USART1->SR & USART_SR_RXNE){
uint8_t data= USART1->DR;
//将数据存储到缓冲区
static uint8_t buffer[256];
static uint16_t index=0;
buffer[index++]=data;
if (bluetooth_callback != NULL) {
bluetooth_callback(buffer, index);
}
}
}
*/

3. 中间件层 (MWL):

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
// mwl_network.h - 网络中间件头文件
#ifndef MWL_NETWORK_H
#define MWL_NETWORK_H

void mwl_network_init(void);
void mwl_network_connect_wifi(const char *ssid, const char *password);
void mwl_network_disconnect_wifi(void);
// 其他网络相关功能,例如发送HTTP请求
#endif

// mwl_network.c - 网络中间件实现
#include "mwl_network.h"
#include <stdio.h>
#include <string.h>
// 使用WiFi驱动进行WIFI连接
void mwl_network_init(void) {
// 初始化WiFi驱动
printf("network init\r\n");
}

void mwl_network_connect_wifi(const char *ssid, const char *password) {
// 连接到WiFi
printf("connect wifi: %s \r\n", ssid);
}

void mwl_network_disconnect_wifi(void) {
// 断开WiFi连接
printf("disconnect wifi\r\n");
}
// mwl_time.h - 时间同步中间件头文件
#ifndef MWL_TIME_H
#define MWL_TIME_H
#include <stdint.h>
typedef struct {
uint16_t year;
uint8_t month;
uint8_t day;
uint8_t hour;
uint8_t minute;
uint8_t second;
} time_t;

void mwl_time_init(void);
void mwl_time_sync(void);
time_t mwl_time_get(void);
#endif
// mwl_time.c - 时间同步中间件实现
#include "mwl_time.h"
#include "mwl_network.h"
#include <stdio.h>
#include <string.h>
#include <time.h>
static time_t current_time;

void mwl_time_init(void) {
// 初始化时间相关配置
current_time.year=2024;
current_time.month=1;
current_time.day=1;
current_time.hour=0;
current_time.minute=0;
current_time.second=0;
printf("time init\r\n");
}

void mwl_time_sync(void) {
// 通过NTP或其他方式同步时间
// 模拟时间同步
current_time.second +=10;
if(current_time.second>=60)
{
current_time.second-=60;
current_time.minute+=1;
if(current_time.minute>=60)
{
current_time.minute-=60;
current_time.hour+=1;
if(current_time.hour>=24)
{
current_time.hour=0;
current_time.day+=1;
}
}

}
printf("time sync\r\n");
}

time_t mwl_time_get(void) {
return current_time;
}
// mwl_config.h - 参数配置中间件头文件
#ifndef MWL_CONFIG_H
#define MWL_CONFIG_H

typedef struct {
uint8_t led_brightness;
uint8_t audio_volume;
char wifi_ssid[32];
char wifi_password[32];
} config_t;

void mwl_config_init(void);
config_t mwl_config_get(void);
void mwl_config_set(config_t config);
#endif
// mwl_config.c - 参数配置中间件实现
#include "mwl_config.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "flash.h" //假设使用flash存储配置
static config_t current_config;
#define CONFIG_FLASH_ADDR 0x08000000 //假设从flash的起始地址开始存储配置
void mwl_config_init(void) {
// 从 Flash 中读取配置
flash_read((uint8_t*)&current_config,sizeof(config_t),CONFIG_FLASH_ADDR);
// 检查配置数据是否有效
if(strlen(current_config.wifi_ssid)==0)
{
current_config.led_brightness = 100;
current_config.audio_volume = 50;
strcpy(current_config.wifi_ssid,"test_ssid");
strcpy(current_config.wifi_password,"test_password");
flash_erase(CONFIG_FLASH_ADDR, sizeof(config_t));
flash_write((uint8_t*)&current_config,sizeof(config_t),CONFIG_FLASH_ADDR);
}

printf("config init\r\n");
}

config_t mwl_config_get(void) {
return current_config;
}

void mwl_config_set(config_t config) {
current_config = config;
flash_erase(CONFIG_FLASH_ADDR, sizeof(config_t));
flash_write((uint8_t*)&current_config,sizeof(config_t),CONFIG_FLASH_ADDR);
printf("config save\r\n");
}

4. 应用层 (APP):

// app_main.h - 应用层头文件
#ifndef APP_MAIN_H
#define APP_MAIN_H

void app_init(void);
void app_run(void);
#endif
// app_main.c - 应用层实现
#include "app_main.h"
#include "ddl_led.h"
#include "ddl_sensor.h"
#include "ddl_audio.h"
#include "ddl_bluetooth.h"
#include "mwl_network.h"
#include "mwl_time.h"
#include "mwl_config.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
static uint8_t receive_buffer[256];
static uint16_t receive_index=0;
static void bluetooth_data_received(uint8_t *data, uint16_t len);
void app_init(void) {
    // 初始化所有模块
    printf("app init \r\n");
    ddl_led_init();
    ddl_sensor_init();
    ddl_audio_init();
    ddl_bluetooth_init(bluetooth_data_received);
    mwl_network_init();
    mwl_time_init();
    mwl_config_init();
    config_t config = mwl_config_get();
    ddl_led_set_brightness(config.led_brightness);
    ddl_audio_set_volume(config.audio_volume);
   mwl_network_connect_wifi(config.wifi_ssid, config.wifi_password);
}
void app_run(void) {
    // 主循环
     time_t time;
    while (1) {
        mwl_time_sync(); // 定期同步时间
         time = mwl_time_get();
         printf("time : %d:%d:%d \r\n",time.hour, time.minute,time.second );

       sensor_data_t sensor_data = ddl_sensor_read();

        printf("temp : %.2f ,hum : %.2f \r\n", sensor_data.temperature,sensor_data.humidity);
        // 根据状态进行LED等操作
        // 读取传感器数据,根据数据调整灯光或音乐
        // 可以响应蓝牙指令,或者根据时间执行操作
        //  接收蓝牙数据
         // 根据接收到的数据执行相应的操作
    }
}

void bluetooth_data_received(uint8_t *data, uint16_t len){
  if(len>0)
    {
       for(int i=0;i<len;i++) {
            printf("recv: %x \r\n",data[i]);
       }
        if(data[0]=='l')
       {
           if(data[1]=='1')
           {
              ddl_led_set_mode(LED_MODE_ON);
           }
           else if(data[1]=='0')
           {
               ddl_led_set_mode(LED_MODE_OFF);
           }
            else if(data[1]=='b')
            {
              ddl_led_set_mode(LED_MODE_BREATH);
            }
            else if(data[1]=='r')
            {
                ddl_led_set_mode(LED_MODE_RAINBOW);
            }
       }
        if(data[0]=='a')
        {
            if(data[1]=='1')
           {
               ddl_audio_play_sound(AUDIO_SOUND_BAMBOO);
                ddl_audio_set_mode(AUDIO_MODE_PLAY);
           }
            else if(data[1]=='2')
            {
                ddl_audio_play_sound(AUDIO_SOUND_FEST

欢迎关注我的其它发布渠道