main.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. main.c
  3. raspberry pi zero GPIO read test
  4. For speedup run this example as root, either wis
  5. sudo ./main
  6. or by setting the superuser bit:
  7. sudo chown root:root ./main
  8. sudo chmod u+s ./main
  9. */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <signal.h>
  13. #include "bcm2835.h"
  14. #include "raspi_gpio_hal.h"
  15. #include "u8g2.h"
  16. u8g2_t u8g2; // a structure which will contain all the data for one display
  17. void sigtermhandler(int x)
  18. {
  19. puts("SIGTERM");
  20. bcm2835_close();
  21. exit(0);
  22. }
  23. void siginthandler(int x)
  24. {
  25. puts("SIGINT");
  26. bcm2835_close();
  27. exit(0);
  28. }
  29. #define BUTTON_STATE_NOT_PRESSED 0
  30. #define BUTTON_STATE_HIGH_DETECT 1
  31. #define BUTTON_STATE_HIGH_WAIT 2
  32. #define BUTTON_STATE_LOW_DELAY 3
  33. struct button_struct
  34. {
  35. int state;
  36. int delay;
  37. uint8_t pin;
  38. };
  39. typedef struct button_struct button_t;
  40. #define BUTTON_DELAY_HIGH 2
  41. #define BUTTON_DELAY_LOW 2
  42. #define BUTTON_CNT 2
  43. button_t button_list[BUTTON_CNT];
  44. int button_init(void)
  45. {
  46. int i;
  47. for( i = 0; i < BUTTON_CNT; i++ )
  48. {
  49. bcm2835_gpio_fsel(button_list[i].pin, BCM2835_GPIO_FSEL_INPT);
  50. }
  51. }
  52. int button_get_event(void)
  53. {
  54. int button_value = -1;
  55. int i;
  56. for( i = 0; i < BUTTON_CNT; i++ )
  57. {
  58. switch(button_list[i].state)
  59. {
  60. case BUTTON_STATE_NOT_PRESSED:
  61. button_list[i].delay = 0;
  62. if ( bcm2835_gpio_lev(button_list[i].pin) != 0 )
  63. button_list[i].state = BUTTON_STATE_HIGH_DETECT;
  64. break;
  65. case BUTTON_STATE_HIGH_DETECT:
  66. if ( bcm2835_gpio_lev(button_list[i].pin) == 0 )
  67. {
  68. button_list[i].delay = 0;
  69. button_list[i].state = BUTTON_STATE_LOW_DELAY;
  70. }
  71. button_list[i].delay++;
  72. if ( button_list[i].delay >= BUTTON_DELAY_HIGH )
  73. {
  74. button_value = i;
  75. button_list[i].state = BUTTON_STATE_HIGH_WAIT;
  76. }
  77. break;
  78. case BUTTON_STATE_HIGH_WAIT:
  79. if ( bcm2835_gpio_lev(button_list[i].pin) == 0 )
  80. {
  81. button_list[i].delay = 0;
  82. button_list[i].state = BUTTON_STATE_LOW_DELAY;
  83. }
  84. break;
  85. case BUTTON_STATE_LOW_DELAY:
  86. button_list[i].delay++;
  87. if ( button_list[i].delay >= BUTTON_DELAY_LOW )
  88. button_list[i].state = BUTTON_STATE_NOT_PRESSED;
  89. break;
  90. }
  91. }
  92. return button_value;
  93. }
  94. /*
  95. title: NULL for no title, valid str for title line. Can contain mutliple lines, separated by '\n'
  96. start_pos: default position for the cursor, first line is 1.
  97. sl: string list (list of strings separated by \n)
  98. returns 0 if user has pressed the home key
  99. returns the selected line if user has pressed the select key
  100. side effects:
  101. u8g2_SetFontDirection(u8g2, 0);
  102. u8g2_SetFontPosBaseline(u8g2);
  103. */
  104. #define BUTTON_SELECT 0
  105. #define BUTTON_NEXT 1
  106. extern void u8g2_DrawSelectionList(u8g2_t *u8g2, u8sl_t *u8sl, u8g2_uint_t y, const char *s);
  107. #define MY_BORDER_SIZE 1
  108. #define MY_RULE_SIZE 2
  109. uint8_t u8g2_RaspiUserInterfaceSelectionList(u8g2_t *u8g2, const char *title, uint8_t start_pos, const char *sl)
  110. {
  111. u8sl_t u8sl;
  112. u8g2_uint_t yy;
  113. uint8_t event;
  114. u8g2_uint_t line_height = u8g2_GetAscent(u8g2) - u8g2_GetDescent(u8g2)+MY_BORDER_SIZE;
  115. uint8_t title_lines = u8x8_GetStringLineCnt(title);
  116. uint8_t display_lines;
  117. if ( start_pos > 0 ) /* issue 112 */
  118. start_pos--; /* issue 112 */
  119. if ( title_lines > 0 )
  120. {
  121. display_lines = (u8g2_GetDisplayHeight(u8g2)-2-MY_RULE_SIZE) / line_height;
  122. u8sl.visible = display_lines;
  123. u8sl.visible -= title_lines;
  124. }
  125. else
  126. {
  127. display_lines = u8g2_GetDisplayHeight(u8g2) / line_height;
  128. u8sl.visible = display_lines;
  129. }
  130. u8sl.total = u8x8_GetStringLineCnt(sl);
  131. u8sl.first_pos = 0;
  132. u8sl.current_pos = start_pos;
  133. if ( u8sl.current_pos >= u8sl.total )
  134. u8sl.current_pos = u8sl.total-1;
  135. if ( u8sl.first_pos+u8sl.visible <= u8sl.current_pos )
  136. u8sl.first_pos = u8sl.current_pos-u8sl.visible+1;
  137. u8g2_SetFontPosBaseline(u8g2);
  138. for(;;)
  139. {
  140. u8g2_FirstPage(u8g2);
  141. do
  142. {
  143. yy = u8g2_GetAscent(u8g2);
  144. if ( title_lines > 0 )
  145. {
  146. yy += u8g2_DrawUTF8Lines(u8g2, 0, yy, u8g2_GetDisplayWidth(u8g2), line_height, title);
  147. //u8g2_DrawHLine(u8g2, 0, yy-line_height- u8g2_GetDescent(u8g2) + 1, u8g2_GetDisplayWidth(u8g2));
  148. u8g2_DrawBox(u8g2, 0, yy-line_height- u8g2_GetDescent(u8g2) + 1, u8g2_GetDisplayWidth(u8g2), MY_RULE_SIZE);
  149. yy += 2+MY_RULE_SIZE;
  150. }
  151. u8g2_DrawSelectionList(u8g2, &u8sl, yy, sl);
  152. } while( u8g2_NextPage(u8g2) );
  153. #ifdef U8G2_REF_MAN_PIC
  154. return 0;
  155. #endif
  156. for(;;)
  157. {
  158. event = button_get_event();
  159. if ( event == BUTTON_SELECT )
  160. return u8sl.current_pos+1; /* +1, issue 112 */
  161. else if ( event == BUTTON_NEXT )
  162. {
  163. u8sl_Next(&u8sl);
  164. break;
  165. }
  166. /*
  167. else if ( event == U8X8_MSG_GPIO_MENU_PREV || event == U8X8_MSG_GPIO_MENU_UP )
  168. {
  169. u8sl_Prev(&u8sl);
  170. break;
  171. }
  172. */
  173. }
  174. }
  175. }
  176. int main(void)
  177. {
  178. int i = 0;
  179. int b = -1;
  180. int pins[4] = { 12, 13, 16, 17 };
  181. if (!bcm2835_init())
  182. exit(1);
  183. signal(SIGTERM, sigtermhandler);
  184. signal(SIGINT, siginthandler);
  185. atexit((void (*) (void))bcm2835_close);
  186. button_list[0].pin = 12;
  187. button_list[1].pin = 16;
  188. button_init();
  189. u8g2_Setup_sh1107_i2c_seeed_128x128_f(&u8g2, U8G2_R3, u8x8_byte_sw_i2c, u8x8_gpio_and_delay_raspi_gpio_hal); // init u8g2 structure
  190. u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_I2C_CLOCK, 5);
  191. u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_I2C_DATA, 6);
  192. u8g2_InitDisplay(&u8g2); // send init sequence to the display, display is in sleep mode after this,
  193. u8g2_SetPowerSave(&u8g2, 0); // wake up display
  194. u8g2_ClearBuffer(&u8g2);
  195. u8g2_SendBuffer(&u8g2);
  196. u8g2_SetFont(&u8g2, u8g2_font_lastapprenticebold_tr);
  197. //u8g2_SetFont(&u8g2, u8g2_font_helvB12_tr);
  198. //u8g2_DrawStr(&u8g2, 0, 50, "Hello");
  199. //u8g2_DrawStr(&u8g2, 0, 100, "World!");
  200. //u8g2_SendBuffer(&u8g2);
  201. u8g2_RaspiUserInterfaceSelectionList(&u8g2, "Menu", 1, "apple\nbanana\nraspberry\nMango");
  202. for( ;; )
  203. {
  204. b = button_get_event();
  205. if ( b >= 0 )
  206. printf("Button %d\n", b);
  207. }
  208. //delaynanoseconds(500000000UL);
  209. }