ui.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "ui.h"
  2. #include "lcd.h"
  3. #include "gpio.h"
  4. #include "thread.h"
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <linux/input.h>
  8. #if 1
  9. #define LOGD log_d
  10. #define LOGE log_e
  11. #else
  12. #define LOGD printf
  13. #define LOGE printf
  14. #endif
  15. typedef struct {
  16. int tp;
  17. int key;
  18. int up; //0: release 1: press
  19. }key_data_t;
  20. typedef struct {
  21. int fd;
  22. }ui_handle_t;
  23. static ui_handle_t uiHandle={0};
  24. static int key_init(ui_handle_t *h)
  25. {
  26. h->fd = open("/dev/input/event0", O_RDWR, 0);
  27. return (h->fd<0)?-1:0;
  28. }
  29. static int key_deinit(ui_handle_t *h)
  30. {
  31. close(h->fd);
  32. return 0;
  33. }
  34. static int key_get(ui_handle_t *h, key_data_t *k)
  35. {
  36. int r;
  37. struct input_event event;
  38. r = read(h->fd, &event, sizeof(event));
  39. if(r<0) {
  40. return -1;
  41. }
  42. printf("_____ evt.type: %d, value: %d, code: %d\n", event.type, event.value, event.code);
  43. switch (event.type) {
  44. case EV_KEY:
  45. {
  46. k->tp = event.type;
  47. k->up = (event.value==0)?1:0;
  48. k->key = event.code;
  49. }
  50. break;
  51. }
  52. return 0;
  53. }
  54. static int menu_init(ui_handle_t *h)
  55. {
  56. key_init(h);
  57. lcd_init(LCD_WIDTH,LCD_HEIGHT,LCD_COLOR);
  58. lcd_fill(0);
  59. //lcd_draw_string(10, 10, 80, 30, "Smart; Hello!", FONT_0806, COLOR_WHITE, COLOR_BLACK, HORIZONTAL_LEFT, VERTICAL_CENTER);
  60. //lcd_draw_string(10, 30, 80, 30, "Voltage: 22.4V", FONT_0806, COLOR_WHITE, COLOR_BLACK, HORIZONTAL_LEFT, VERTICAL_CENTER);
  61. lcd_draw_string(10, 30, 80, 30, "Voltage: 22.4V", FONT_1206, COLOR_WHITE, COLOR_BLACK, HORIZONTAL_LEFT, VERTICAL_CENTER);
  62. lcd_refresh();
  63. return 0;
  64. }
  65. static int menu_deinit(ui_handle_t *h)
  66. {
  67. key_deinit(h);
  68. lcd_deinit();
  69. return 0;
  70. }
  71. static int menu_handle(ui_handle_t *h)
  72. {
  73. int r;
  74. key_data_t kd;
  75. r = key_get(h, &kd);
  76. if(r==0) {
  77. switch(kd.key) {
  78. case KEY_LEFT:
  79. break;
  80. }
  81. }
  82. return 0;
  83. }
  84. static void *ui_thread(void *arg)
  85. {
  86. int r;
  87. thread_handle_t *th=(thread_handle_t*)arg;
  88. ui_handle_t *h=(ui_handle_t*)th->arg;
  89. menu_init(h);
  90. while(th->quit==0) {
  91. menu_handle(h);
  92. }
  93. menu_deinit(h);
  94. pthread_exit(NULL);
  95. }
  96. /////////////////////////////////////////////////////
  97. int ui_init(void)
  98. {
  99. ui_handle_t *h=&uiHandle;
  100. thread_start(THREAD_ID_UI, ui_thread, h);
  101. return 0;
  102. }