|
| 1 | +/** |
| 2 | + * \file . |
| 3 | + * Connects LVGL callbacks to display driver and input device. |
| 4 | + */ |
| 5 | +#include "GuiEngine.hpp" |
| 6 | +#include "Screen.hpp" |
| 7 | +#include <Adafruit_SSD1306.h> |
| 8 | +#include <memory> |
| 9 | +#include <user_interaction/MenuItem.hpp> |
| 10 | + |
| 11 | +/* lvgl log functionality via serial interface */ |
| 12 | +#if LV_USE_LOG != 0 |
| 13 | +#include <serial_interface/serial_port.hpp> |
| 14 | +static void lvgl_log_to_serial(const char *buf) |
| 15 | +{ |
| 16 | + serial_port::cout << buf << std::endl; |
| 17 | +} |
| 18 | +#endif |
| 19 | + |
| 20 | +/* Display flushing */ |
| 21 | +static void flushSSD1306Adafruit(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p) |
| 22 | +{ |
| 23 | + const auto displayAdapter = reinterpret_cast<GuiEngine *>(disp_drv->user_data); |
| 24 | + auto &display = displayAdapter->display; |
| 25 | + uint32_t width = (area->x2 - area->x1 + 1); |
| 26 | + uint32_t height = (area->y2 - area->y1 + 1); |
| 27 | + |
| 28 | + for (uint16_t row = 0; row < height; row++) |
| 29 | + { |
| 30 | + for (uint16_t col = 0; col < width; col++) |
| 31 | + { |
| 32 | + display.drawPixel(area->x1 + col, area->y1 + row, (color_p->full) ? SSD1306_WHITE : SSD1306_BLACK); |
| 33 | + color_p++; |
| 34 | + } |
| 35 | + } |
| 36 | + //this->display.display(); // this has been removed from here due to performance, as multiple flushes are triggered, when more areas on screen are refreshed |
| 37 | + lv_disp_flush_ready(disp_drv); |
| 38 | +} |
| 39 | + |
| 40 | +static IKeypad *myKeypad = nullptr; |
| 41 | + |
| 42 | +/** |
| 43 | + * @brief Construct a new Gui Engine:: GuiEngine object |
| 44 | + * |
| 45 | + * @param configuration - structure fith configuration information |
| 46 | + * @param i2c - reference to the i2c/TwoWire object for communication |
| 47 | + */ |
| 48 | +GuiEngine::GuiEngine(const Configuration &configuration, TwoWire &i2c) |
| 49 | + : display(configuration.screen_width, configuration.screen_height, &i2c), |
| 50 | + buf(std::make_unique<lv_color_t[]>(configuration.screen_width * 16)) |
| 51 | +{ |
| 52 | +#if LV_USE_LOG != 0 |
| 53 | + lv_log_register_print_cb(lvgl_log_to_serial); /* register print function for debugging */ |
| 54 | +#endif |
| 55 | + const std::uint8_t switchVcc = configuration.generateDisplayVoltageInternally ? SSD1306_SWITCHCAPVCC : SSD1306_EXTERNALVCC; |
| 56 | + const bool allocationSuccessful = display.begin(switchVcc, configuration.display_i2c_address); |
| 57 | + assert(allocationSuccessful); |
| 58 | + |
| 59 | + // Show initial display buffer contents on the screen -- |
| 60 | + // the library initializes this with an Adafruit splash screen. |
| 61 | + display.display(); |
| 62 | + delay(500); // Pause for short time |
| 63 | + |
| 64 | + // Clear the buffer |
| 65 | + display.clearDisplay(); |
| 66 | + |
| 67 | + // Show the display buffer on the screen. You MUST call display() after |
| 68 | + // drawing commands to make them visible on screen! |
| 69 | + display.display(); |
| 70 | + |
| 71 | + // Initialize lvgl library |
| 72 | + static lv_disp_draw_buf_t draw_buf; |
| 73 | + lv_init(); |
| 74 | + lv_disp_draw_buf_init(&draw_buf, buf.get(), NULL, configuration.screen_width * 16); |
| 75 | + |
| 76 | + // Initialize the display driver for lvgl |
| 77 | + static lv_disp_drv_t disp_drv; |
| 78 | + lv_disp_drv_init(&disp_drv); |
| 79 | + disp_drv.hor_res = configuration.screen_width; |
| 80 | + disp_drv.ver_res = configuration.screen_height; |
| 81 | + disp_drv.flush_cb = flushSSD1306Adafruit; |
| 82 | + disp_drv.draw_buf = &draw_buf; |
| 83 | + disp_drv.user_data = this; |
| 84 | + |
| 85 | + // Register display driver to lvgl |
| 86 | + lv_disp_drv_register(&disp_drv); |
| 87 | + delay(200); |
| 88 | + |
| 89 | + // Create monochomatic theme and set it as default |
| 90 | + lv_theme_t *mono_theme = lv_theme_mono_init(0, false, &lv_font_unscii_8); |
| 91 | + lv_disp_set_theme(0, mono_theme); |
| 92 | + |
| 93 | + // create first text in lvgl |
| 94 | + lv_obj_t *label = lv_label_create(lv_scr_act()); |
| 95 | + lv_label_set_text(label, "LVGL is up"); |
| 96 | + lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 0); |
| 97 | + |
| 98 | + // display lvgl screen |
| 99 | + lv_timer_handler(); |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * @brief implementation for lvgl read_cb for the left key |
| 104 | + */ |
| 105 | +static void keypad_read_left(lv_indev_drv_t *indev_drv, lv_indev_data_t *data) |
| 106 | +{ |
| 107 | + if (!myKeypad) |
| 108 | + return; |
| 109 | + |
| 110 | + // assign key state |
| 111 | + data->state = (myKeypad->isKeyPressed(KeyId::LEFT)) ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED; |
| 112 | + data->key = LV_KEY_PREV; |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * @brief implementation for lvgl read_cb for the right key |
| 117 | + */ |
| 118 | +static void keypad_read_right(lv_indev_drv_t *indev_drv, lv_indev_data_t *data) |
| 119 | +{ |
| 120 | + if (!myKeypad) |
| 121 | + return; |
| 122 | + |
| 123 | + // assign key state |
| 124 | + data->state = (myKeypad->isKeyPressed(KeyId::RIGHT)) ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED; |
| 125 | + data->key = LV_KEY_NEXT; |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * @brief implementation for lvgl read_cb for the enter key |
| 130 | + */ |
| 131 | +static void keypad_read_enter(lv_indev_drv_t *indev_drv, lv_indev_data_t *data) |
| 132 | +{ |
| 133 | + if (!myKeypad) |
| 134 | + return; |
| 135 | + |
| 136 | + // assign key state |
| 137 | + data->state = (myKeypad->isKeyPressed(KeyId::ENTER)) ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED; |
| 138 | + data->key = LV_KEY_ENTER; |
| 139 | +} |
| 140 | + |
| 141 | +/** |
| 142 | + * @brief implementation for lvgl read_cb for the back key |
| 143 | + */ |
| 144 | +static void keypad_read_back(lv_indev_drv_t *indev_drv, lv_indev_data_t *data) |
| 145 | +{ |
| 146 | + if (!myKeypad) |
| 147 | + return; |
| 148 | + |
| 149 | + // assign key state |
| 150 | + data->state = (myKeypad->isKeyPressed(KeyId::BACK)) ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED; |
| 151 | + data->key = LV_KEY_ESC; |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * @brief register the Keypad to read the button states from |
| 156 | + * |
| 157 | + * @param keypad - pointer to the Keypad object |
| 158 | + */ |
| 159 | +void GuiEngine::registerKeyPad(IKeypad *keypad) |
| 160 | +{ |
| 161 | + //assign keypad reference to local pointer |
| 162 | + myKeypad = keypad; |
| 163 | + |
| 164 | + // create default group for button navigation and assign input device to it |
| 165 | + static lv_group_t *const group = lv_group_create(); |
| 166 | + lv_group_set_default(group); |
| 167 | + |
| 168 | + // Register all buttons as individual indev, so they all have their separate "old" state (lvgl has only one "old" state per indev) |
| 169 | + static lv_indev_drv_t indev_drv_left; |
| 170 | + lv_indev_drv_init(&indev_drv_left); |
| 171 | + indev_drv_left.type = LV_INDEV_TYPE_KEYPAD; |
| 172 | + indev_drv_left.read_cb = keypad_read_left; |
| 173 | + |
| 174 | + static lv_indev_drv_t indev_drv_right; |
| 175 | + lv_indev_drv_init(&indev_drv_right); |
| 176 | + indev_drv_right.type = LV_INDEV_TYPE_KEYPAD; |
| 177 | + indev_drv_right.read_cb = keypad_read_right; |
| 178 | + |
| 179 | + static lv_indev_drv_t indev_drv_enter; |
| 180 | + lv_indev_drv_init(&indev_drv_enter); |
| 181 | + indev_drv_enter.type = LV_INDEV_TYPE_KEYPAD; |
| 182 | + indev_drv_enter.read_cb = keypad_read_enter; |
| 183 | + |
| 184 | + static lv_indev_drv_t indev_drv_back; |
| 185 | + lv_indev_drv_init(&indev_drv_back); |
| 186 | + indev_drv_back.type = LV_INDEV_TYPE_KEYPAD; |
| 187 | + indev_drv_back.read_cb = keypad_read_back; |
| 188 | + |
| 189 | + // Register the drivers in LVGL and save the created input device object |
| 190 | + lv_indev_set_group(lv_indev_drv_register(&indev_drv_left), group); |
| 191 | + lv_indev_set_group(lv_indev_drv_register(&indev_drv_right), group); |
| 192 | + lv_indev_set_group(lv_indev_drv_register(&indev_drv_enter), group); |
| 193 | + lv_indev_set_group(lv_indev_drv_register(&indev_drv_back), group); |
| 194 | +} |
| 195 | + |
| 196 | +/** |
| 197 | + * @brief cyclic function to be called to handle lvgl |
| 198 | + * This also flushes the data to the display. |
| 199 | + * |
| 200 | + */ |
| 201 | +void GuiEngine::refresh() |
| 202 | +{ |
| 203 | + lv_timer_handler(); |
| 204 | + LV_LOG_USER("Adafruit display() start"); |
| 205 | + this->display.display(); |
| 206 | + LV_LOG_USER("Adafruit display() end"); |
| 207 | +} |
| 208 | + |
| 209 | +/** |
| 210 | + * @brief function to draw a menu Screen onto the display |
| 211 | + * |
| 212 | + * @param menuList - list of items for the menu to display |
| 213 | + */ |
| 214 | +void GuiEngine::drawMenu(const MenuItemList *menuList) |
| 215 | +{ |
| 216 | + CurrentScreen = std::make_shared<ScreenMenu>(*menuList); |
| 217 | + CurrentScreen->draw(); |
| 218 | +} |
0 commit comments