[Fw] Add MPU6050 as input device in lvgl.

This commit is contained in:
稚晖 2021-01-21 21:49:48 +08:00
parent 7a0a2a623f
commit 1862b0749b
19 changed files with 578 additions and 41 deletions

4
.gitignore vendored
View File

@ -9,4 +9,6 @@
2.Firmware/HoloCubic_fw/.vs
2.Firmware/HoloCubic_fw/__vm
2.Firmware/HoloCubic_fw/Release
3.Software/vs2019_proj/x64
3.Software/vs2019_proj/x64
3.Software/vs2019_proj/.vs
3.Software/vs2019_proj/.vs/lvgl_similator/v16/.suo

View File

@ -2,7 +2,8 @@
#include "imu.h"
#include "rgb_led.h"
#include "ambient.h"
#include "lv_examples/lv_examples.h"
#include "lv_port_indev.h"
#include "lv_demo_encoder.h"
Display screen;
IMU mpu;
@ -15,15 +16,18 @@ void setup()
screen.init();
screen.setBackLight(0.2);
lv_port_indev_init();
mpu.init();
rgb.init();
rgb.setBrightness(0.3).setRGB(0, 122, 204);
rgb.setBrightness(0.1).setRGB(0, 122, 204);
ambLight.init(ONE_TIME_L_RESOLUTION_MODE);
lv_demo_benchmark();
//lv_demo_benchmark();
lv_demo_encoder();
}
int i = 0;
@ -32,10 +36,10 @@ void loop()
// run this as often as possible ¡ý
screen.routine();
mpu.update();
mpu.update(200);
rgb.setBrightness(ambLight.getLux() / 500.0);
Serial.println(ambLight.getLux());
//Serial.println(ambLight.getLux());
delay(10);
}

View File

@ -1,9 +1,9 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30225.117
VisualStudioVersion = 16.0.30413.136
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "HoloCubic_fw", "HoloCubic_fw.vcxproj", "{25A27E44-D676-445B-87FB-AD9830B60FEA}"
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "HoloCubic_fw", "HoloCubic_fw.vcxproj", "{C5F80730-F44F-4478-BDAE-6634EFC2CA88}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -11,15 +11,15 @@ Global
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{25A27E44-D676-445B-87FB-AD9830B60FEA}.Debug|x86.ActiveCfg = Debug|Win32
{25A27E44-D676-445B-87FB-AD9830B60FEA}.Debug|x86.Build.0 = Debug|Win32
{25A27E44-D676-445B-87FB-AD9830B60FEA}.Release|x86.ActiveCfg = Release|Win32
{25A27E44-D676-445B-87FB-AD9830B60FEA}.Release|x86.Build.0 = Release|Win32
{C5F80730-F44F-4478-BDAE-6634EFC2CA88}.Debug|x86.ActiveCfg = Debug|Win32
{C5F80730-F44F-4478-BDAE-6634EFC2CA88}.Debug|x86.Build.0 = Debug|Win32
{C5F80730-F44F-4478-BDAE-6634EFC2CA88}.Release|x86.ActiveCfg = Release|Win32
{C5F80730-F44F-4478-BDAE-6634EFC2CA88}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {792E64C3-4250-484E-87AA-21824A44E240}
SolutionGuid = {09150B20-AE1A-4A10-B59E-929DA0AC70FF}
EndGlobalSection
EndGlobal

File diff suppressed because one or more lines are too long

View File

@ -21,31 +21,43 @@
<ClInclude Include="__vm\.HoloCubic_fw.vsarduino.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ambient.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="display.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="imu.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="lv_port_indev.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="rgb_led.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ambient.h">
<ClInclude Include="lv_demo_encoder.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="display.cpp">
<Filter>Resource Files</Filter>
</ClCompile>
<ClCompile Include="imu.cpp">
<Filter>Resource Files</Filter>
</ClCompile>
<ClCompile Include="rgb_led.cpp">
<Filter>Resource Files</Filter>
<ClCompile Include="lv_port_indev.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ambient.cpp">
<Filter>Resource Files</Filter>
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="display.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="imu.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="rgb_led.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="lv_demo_encoder.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -1,3 +1,6 @@
#ifndef AMBIENT_H
#define AMBIENT_H
#include <Wire.h>
#define AMB_I2C_SDA 32
@ -19,4 +22,6 @@ private:
public:
void init(int mode);
unsigned int getLux();
};
};
#endif

View File

@ -9,6 +9,7 @@ TFT_eSPI tft = TFT_eSPI();
static lv_disp_buf_t disp_buf;
static lv_color_t buf[LV_HOR_RES_MAX * 10];
void my_print(lv_log_level_t level, const char* file, uint32_t line, const char* fun, const char* dsc)
{
Serial.printf("%s@%d %s->%s\r\n", file, line, fun, dsc);

View File

@ -1,3 +1,6 @@
#ifndef DISPLAY_H
#define DISPLAY_H
#include <lvgl.h>
#define LCD_BL_PIN 5
@ -13,4 +16,6 @@ public:
void init();
void routine();
void setBackLight(float);
};
};
#endif

View File

@ -1,6 +1,9 @@
#include "imu.h"
int flag;
extern Pixel rgb;
void IMU::init()
{
Wire.begin(IMU_I2C_SDA, IMU_I2C_SCL);
@ -9,9 +12,45 @@ void IMU::init()
imu.initialize();
}
void IMU::update()
void IMU::update(int interval)
{
imu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
Serial.print(gx);
Serial.print(" ");
Serial.print(gy);
Serial.print(" ");
Serial.print(gz);
Serial.println(" ");
if (millis() - last_update_time > interval)
{
if (ay > 3000 && flag)
{
encoder_diff--;
flag = 0;
}
else if (ay < -3000 && flag)
{
encoder_diff++;
flag = 0;
}
else
{
flag = 1;
}
if (ax > 10000)
{
encoder_state = LV_INDEV_STATE_PR;
}
else
{
encoder_state = LV_INDEV_STATE_REL;
}
last_update_time = millis();
}
}
int16_t IMU::getAccelX()

View File

@ -1,9 +1,19 @@
#ifndef IMU_H
#define IMU_H
#include <I2Cdev.h>
#include <MPU6050.h>
#include "lv_port_indev.h"
#include "rgb_led.h"
#define IMU_I2C_SDA 32
#define IMU_I2C_SCL 33
extern int32_t encoder_diff;
extern lv_indev_state_t encoder_state;
class IMU
{
private:
@ -11,10 +21,12 @@ private:
int16_t ax, ay, az;
int16_t gx, gy, gz;
long last_update_time;
public:
void init();
void update();
void update(int interval);
int16_t getAccelX();
int16_t getAccelY();
@ -24,4 +36,6 @@ public:
int16_t getGyroY();
int16_t getGyroZ();
};
};
#endif

View File

@ -0,0 +1,282 @@
/**
* @file lv_demo_keypad_encoder.c
*
*/
/*********************
* INCLUDES
*********************/
#include "lv_demo_encoder.h"
#include "lv_port_indev.h"
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static void selectors_create(lv_obj_t * parent);
static void text_input_create(lv_obj_t * parent);
static void msgbox_create(void);
static void focus_cb(lv_group_t * g);
static void msgbox_event_cb(lv_obj_t * msgbox, lv_event_t e);
static void tv_event_cb(lv_obj_t * ta, lv_event_t e);
static void ta_event_cb(lv_obj_t * ta, lv_event_t e);
static void kb_event_cb(lv_obj_t * kb, lv_event_t e);
/**********************
* STATIC VARIABLES
**********************/
static lv_group_t* g;
static lv_obj_t * tv;
static lv_obj_t * t1;
static lv_obj_t * t2;
static lv_obj_t * t3;
struct {
lv_obj_t * btn;
lv_obj_t * cb;
lv_obj_t * slider;
lv_obj_t * sw;
lv_obj_t * spinbox;
lv_obj_t * dropdown;
lv_obj_t * roller;
lv_obj_t * list;
}selector_objs;
struct {
lv_obj_t * ta1;
lv_obj_t * ta2;
lv_obj_t * kb;
}textinput_objs;
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
LV_EVENT_CB_DECLARE(dd_enc)
{
if(e == LV_EVENT_VALUE_CHANGED) {
/*printf("chg\n");*/
}
}
void lv_demo_encoder(void)
{
g = lv_group_create();
lv_group_set_focus_cb(g, focus_cb);
lv_indev_set_group(indev_encoder, g);
tv = lv_tabview_create(lv_scr_act(), NULL);
lv_obj_set_event_cb(tv, tv_event_cb);
t1 = lv_tabview_add_tab(tv, "Selectors");
t2 = lv_tabview_add_tab(tv, "Text input");
lv_group_add_obj(g, tv);
selectors_create(t1);
text_input_create(t2);
msgbox_create();
}
/**********************
* STATIC FUNCTIONS
**********************/
static void selectors_create(lv_obj_t * parent)
{
lv_page_set_scrl_layout(parent, LV_LAYOUT_COLUMN_MID);
selector_objs.btn = lv_btn_create(parent, NULL);
lv_obj_t * label = lv_label_create(selector_objs.btn, NULL);
lv_label_set_text(label, "Button");
selector_objs.cb = lv_checkbox_create(parent, NULL);
selector_objs.slider = lv_slider_create(parent, NULL);
lv_slider_set_range(selector_objs.slider, 0, 10);
selector_objs.sw = lv_switch_create(parent, NULL);
selector_objs.spinbox = lv_spinbox_create(parent, NULL);
selector_objs.dropdown = lv_dropdown_create(parent, NULL);
lv_obj_set_event_cb(selector_objs.dropdown, dd_enc);
selector_objs.roller = lv_roller_create(parent, NULL);
selector_objs.list = lv_list_create(parent, NULL);
if(lv_obj_get_height(selector_objs.list) > lv_page_get_height_fit(parent)) {
lv_obj_set_height(selector_objs.list, lv_page_get_height_fit(parent));
}
lv_list_add_btn(selector_objs.list, LV_SYMBOL_OK, "Apply");
lv_list_add_btn(selector_objs.list, LV_SYMBOL_CLOSE, "Close");
lv_list_add_btn(selector_objs.list, LV_SYMBOL_EYE_OPEN, "Show");
lv_list_add_btn(selector_objs.list, LV_SYMBOL_EYE_CLOSE, "Hide");
lv_list_add_btn(selector_objs.list, LV_SYMBOL_TRASH, "Delete");
lv_list_add_btn(selector_objs.list, LV_SYMBOL_COPY, "Copy");
lv_list_add_btn(selector_objs.list, LV_SYMBOL_PASTE, "Paste");
}
static void text_input_create(lv_obj_t * parent)
{
textinput_objs.ta1 = lv_textarea_create(parent, NULL);
lv_obj_set_event_cb(textinput_objs.ta1, ta_event_cb);
lv_obj_align(textinput_objs.ta1, NULL, LV_ALIGN_IN_TOP_MID, 0, LV_DPI / 20);
lv_textarea_set_one_line(textinput_objs.ta1, true);
lv_textarea_set_cursor_hidden(textinput_objs.ta1, true);
lv_textarea_set_placeholder_text(textinput_objs.ta1, "Type something");
lv_textarea_set_text(textinput_objs.ta1, "");
textinput_objs.ta2 = lv_textarea_create(parent, textinput_objs.ta1);
lv_obj_align(textinput_objs.ta2, textinput_objs.ta1, LV_ALIGN_OUT_BOTTOM_MID, 0, LV_DPI / 20);
textinput_objs.kb = NULL;
}
static void msgbox_create(void)
{
lv_obj_t * mbox = lv_msgbox_create(lv_layer_top(), NULL);
lv_msgbox_set_text(mbox, "Welcome to the keyboard and encoder demo");
lv_obj_set_event_cb(mbox, msgbox_event_cb);
lv_group_add_obj(g, mbox);
lv_group_focus_obj(mbox);
lv_group_set_editing(g, true);
lv_group_focus_freeze(g, true);
static const char * btns[] = {"Ok", "Cancel", ""};
lv_msgbox_add_btns(mbox, btns);
lv_obj_align(mbox, NULL, LV_ALIGN_CENTER, 0, 0);
lv_obj_set_style_local_bg_opa(lv_layer_top(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_70);
lv_obj_set_style_local_bg_color(lv_layer_top(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
lv_obj_set_click(lv_layer_top(), true);
}
static void msgbox_event_cb(lv_obj_t * msgbox, lv_event_t e)
{
if(e == LV_EVENT_CLICKED) {
uint16_t b = lv_msgbox_get_active_btn(msgbox);
if(b == 0 || b == 1) {
lv_obj_del(msgbox);
lv_obj_reset_style_list(lv_layer_top(), LV_OBJ_PART_MAIN);
lv_obj_set_click(lv_layer_top(), false);
lv_event_send(tv, LV_EVENT_REFRESH, NULL);
}
}
}
static void focus_cb(lv_group_t * group)
{
lv_obj_t * obj = lv_group_get_focused(group);
if(obj != tv) {
uint16_t tab = lv_tabview_get_tab_act(tv);
switch(tab) {
case 0:
lv_page_focus(t1, obj, LV_ANIM_ON);
break;
case 1:
lv_page_focus(t2, obj, LV_ANIM_ON);
break;
case 2:
lv_page_focus(t3, obj, LV_ANIM_ON);
break;
}
}
}
static void tv_event_cb(lv_obj_t * ta, lv_event_t e)
{
if(e == LV_EVENT_VALUE_CHANGED || e == LV_EVENT_REFRESH) {
lv_group_remove_all_objs(g);
uint16_t tab = lv_tabview_get_tab_act(tv);
size_t size = 0;
lv_obj_t ** objs = NULL;
if(tab == 0) {
size = sizeof(selector_objs);
objs = (lv_obj_t**) &selector_objs;
}
else if(tab == 1) {
size = sizeof(textinput_objs);
objs = (lv_obj_t**) &textinput_objs;
}
lv_group_add_obj(g, tv);
uint32_t i;
for(i = 0; i < size / sizeof(lv_obj_t *); i++) {
if(objs[i] == NULL) continue;
lv_group_add_obj(g, objs[i]);
}
}
}
static void ta_event_cb(lv_obj_t * ta, lv_event_t e)
{
/*Create a virtual keyboard for the encoders*/
lv_indev_t * indev = lv_indev_get_act();
if(indev == NULL) return;
lv_indev_type_t indev_type = lv_indev_get_type(indev);
if(e == LV_EVENT_FOCUSED) {
lv_textarea_set_cursor_hidden(ta, false);
if(lv_group_get_editing(g)) {
if(textinput_objs.kb == NULL) {
textinput_objs.kb = lv_keyboard_create(lv_scr_act(), NULL);
lv_group_add_obj(g, textinput_objs.kb);
lv_obj_set_event_cb(textinput_objs.kb, kb_event_cb);
lv_obj_set_height(tv, LV_VER_RES - lv_obj_get_height(textinput_objs.kb));
}
lv_keyboard_set_textarea(textinput_objs.kb, ta);
lv_group_focus_obj(textinput_objs.kb);
lv_group_set_editing(g, true);
lv_page_focus(t2, lv_textarea_get_label(ta), LV_ANIM_ON);
}
}
else if(e == LV_EVENT_DEFOCUSED) {
if(indev_type == LV_INDEV_TYPE_ENCODER) {
if(textinput_objs.kb == NULL) {
lv_textarea_set_cursor_hidden(ta, true);
}
} else {
lv_textarea_set_cursor_hidden(ta, true);
}
}
}
static void kb_event_cb(lv_obj_t * kb, lv_event_t e)
{
lv_keyboard_def_event_cb(kb, e);
if(e == LV_EVENT_APPLY || e == LV_EVENT_CANCEL) {
lv_group_focus_obj(lv_keyboard_get_textarea(kb));
lv_obj_del(kb);
textinput_objs.kb = NULL;
lv_obj_set_height(tv, LV_VER_RES);
}
}

View File

@ -0,0 +1,38 @@
/**
* @file lv_demo_keypad_encoder.h
*
*/
#ifndef LV_DEMO_KEYPAD_ENCODER_H
#define LV_DEMO_KEYPAD_ENCODER_H
#ifdef __cplusplus
extern "C" {
#endif
/*********************
* INCLUDES
*********************/
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* GLOBAL PROTOTYPES
**********************/
void lv_demo_encoder(void);
/**********************
* MACROS
**********************/
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /*LV_DEMO_KEYPAD_ENCODER_H*/

View File

@ -0,0 +1,98 @@
/**
* @file lv_port_indev_templ.c
*
*/
/*Copy this file as "lv_port_indev.c" and set this value to "1" to enable content*/
/*********************
* INCLUDES
*********************/
#include "lv_port_indev.h"
static void encoder_init(void);
static bool encoder_read(lv_indev_drv_t* indev_drv, lv_indev_data_t* data);
static void encoder_handler(void);
lv_indev_t* indev_encoder;
int32_t encoder_diff;
lv_indev_state_t encoder_state;
void lv_port_indev_init(void)
{
/* Here you will find example implementation of input devices supported by LittelvGL:
* - Touchpad
* - Mouse (with cursor support)
* - Keypad (supports GUI usage only with key)
* - Encoder (supports GUI usage only with: left, right, push)
* - Button (external buttons to press points on the screen)
*
* The `..._read()` function are only examples.
* You should shape them according to your hardware
*/
lv_indev_drv_t indev_drv;
/*------------------
* Encoder
* -----------------*/
/*Initialize your encoder if you have*/
encoder_init();
/*Register a encoder input device*/
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_ENCODER;
indev_drv.read_cb = encoder_read;
indev_encoder = lv_indev_drv_register(&indev_drv);
/* Later you should create group(s) with `lv_group_t * group = lv_group_create()`,
* add objects to the group with `lv_group_add_obj(group, obj)`
* and assign this input device to group to navigate in it:
* `lv_indev_set_group(indev_encoder, group);` */
}
/**********************
* STATIC FUNCTIONS
**********************/
/*------------------
* Encoder
* -----------------*/
/* Initialize your keypad */
static void encoder_init(void)
{
/*Your code comes here*/
}
/* Will be called by the library to read the encoder */
static bool encoder_read(lv_indev_drv_t* indev_drv, lv_indev_data_t* data)
{
data->enc_diff = encoder_diff;
data->state = encoder_state;
encoder_diff = 0;
/*Return `false` because we are not buffering and no more data to read*/
return false;
}
/*Call this function in an interrupt to process encoder events (turn, press)*/
static void encoder_handler(void)
{
/*Your code comes here*/
encoder_diff += 0;
encoder_state = LV_INDEV_STATE_REL;
}

View File

@ -0,0 +1,33 @@
/**
* @file lv_port_indev_templ.h
*
*/
/*Copy this file as "lv_port_indev.h" and set this value to "1" to enable content*/
#ifndef LV_PORT_INDEV_TEMPL_H
#define LV_PORT_INDEV_TEMPL_H
#ifdef __cplusplus
extern "C" {
#endif
/*********************
* INCLUDES
*********************/
#include "lvgl.h"
extern lv_indev_t* indev_encoder;
void lv_port_indev_init(void);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /*LV_PORT_INDEV_TEMPL_H*/

View File

@ -1,3 +1,6 @@
#ifndef RGB_H
#define RGB_H
#include <FastLED.h>
#define RGB_LED_NUM 1
@ -14,4 +17,6 @@ public:
Pixel& setRGB(int r, int g, int b);
Pixel& setBrightness(float duty);
};
};
#endif

View File

@ -60,8 +60,8 @@ int main(int argc, char** argv)
*/
//lv_demo_widgets();
lv_demo_benchmark();
//lv_demo_keypad_encoder();
//lv_demo_benchmark();
lv_demo_keypad_encoder();
//lv_demo_printer();
//lv_demo_stress();
//lv_ex_get_started_1();