Jump to content

how to implement a key driver ?


yaoyutaoTom

Recommended Posts

I want to implement a key driver. And I see the example in the gdrivers.

GINPUT_TOGGLE_DECLARE_STRUCTURE();

void ginput_lld_toggle_init(const GToggleConfig *ptc) {
	palSetGroupMode(((IOBus *)ptc->id)->portid, ptc->mask, 0, ptc->mode);
}

unsigned ginput_lld_toggle_getbits(const GToggleConfig *ptc) {
	return palReadBus((IOBus *)ptc->id);
}
// Describes how the toggle bits are obtained
typedef struct GToggleConfig_t {
	void		*id;
	unsigned	mask;
	unsigned	invert;
	unsigned	mode;
} GToggleConfig;

But I have no idea how to write the code, is there a example ? what's the mean of invert ?

I use in bare metal mode.  no palSetGroupMode() function, no IOBus * type

Link to comment
Share on other sites

Hello @yaoyutaoTom,

There's currently just one example and that's the one that you mentioned/showed. We'd be happy to include your code as an example once you have it working :)

 

2 hours ago, yaoyutaoTom said:

what's the mean of invert ?

This is to simply invert the input. Imagine you want to have a hardware button to select an item in a list widget. You want the item to be selected whenever you press the button. You don't want to bother how the button is connected to the input pin of your microcontroller (active-low or active-high). Using the "invert", both cases can be used with the same high-level user code.
I hope that makes it more clear. Please don't hesitate to ask if you didn't understand. We're happy to elaborate.

 

2 hours ago, yaoyutaoTom said:

I use in bare metal mode.  no palSetGroupMode() function, no IOBus * type

These all come from the ChibiOS/HAL. That is something you don't have. All you have to do in those two functions is initializing the GPIO so it becomes a digital input and then reading the digital input (whether it's low or high). That depends what underlying system you're using.
For example, if you're using the CubeHAL for the STM32 you'd simply call HAL_GPIO_Init() and HAL_GPIO_ReadPin() instead of palSetGroupMode() and palReadBus().

Link to comment
Share on other sites

Now, I have completed a key driver,

#define GINPUT_TOGGLE_NUM_PORTS			3			// The total number of toggle inputs
#define GINPUT_TOGGLE_CONFIG_ENTRIES	1

#define GINPUT_TOGGLE_DECLARE_STRUCTURE()											\
const GToggleConfig GInputToggleConfigTable[GINPUT_TOGGLE_CONFIG_ENTRIES] = {	\
		{GPIOC,								\
			GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9,											\
			GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9,											\
			GPIO_Mode_IN}													\
	}

	
GINPUT_TOGGLE_DECLARE_STRUCTURE();

void ginput_lld_toggle_init(const GToggleConfig *ptc) 
{
	key_init();
}

unsigned ginput_lld_toggle_getbits(const GToggleConfig *ptc) {
	return GPIO_ReadInputData(GPIOC);
}

if the key GPIOC.7 is pressed, how do I check the key event in a win ?

Link to comment
Share on other sites

All widgets have the possibility to submit functions that handle toggle inputs:

    #if GINPUT_NEED_TOGGLE
        struct {
            uint16_t                toggleroles;                                                        /**< The roles supported for toggles (0->toggleroles-1) */
            void (*ToggleAssign)    (GWidgetObject *gw, uint16_t role, uint16_t instance);              /**< Assign a toggle to a role (optional) */
            uint16_t (*ToggleGet)   (GWidgetObject *gw, uint16_t role);                                 /**< Return the instance for a particular role (optional) */
            void (*ToggleOff)       (GWidgetObject *gw, uint16_t role);                                 /**< Process toggle off events (optional) */
            void (*ToggleOn)        (GWidgetObject *gw, uint16_t role);                                 /**< Process toggle on events (optional) */
        };
    #endif

This means that each widget itself decides what it does with a toggle input. For example, the list widget allows to scroll through the list and to select an item in the list:

#if GINPUT_NEED_TOGGLE
    {
        2,          // two toggle roles
        ListToggleAssign,   // Assign toggles
        ListToggleGet,      // Get toggles
        0,
        ListToggleOn,       // Process toggle on event
    },
#endif

You use the gwinAttachToggle() function in your application to attach a toggle input to a widget. Note that you can re-assign those at any time.

I hope that helps.

Link to comment
Share on other sites

  • 4 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...