|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Raphael Lehmann |
| 3 | + * |
| 4 | + * This file is part of the modm project. |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | + */ |
| 10 | +// ---------------------------------------------------------------------------- |
| 11 | + |
| 12 | +#include <modm/board.hpp> |
| 13 | +#include <modm/debug.hpp> |
| 14 | +#include <modm/processing.hpp> |
| 15 | + |
| 16 | +#include <lvgl/lvgl.h> |
| 17 | + |
| 18 | + |
| 19 | +// Set the log level |
| 20 | +#undef MODM_LOG_LEVEL |
| 21 | +#define MODM_LOG_LEVEL modm::log::DEBUG |
| 22 | + |
| 23 | +using namespace Board; |
| 24 | +using namespace modm::glcd; |
| 25 | +using namespace Board::ft6; |
| 26 | +using namespace modm::literals; |
| 27 | + |
| 28 | + |
| 29 | +static uint16_t* displayBuffer; |
| 30 | +static lv_disp_buf_t disp_buf; |
| 31 | +static lv_color_t* buf; |
| 32 | +static constexpr size_t buf_size = LV_HOR_RES_MAX * LV_VER_RES_MAX; |
| 33 | + |
| 34 | +Touch::Data touchData; |
| 35 | +Touch touch{touchData}; |
| 36 | + |
| 37 | +bool my_touchpad_read(lv_indev_drv_t*, lv_indev_data_t* data) |
| 38 | +{ |
| 39 | + RF_CALL_BLOCKING(touch.readTouches()); |
| 40 | + Touch::touch_t tp; |
| 41 | + touch.getData().getTouch(&tp, 0); |
| 42 | + // mirror and rotate correctly |
| 43 | + uint16_t x{tp.y}, y{uint16_t(480 - tp.x)}; |
| 44 | + data->state = (tp.event == Touch::Event::Contact) ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL; |
| 45 | + if(data->state == LV_INDEV_STATE_PR) { |
| 46 | + data->point.x = x; |
| 47 | + data->point.y = y; |
| 48 | + } |
| 49 | + return false; |
| 50 | +} |
| 51 | + |
| 52 | +void my_flush_cb(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p) |
| 53 | +{ |
| 54 | + for(lv_coord_t y = area->y1; y <= area->y2; y++) { |
| 55 | + std::memcpy( |
| 56 | + &displayBuffer[(800*y) + area->x1], |
| 57 | + &color_p[(y - area->y1) * (area->x2 - area->x1 + 1)], |
| 58 | + (area->x2 - area->x1 + 1) * sizeof(lv_color_t) |
| 59 | + ); |
| 60 | + } |
| 61 | + lv_disp_flush_ready(disp_drv); |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +int |
| 66 | +main() |
| 67 | +{ |
| 68 | + Board::initialize(); |
| 69 | + Board::initializeDisplay(); |
| 70 | + Board::initializeTouchscreen(); |
| 71 | + |
| 72 | + MODM_LOG_INFO << "modm LVGL example on STM32F469-Discovery board!\n\n"; |
| 73 | + |
| 74 | + RF_CALL_BLOCKING(touch.configure(Touch::InterruptMode::Trigger, 60, 60)); |
| 75 | + |
| 76 | + lv_init(); |
| 77 | + |
| 78 | + buf = new(modm::MemoryExternal) lv_color_t[buf_size]; |
| 79 | + displayBuffer = new(modm::MemoryExternal) uint16_t[buf_size]; |
| 80 | + |
| 81 | + setDisplayBuffer(displayBuffer); |
| 82 | + |
| 83 | + // Initialize the display buffer |
| 84 | + lv_disp_buf_init(&disp_buf, buf, NULL, buf_size); |
| 85 | + |
| 86 | + // Initialize the display: |
| 87 | + lv_disp_drv_t disp_drv; |
| 88 | + lv_disp_drv_init(&disp_drv); |
| 89 | + disp_drv.buffer = &disp_buf; |
| 90 | + disp_drv.flush_cb = my_flush_cb; |
| 91 | + disp_drv.hor_res = LV_HOR_RES_MAX; |
| 92 | + disp_drv.ver_res = LV_VER_RES_MAX; |
| 93 | + lv_disp_t * disp; |
| 94 | + disp = lv_disp_drv_register(&disp_drv); |
| 95 | + |
| 96 | + // Initialize touchscreen driver: |
| 97 | + lv_indev_drv_t indev_drv; |
| 98 | + lv_indev_drv_init(&indev_drv); |
| 99 | + indev_drv.type = LV_INDEV_TYPE_POINTER; |
| 100 | + indev_drv.read_cb = my_touchpad_read; |
| 101 | + lv_indev_drv_register(&indev_drv); |
| 102 | + |
| 103 | + lv_obj_t* scr = lv_disp_get_scr_act(disp); // Get the current screen |
| 104 | + |
| 105 | + lv_obj_t* labelA = lv_label_create(scr, NULL); |
| 106 | + lv_label_set_text(labelA, "Hello world!"); |
| 107 | + lv_obj_set_pos(labelA, 10, 10); |
| 108 | + lv_obj_set_size(labelA, 120, 50); |
| 109 | + |
| 110 | + lv_obj_t* btn2 = lv_btn_create(lv_scr_act(), NULL); |
| 111 | + lv_obj_set_pos(btn2, 140, 10); |
| 112 | + lv_obj_set_size(btn2, 120, 50); |
| 113 | + lv_obj_t* label2 = lv_label_create(btn2, NULL); |
| 114 | + lv_label_set_text(label2, "Button2"); |
| 115 | + lv_obj_set_event_cb(btn2, [](struct _lv_obj_t * obj, lv_event_t event) { |
| 116 | + static uint16_t btn2Counter = 0; |
| 117 | + if(event == LV_EVENT_PRESSED) { |
| 118 | + lv_label_set_text_fmt(lv_obj_get_child(obj, NULL), "Button 2: %d", ++btn2Counter); |
| 119 | + } |
| 120 | + }); |
| 121 | + |
| 122 | + uint16_t counter = 0; |
| 123 | + |
| 124 | + modm::ShortPeriodicTimer tmr{20ms}; |
| 125 | + while (true) |
| 126 | + { |
| 127 | + lv_task_handler(); |
| 128 | + |
| 129 | + if (tmr.execute()) |
| 130 | + { |
| 131 | + lv_label_set_text_fmt(labelA, "counter=%d", ++counter); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return 0; |
| 136 | +} |
0 commit comments