Skip to content

Commit

Permalink
[examples] Add LVGL example for Nucleo-L452RE with Ili9341
Browse files Browse the repository at this point in the history
  • Loading branch information
rleh committed Apr 3, 2021
1 parent c8d6378 commit 7c31019
Show file tree
Hide file tree
Showing 2 changed files with 220 additions and 0 deletions.
198 changes: 198 additions & 0 deletions examples/nucleo_l452re/lvgl/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/*
* Copyright (c) 2021, Raphael Lehmann
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>
#include <modm/debug/logger.hpp>
#include <modm/driver/display/ili9341_spi.hpp>
#include <modm/driver/touch/touch2046.hpp>

#include <lvgl/lvgl.h>


// Set the log level
#undef MODM_LOG_LEVEL
#define MODM_LOG_LEVEL modm::log::DEBUG

using namespace Board;
using namespace modm::literals;

static modm::Abandonment
test_assertion_handler(const modm::AssertionInfo &info)
{
MODM_LOG_ERROR << "AssertionInfo.name: " << info.name << "\n";
MODM_LOG_ERROR << "AssertionInfo.context: " << info.context << "\n";
MODM_LOG_ERROR << "AssertionInfo.behavior: " << info.behavior << "\n";
MODM_LOG_ERROR << modm::endl;
return modm::Abandonment::DontCare;
}
MODM_ASSERTION_HANDLER(test_assertion_handler);


namespace tft
{
using DmaRx = Dma1::Channel2;
using DmaTx = Dma1::Channel3;
using Spi = SpiMaster1_Dma<DmaRx, DmaTx>;
//using Spi = SpiMaster1;
using Cs = modm::platform::GpioC8;
using Sck = modm::platform::GpioA5;
using Miso = modm::platform::GpioA6;
using Mosi = modm::platform::GpioA7;
using DataCommands = modm::platform::GpioC5;
using Reset = modm::platform::GpioC6;
using Backlight = modm::platform::GpioC9;
}

modm::Ili9341Spi<
tft::Spi,
tft::Cs,
tft::DataCommands,
tft::Reset,
tft::Backlight
> tftController;

namespace touch
{
using Spi = SpiMaster2;
using Cs = modm::platform::GpioB3;
using Sck = modm::platform::GpioB13;
using Miso = modm::platform::GpioB14;
using Mosi = modm::platform::GpioB15;
//using Interrupt = modm::platform::GpioA10;
}

modm::Touch2046<touch::Spi, touch::Cs> touchController;


static lv_disp_buf_t disp_buf;
static constexpr size_t buf_size = LV_HOR_RES_MAX * LV_VER_RES_MAX / 8;
static lv_color_t buf[buf_size];

bool my_touchpad_read(lv_indev_drv_t* indev, lv_indev_data_t* data)
{
data->state = RF_CALL_BLOCKING(touchController.isTouched()) ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
if(data->state == LV_INDEV_STATE_PR) {
auto xy = RF_CALL_BLOCKING(touchController.getTouchPosition());
data->point.x = std::get<0>(xy);
data->point.y = std::get<1>(xy);
}
return false;
}

void my_flush_cb(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p)
{
tftController.drawRaw(
{area->x1, area->y1},
(area->x2 - area->x1 +1),
(area->y2 - area->y1 + 1),
(modm::glcd::Color*)color_p);
lv_disp_flush_ready(disp_drv);
}

static uint16_t btn2Counter = 0;

int
main()
{
Board::initialize();
Dma1::enable();

tft::Spi::connect<
tft::Sck::Sck,
tft::Miso::Miso,
tft::Mosi::Mosi>();
tft::Spi::initialize<SystemClock, 40_MHz>();
tftController.initialize();
tftController.enableBacklight(true);

touch::Spi::connect<
touch::Sck::Sck,
touch::Miso::Miso,
touch::Mosi::Mosi>();
touch::Spi::initialize<SystemClock, 2500_kHz>();
modm::touch2046::Calibration cal{
.OffsetX = -11,
.OffsetY = 335,
.FactorX = 22018,
.FactorY = -29358,
.MaxX = 240,
.MaxY = 320,
.ThresholdZ = 500,
};
touchController.setCalibration(cal);

MODM_LOG_INFO << "modm LVGL example on Nucleo-L452RE board!\n\n";

if (FaultReporter::hasReport())
{
MODM_LOG_ERROR << "\n\nHardFault! Copy the data into a 'coredump.txt' file, ";
MODM_LOG_ERROR << "then execute\n\n\tscons postmortem firmware=" << modm::hex;
for (const auto data : FaultReporter::buildId()) MODM_LOG_ERROR << data;
MODM_LOG_ERROR << "\n\n";
for (const auto data : FaultReporter())
MODM_LOG_ERROR << modm::hex << data << modm::flush;
MODM_LOG_ERROR << "\n\n\n" << modm::flush;
FaultReporter::clear();
while (1) ;
}

lv_init();

// Initialize the display buffer
lv_disp_buf_init(&disp_buf, buf, NULL, buf_size);

// Initialize the display:
lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.buffer = &disp_buf;
disp_drv.flush_cb = my_flush_cb;
disp_drv.hor_res = LV_HOR_RES_MAX;
disp_drv.ver_res = LV_VER_RES_MAX;
lv_disp_t * disp;
disp = lv_disp_drv_register(&disp_drv);

// Initialize touchscreen driver:
lv_indev_drv_t indev_drv;
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = my_touchpad_read;
lv_indev_drv_register(&indev_drv);

lv_obj_t* scr = lv_disp_get_scr_act(disp); // Get the current screen

lv_obj_t* labelA = lv_label_create(scr, NULL);
lv_label_set_text(labelA, "Hello world!");
lv_obj_set_pos(labelA, 60, 10);
lv_obj_set_size(labelA, 120, 50);

lv_obj_t* btn2 = lv_btn_create(lv_scr_act(), NULL);
lv_obj_set_pos(btn2, 60, 135);
lv_obj_set_size(btn2, 120, 50);
lv_obj_t* label2 = lv_label_create(btn2, NULL);
lv_label_set_text(label2, "Button2");
lv_obj_set_event_cb(btn2, [](struct _lv_obj_t * obj, lv_event_t event) {
if(event == LV_EVENT_PRESSED) {
lv_label_set_text_fmt(lv_obj_get_child(obj, NULL), "Button 2: %d", ++btn2Counter);
}
});

uint16_t counter = 0;

while (true)
{
lv_label_set_text_fmt(labelA, "counter=%d", ++counter);
lv_tick_inc(10);
lv_task_handler();
modm::delay(10ms);
}

return 0;
}
22 changes: 22 additions & 0 deletions examples/nucleo_l452re/lvgl/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<library>
<extends>modm:nucleo-l452re</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo_l452re/lvgl</option>
<option name="modm:lvgl:resolution_horizontal_max">240</option>
<option name="modm:lvgl:resolution_vertical_max">320</option>
<option name="modm:lvgl:color_depth">RGB565</option>
<option name="modm:lvgl:dpi">200</option>

</options>
<modules>
<module>modm:platform:fault</module>
<module>modm:platform:heap</module>
<module>modm:build:scons</module>
<module>modm:driver:ili9341</module>
<module>modm:driver:touch2046</module>
<module>modm:platform:spi:1</module>
<module>modm:platform:spi:2</module>
<module>modm:platform:dma</module>
<module>modm:lvgl</module>
</modules>
</library>

0 comments on commit 7c31019

Please sign in to comment.