led.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "led.h"
  2. #include "timer.h"
  3. #include "gpio.h"
  4. #include "elog.h"
  5. #include "common.h"
  6. #if 1
  7. #define LOGD log_d
  8. #define LOGE log_e
  9. #else
  10. #define LOGD printf
  11. #define LOGE printf
  12. #endif
  13. #define LEDR_GPIO 106 //PD10=96+10
  14. #define LEDG_GPIO 105 //PD9=96+9
  15. typedef struct {
  16. int gpio;
  17. int on_level;
  18. int off_level;
  19. }led_info_t;
  20. static led_info_t led_info[LED_ID_MAX]={
  21. {LEDR_GPIO, 0, 1},
  22. {LEDG_GPIO, 0, 1},
  23. };
  24. typedef struct {
  25. int io;
  26. int tmr;
  27. led_para_t para;
  28. led_info_t *info;
  29. uint32_t cnt;
  30. int id;
  31. }led_data_t;
  32. typedef struct {
  33. led_data_t data[LED_ID_MAX];
  34. }led_handle_t;
  35. static led_handle_t ledHandle={0};
  36. static void tmr_handler(void *arg, int last)
  37. {
  38. int level;
  39. led_data_t *h=(led_data_t*)arg;
  40. h->cnt++;
  41. if(h->cnt%2) {
  42. if(h->para.off_ms>0) {
  43. gpio_set(h->io, h->info->on_level);
  44. timer_setup(h->tmr, h->para.off_ms, 0);
  45. }
  46. }
  47. else {
  48. if(h->para.on_ms>0) {
  49. gpio_set(h->io, h->info->off_level);
  50. timer_setup(h->tmr, h->para.on_ms, 0);
  51. }
  52. }
  53. if(last) {
  54. timer_stop(h->tmr);
  55. }
  56. }
  57. //////////////////////////////////////////////////
  58. int led_init(void)
  59. {
  60. int i;
  61. led_handle_t *h=&ledHandle;
  62. for(i=0; i<LED_ID_MAX; i++) {
  63. h->data[i].info = &led_info[i];
  64. h->data[i].io = gpio_init(led_info[i].gpio, GPIO_OUT);
  65. gpio_set(h->data[i].io, led_info[i].off_level);
  66. h->data[i].tmr = -1;
  67. h->data[i].cnt = 0;
  68. }
  69. //led_para_t para={1000, 1000, -1};
  70. //led_set_ex(LED_ID_RED, &para);
  71. return 0;
  72. }
  73. int led_deinit(void)
  74. {
  75. int i;
  76. led_handle_t *h=&ledHandle;
  77. for(i=0; i<LED_ID_MAX; i++) {
  78. gpio_deinit(h->data[i].io);
  79. timer_stop(h->data[i].tmr);
  80. h->data[i].io = -1;
  81. h->data[i].tmr = -1;
  82. }
  83. return 0;
  84. }
  85. int led_set(int mode)
  86. {
  87. return 0;
  88. }
  89. int led_set_ex(int led_id, led_para_t *para)
  90. {
  91. led_handle_t *h=&ledHandle;
  92. if(!para || para->times==0) {
  93. return -1;
  94. }
  95. led_data_t *ld=&h->data[led_id];
  96. if(ld->tmr!=-1) {
  97. timer_stop(ld->tmr);
  98. }
  99. ld->cnt = 0;
  100. ld->para = *para;
  101. if(ld->para.on_ms>0 && ld->para.off_ms>0) {
  102. ld->para.times *= 2;
  103. }
  104. ld->tmr = timer_start(ld->para.on_ms, 0, ld->para.times, tmr_handler, ld);
  105. return ld->tmr;
  106. }
  107. int led_stop(int h)
  108. {
  109. led_data_t *ld=(led_data_t*)h;
  110. if(h==-1) {
  111. return -1;
  112. }
  113. timer_stop(ld->tmr);
  114. ld->tmr = -1;
  115. return 0;
  116. }