led呼吸闪烁驱动
1 #include "led.h" 2 #include "gpio.h" 3 u8 led_mode = 0; //led 灯显 4 LED_s led[4]; 5 6 void on_start_red(void) 7 { 8 ON_LEDRED; 9 } 10 void off_start_red(void) 11 { 12 OFF_LEDRED; 13 } 14 void on_start_blue(void) 15 { 16 ON_LEDGREEN; 17 } 18 void off_start_blue(void) 19 { 20 OFF_LEDGREEN; 21 } 22 23 24 void on_homecharge(void) 25 { 26 ON_HOMELED; 27 } 28 void off_homecharge(void) 29 { 30 OFF_HOMELED; 31 } 32 33 34 void on_clean(void) 35 { 36 ON_LOCAL_CLEAN_LED; 37 } 38 void off_clean(void) 39 { 40 OFF_LOCAL_CLEAN_LED; 41 } 42 43 /***************************************************************************************/ 44 45 /*100us中断调用*/ 46 void led_duty_refresh(void) 47 { 48 u8 i = 0; 49 for(i = 0; i < LED_NUM; i++) 50 { 51 if(led[i].breath_en) 52 { 53 led[i].cnt++; 54 if(led[i].cnt >= 100)led[i].cnt = 0; 55 56 if(led[i].cnt < led[i].duty) 57 { 58 led[i].on(); 59 } 60 else 61 { 62 led[i].off(); 63 } 64 } 65 } 66 } 67 68 void led_breath(LED_s * led) 69 { 70 if(led->breath_en == 0)return; 71 led->speed_cnt++; 72 if(led->speed_cnt >= led->speed) 73 { 74 led->speed_cnt = 0; 75 switch(led->step) 76 { 77 case 0: 78 led->duty++; 79 if(led->duty >= 100) 80 { 81 led->step ++; 82 } 83 break; 84 case 1: 85 led->duty--; 86 if(led->duty <= 1) 87 { 88 led->step++; 89 if(led->wait == 0)led->step = 0; //呼吸到最暗不暂停 90 led->wait_cnt = 0; 91 } 92 break; 93 case 2: 94 led->wait_cnt++; 95 if(led->wait_cnt >= (led->wait_ms / led->speed)) 96 { 97 led->step = 0; 98 led->wait_cnt = 0; 99 } 100 break; 101 default: 102 led->step = 0; 103 break; 104 } 105 } 106 } 107 108 /* 109 函数:led_breath_set()呼吸参数设置 110 参数: 111 led:设置的目标指针 112 en:是否使能呼吸 113 speed:呼吸的速度 114 wait:最暗时是否停留 115 wait_ms:最暗时停留多久 116 */ 117 void led_breath_set(LED_s * led, u8 en, u8 speed, u8 wait, u16 wait_ms) 118 { 119 led->breath_en = en; 120 led->speed = speed ? speed : 1; 121 led->wait = wait; 122 led->wait_ms = wait_ms; 123 } 124 void led_blink(LED_s * led) 125 { 126 if(led->blink_en == 0)return; 127 led->blink_cnt++; 128 if(led->blink_cnt <= led->blink_on_ms) 129 { 130 led->on(); 131 } 132 else if( (led->blink_cnt > led->blink_on_ms) && (led->blink_cnt <= (led->blink_on_ms + led->blink_off_ms)) ) 133 { 134 led->off(); 135 } 136 else 137 { 138 led->blink_cnt = 0; 139 } 140 } 141 /* 142 函数:led_blink_set()闪烁参数设置 143 参数: 144 led:设置的目标指针 145 en:是否使能闪烁 146 blink_on_ms:亮的时间 147 blink_off_ms:灭的时间 148 */ 149 150 void led_blink_set(LED_s * led, u8 en, u16 blink_on_ms, u16 blink_off_ms) 151 { 152 led->blink_en = en; 153 led->blink_on_ms = blink_on_ms; 154 led->blink_off_ms = blink_off_ms; 155 156 } 157 158 void led_init(void) 159 { 160 memset(led, 0, sizeof(led)); 161 led[0].on = on_start_red; 162 led[0].off = off_start_red; 163 led[0].off(); 164 165 led[1].on = on_start_blue; 166 led[1].off = off_start_blue; 167 led[1].off(); 168 169 led[2].on = on_homecharge; 170 led[2].off = off_homecharge; 171 led[2].off(); 172 173 led[3].on = on_clean; 174 led[3].off = off_clean; 175 led[3].off(); 176 } 177 /* 178 主机按键灯显定义交互 179 模式状态灯 180 181 序号 状态 启动键(红蓝) 回充灯(黄) 局部清扫键(蓝) 备注 182 1 无网络 无变化 熄灭 熄灭 "呼吸指:4s一呼吸,由最暗到最亮至最暗,之后最暗时间持续1秒;慢闪指:3s一呼吸;快闪指:亮1秒,灭1秒;" 183 2 已联网 无变化 熄灭 熄灭 184 3 开机 蓝灯长亮 熄灭 熄灭 185 4 待机 蓝灯长亮 熄灭 熄灭 186 5 待机-低电状态 红灯呼吸 熄灭 熄灭 187 6 休眠 熄灭 熄灭 熄灭 188 7 清扫 蓝灯长亮 熄灭 熄灭 189 8 暂停 蓝灯长亮 熄灭 熄灭 190 9 回充 熄灭 黄灯慢闪 熄灭 191 10 回充-低电状态 熄灭 黄灯慢闪 熄灭 192 11 充电 熄灭 黄灯呼吸 熄灭 193 12 充电-低电状态 熄灭 黄灯呼吸 熄灭 194 13 异常 红灯快闪 熄灭 熄灭 195 14 遥控(APP) 蓝灯长亮 熄灭 熄灭 196 15 充满电 蓝灯长亮 熄灭 熄灭 197 16 关机 熄灭 熄灭 熄灭 198 17 产测老化跑机 红灯常亮 熄灭 熄灭 199 18 重置WiFi 蓝灯快闪 熄灭 熄灭 200 19 局部清扫 熄灭 熄灭 蓝灯长亮 201 20 433配对 蓝灯快闪 熄灭 蓝灯快闪 202 21 清洗拖布 熄灭 黄灯慢闪 熄灭 203 22 烘干拖布(非满电)熄灭 黄灯呼吸 熄灭 204 23 烘干拖布(满电) 蓝灯长亮 熄灭 熄灭 205 24 集尘 熄灭 黄灯慢闪 熄灭 206 25 升级固件 蓝灯慢闪 熄灭 熄灭 207 208 */ 209 210 void led_mode_set(u8 mode) 211 { 212 led_mode = mode; 213 if((mode != 1) && (mode != 2)) 214 { 215 memset(&led[0], 0, sizeof(led[0]) - 8); //函数指针不清0 216 memset(&led[1], 0, sizeof(led[1]) - 8); //函数指针不清0 217 led[0].off(); 218 led[1].off(); 219 } 220 221 memset(&led[2], 0, sizeof(led[2]) - 8); //函数指针不清0 222 memset(&led[3], 0, sizeof(led[3]) - 8); //函数指针不清0 223 led[2].off(); 224 led[3].off(); 225 226 switch(mode) 227 { 228 case 1: 229 break; 230 case 2: 231 break; 232 case 3: 233 led[LED_START_BLUE].on(); 234 break; 235 case 4: 236 led[LED_START_BLUE].on(); 237 break; 238 case 5: 239 led_breath_set(&led[LED_START_RED], 1, 15, 1, 1000); 240 break; 241 case 6: 242 break; 243 case 7: 244 led[LED_START_BLUE].on(); 245 break; 246 case 8: 247 led[LED_START_BLUE].on(); 248 break; 249 case 9: 250 led_breath_set(&led[LED_HOME_CHARGE], 1, 15, 0, 1000); //慢闪 251 break; 252 case 10: 253 led_breath_set(&led[LED_HOME_CHARGE], 1, 15, 0, 1000); //慢闪 254 break; 255 case 11: 256 led_breath_set(&led[LED_HOME_CHARGE], 1, 15, 1, 1000); //呼吸 257 break; 258 case 12: 259 led_breath_set(&led[LED_HOME_CHARGE], 1, 15, 1, 1000); //呼吸 260 break; 261 case 13: 262 led_blink_set(&led[LED_START_RED], 1, 1000, 1000); 263 break; 264 case 14: 265 led[LED_START_BLUE].on(); 266 break; 267 case 15: 268 led[LED_START_BLUE].on(); 269 break; 270 case 16: 271 break; 272 case 17: 273 led[LED_START_RED].on(); 274 break; 275 case 18: 276 led_blink_set(&led[LED_START_BLUE], 1, 1000, 1000); 277 break; 278 case 19: 279 led[LED_CLEAN].on(); 280 break; 281 case 20: 282 led_blink_set(&led[LED_START_BLUE], 1, 1000, 1000); 283 led_blink_set(&led[LED_CLEAN], 1, 1000, 1000); 284 break; 285 case 21: 286 led_breath_set(&led[LED_HOME_CHARGE], 1, 15, 0, 1000); //慢闪 287 break; 288 case 22: 289 led_breath_set(&led[LED_HOME_CHARGE], 1, 15, 1, 1000); //呼吸 290 break; 291 case 23: 292 led[LED_START_BLUE].on(); 293 break; 294 case 24: 295 led_breath_set(&led[LED_HOME_CHARGE], 1, 15, 0, 1000); //慢闪 296 break; 297 case 25: 298 led_breath_set(&led[LED_START_BLUE], 1, 15, 0, 1000); //慢闪 299 break; 300 case 26: 301 led_breath_set(&led[LED_CLEAN], 1, 15, 0, 1000); //慢闪 302 led_breath_set(&led[LED_START_RED], 1, 15, 0, 1000); //慢闪 303 led_breath_set(&led[LED_HOME_CHARGE], 1, 15, 0, 1000); //慢闪 304 break; 305 default: 306 break; 307 } 308 } 309 /*1ms调用*/ 310 void led_task(void) 311 { 312 u8 i = 0; 313 for(i = 0; i < LED_NUM; i++) 314 { 315 led_breath(&led[i]); 316 } 317 for(i = 0; i < LED_NUM; i++) 318 { 319 led_blink(&led[i]); 320 } 321 322 } 323 324 long led_ctrl(int argc, char* arg[]) 325 { 326 if(argc > 2)return 0; 327 led_mode_set(atoi(arg[1])); 328 return 0; 329 } 330 331 332 MSH_CMD_EXPORT(led_ctrl, "led_ctrl 1,1-24");
1 #ifndef _LED_H_ 2 #define _LED_H_ 3 #include "main.h" 4 5 #define LED_NUM (4) //led 数量 6 #pragma __pack(1) 7 typedef enum 8 { 9 LED_START_RED = 0, 10 LED_START_BLUE = 1, 11 LED_HOME_CHARGE = 2, 12 LED_CLEAN = 3 13 } LED_INDEX; 14 15 16 typedef struct 17 { 18 u8 breath_en;//1-使能呼吸 0-不呼吸,需设置 19 u8 speed_cnt;//呼吸速度计数器 20 u8 speed;//呼吸速度,需设置 21 u8 cnt;//呼吸周期计数器 22 u8 duty;//呼吸亮度占空比 23 u8 step;//呼吸步骤 24 u8 wait;//呼吸到最暗是否暂停,需设置 25 u8 wait_cnt;//暂停计数器 26 u16 wait_ms;//暂停时间,需设置 27 u8 blink_en;//闪烁使能,需设置 28 u16 blink_cnt;//闪烁计数器 29 u16 blink_on_ms;//闪烁亮的时间,需设置 30 u16 blink_off_ms;//闪烁灭的时间,需设置 31 32 void (*on)(void); //开灯,需设置 33 void (*off)(void); //关灯,需设置 34 } LED_s; 35 36 #pragma __pack() 37 38 void led_duty_refresh(void); 39 void led_init(void); 40 void led_mode_set(u8 mode); 41 void led_task(void); 42 43 #endif