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
| #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
#include "ddl_led.h" #include "hal_gpio.h" #include "hal_timer.h" #include <stdlib.h>
#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); 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: hal_timer_start(TIMER_LED); break; default: break; } }
void ddl_led_set_brightness(uint8_t brightness) { current_brightness = brightness; }
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; } }
#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
#include "ddl_sensor.h" #include "hal_gpio.h" #include "hal_timer.h"
#include <stdio.h>
#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(); }
#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
#include "ddl_audio.h" #include "hal_gpio.h"
#define SPEAKER_PIN GPIO_PIN_3 #define TIMER_AUDIO TIMER_2
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){ } 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: 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){ switch(sound){ case AUDIO_SOUND_BAMBOO: break; case AUDIO_SOUND_FESTIVAL: break; case AUDIO_SOUND_SLEEP: break; default: break; } }
#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
#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++) { } }
|