widgets.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * widgets.h
  3. *
  4. * Created on: Jan 12, 2021
  5. * Author: David Original work by Jose (PTDreamer), 2017
  6. */
  7. #ifndef GRAPHICS_GUI_WIDGETS_H_
  8. #define GRAPHICS_GUI_WIDGETS_H_
  9. #include "rotary_encoder.h"
  10. #define default_font u8g2_font_t0_16_tr
  11. // Just some fonts I tried and wanted to remember
  12. //u8g2_font_helvR10_tr // ok
  13. //u8g2_font_t0_16_tr // meh
  14. //u8g2_font_DigitalDisco_tr
  15. //u8g2_font_timB10_tr
  16. //u8g2_font_timB12_tr
  17. typedef enum widgetStateType {widget_idle, widget_selected, widget_edit, widget_error}widgetStateType;
  18. typedef enum widgetFieldType {field_int32, field_bmp, field_string}widgetFieldType;
  19. typedef enum AlignType { align_disabled, align_left, align_center, align_right }AlignType;
  20. typedef enum widgetFrameType {frame_auto, frame_solid, frame_outline, frame_disabled, frame_combo}widgetFrameType;
  21. typedef enum widgetType {widget_combo, widget_label, widget_display, widget_editable, widget_bmp, widget_multi_option, widget_button, widget_bmp_button}widgetType;
  22. typedef enum comboType {combo_Screen, combo_Editable, combo_MultiOption, combo_Action}comboType;
  23. typedef enum widgetRefreshType {refresh_idle, refresh_triggered, refresh_always}widgetRefreshType;
  24. typedef struct widget_t widget_t;
  25. typedef struct comboWidget_t comboWidget_t;
  26. typedef struct comboBox_item_t comboBox_item_t;
  27. typedef struct selectable_widget_t selectable_widget_t;
  28. typedef struct editable_widget_t editable_widget_t;
  29. typedef struct button_widget_t button_widget_t;
  30. typedef struct displayOnly_widget_t displayOnly_widget_t;
  31. typedef struct comboBox_widget_t comboBox_widget_t;
  32. typedef struct bmp_widget_t bmp_widget_t;
  33. struct selectable_widget_t {
  34. widgetStateType state;
  35. widgetStateType previous_state;
  36. uint8_t tab;
  37. int (*processInput)(widget_t*, RE_Rotation_t, RE_State_t *);
  38. int (*longPressAction)(widget_t*);
  39. };
  40. struct displayOnly_widget_t {
  41. widgetFieldType type;
  42. AlignType dispAlign;
  43. AlignType textAlign;
  44. int32_t last_value;
  45. uint8_t number_of_dec;
  46. uint8_t reservedChars;
  47. uint8_t stringStart;
  48. const uint8_t *font;
  49. char* displayString;
  50. char* endString;
  51. void * (*getData)();
  52. };
  53. struct editable_widget_t {
  54. int8_t current_edit;
  55. uint8_t numberOfOptions;
  56. int16_t step;
  57. int16_t big_step;
  58. int32_t min_value;
  59. int32_t max_value;
  60. char **options;
  61. void (*setData)(void *);
  62. selectable_widget_t selectable;
  63. displayOnly_widget_t inputData;
  64. };
  65. struct button_widget_t {
  66. union{
  67. struct{
  68. char* displayString;
  69. const uint8_t* font;
  70. int32_t last_value;
  71. uint8_t stringStart;
  72. };
  73. struct{
  74. const uint8_t* xbm;
  75. const uint8_t* last_xbm;
  76. };
  77. };
  78. int (*action)(widget_t*);
  79. selectable_widget_t selectable;
  80. };
  81. struct bmp_widget_t {
  82. const uint8_t* xbm;
  83. const uint8_t* last_xbm;
  84. };
  85. struct comboBox_widget_t {
  86. const uint8_t* font;
  87. selectable_widget_t selectable;
  88. comboBox_item_t *first;
  89. uint8_t currentScroll;
  90. comboBox_item_t *currentItem;
  91. } ;
  92. struct comboBox_item_t {
  93. comboBox_item_t *next_item;
  94. comboType type;
  95. uint8_t enabled;
  96. AlignType dispAlign;
  97. union{
  98. uint8_t action_screen;
  99. int (*action)();
  100. editable_widget_t *widget;
  101. };
  102. char *text;
  103. };
  104. struct widget_t
  105. {
  106. widgetType type;
  107. widget_t *next_widget;
  108. widgetRefreshType refresh;
  109. widgetFrameType frameType;
  110. uint8_t posX;
  111. uint8_t posY;
  112. uint8_t width;
  113. uint8_t enabled;
  114. int8_t radius;
  115. struct screen_t *parent;
  116. void (*draw)(widget_t*);
  117. void (*update)(widget_t*);
  118. void* content;
  119. };
  120. displayOnly_widget_t * extractDisplayPartFromWidget(widget_t *w);
  121. editable_widget_t * extractEditablePartFromWidget(widget_t *);
  122. selectable_widget_t * extractSelectablePartFromWidget(widget_t *w);
  123. void widgetAlign(widget_t* w);
  124. void widgetDefaultsInit(widget_t *w, widgetType t, void* content);
  125. void editableDefaultsInit(editable_widget_t* editable, widgetType type);
  126. void default_widgetDraw(widget_t* w);
  127. void default_widgetUpdate(widget_t *w);
  128. void widgetDetectChange(widget_t* w, int32_t val);
  129. void widgetClearField(widget_t* w);
  130. void widgetPrintVal(widget_t* w, int32_t val_ui);
  131. int32_t widgetGetVal(widget_t* w);
  132. void widgetEnable(widget_t* w);
  133. void widgetDisable(widget_t* w);
  134. int default_widgetProcessInput(widget_t* w, RE_Rotation_t, RE_State_t *);
  135. int comboBoxProcessInput(widget_t* w, RE_Rotation_t, RE_State_t *);
  136. void comboBoxDraw(widget_t *w);
  137. void comboAddScreen(comboBox_item_t* item, widget_t *combo, char *label, uint8_t actionScreen);
  138. //void comboAddEditable(comboBox_item_t* item, widget_t *combo, char *label, widget_t* w);
  139. void comboAddEditable(comboBox_item_t* item, widget_t *combo, char *label, editable_widget_t *editable);
  140. void comboAddMultiOption(comboBox_item_t* item, widget_t *combo, char *label, editable_widget_t *editable);
  141. void comboAddAction(comboBox_item_t* item, widget_t *combo, char *label, int (*action)());
  142. void comboResetIndex(widget_t *combo);
  143. int32_t strsum(char* str);
  144. #endif /* GRAPHICS_GUI_WIDGETS_H_ */