/ src / input / input_keypad.cpp
input_keypad.cpp
 1  #include "input_keypad.h"
 2  
 3  #include "input_log_module.ii"
 4  
 5  namespace input
 6  {
 7  	bool Keypad::ReadCallback(lv_indev_drv_t* driver, lv_indev_data_t*data)
 8  	{
 9  		Keypad* instance = (Keypad*)driver->user_data;
10  
11  		if (instance->lastKeyPressed == LV_KEY_NONE)
12  		{
13  			// we don't have a key pressed, scan for a new one.
14  			lv_key_t pressed = GetPressedKey();
15  			if (pressed != LV_KEY_NONE)
16  			{
17  				// a new key was pressed!
18  				data->key = pressed;
19  				data->state = LV_INDEV_STATE_PR;
20  				instance->lastKeyPressed = pressed;
21  			}
22  			else
23  			{
24  				// no key pressed.
25  				data->state = LV_INDEV_STATE_REL;
26  			}
27  		}
28  		else
29  		{
30  			// we are currently pressing a key, return it's state.
31  			if (KeyIsPressed(instance->lastKeyPressed))
32  			{
33  				// key is continued to be pressed.
34  				data->state = LV_INDEV_STATE_PR;
35  			}
36  			else
37  			{
38  				// key is released.
39  				data->state = LV_INDEV_STATE_REL;
40  
41  				// we let go of the key, so now no key is pressed,
42  				// we can go back to scanning mode
43  				instance->lastKeyPressed = LV_KEY_NONE;
44  			}
45  		}
46  
47  		// we aren't doing any buffering, so no need to return true.
48  		return false;
49  	}
50  
51  	Keypad::Keypad()
52  	{
53  		input::InitializeHal();
54  	}
55  
56  	Keypad::~Keypad()
57  	{
58  
59  	}
60  
61  	lv_group_t* Keypad::GetInputGroup()
62  	{
63  		// create the device on the fly when someone first tries to get the group.
64  		// the framework should be ready by then.
65  		if (inputGroup == nullptr)
66  		{
67  			CreateDevice();
68  		}
69  
70  		return inputGroup;
71  	}
72  
73  	void Keypad::CreateDevice()
74  	{
75  		// already created.
76  		if (keypadDev != nullptr)
77  		{
78  			return;
79  		}
80  
81  		if (lv_disp_get_default() == nullptr)
82  		{
83  			NRF_LOG_ERROR("Cannot create Keypad device: lv display must be initialized and assigned first.");
84  			return;
85  		}
86  
87  		lv_indev_drv_t inputDeviceDriver;
88  		lv_indev_drv_init(&inputDeviceDriver);
89  		inputDeviceDriver.type = LV_INDEV_TYPE_KEYPAD;
90  		inputDeviceDriver.read_cb = Keypad::ReadCallback;
91  		inputDeviceDriver.user_data = this;
92  		keypadDev = lv_indev_drv_register(&inputDeviceDriver);
93  
94  		inputGroup = lv_group_create();
95  		lv_indev_set_group(keypadDev, inputGroup);
96  	}
97  }