cancel
Showing results for 
Search instead for 
Did you mean: 

How to interface multiple buttons (custom keypad with 24 keys) with touchGFX on a STM32f767IG custom board?

NMung.1
Associate II

Hi, ST community...My name is Nday Mungala (Msc Mechatronics) and I've been building for almost 3 months a monitoring terminal system for multiple belt conveyor. since it destine to harsh environment, most of the entries are from the keypad panel. my question is how to get event triggers for multiple buttons and how to display keys alpha numeric keys from the keypad to the screen (LTDC 7")?

I would be glad to hear from you.

6 REPLIES 6
Alexandre RENOUX
Principal

Hello,

For the multiple physical buttons implementation, you need to create a custom ButtonController class. Here is an example :

STM32ButtonController.cpp

#include <STM32ButtonController.hpp>
extern "C"
{
  #include "main.h"
}
 
void STM32ButtonController::init()
{
    /**
     * Initialize button controller and driver
     *
     */
    previousState = 0;
}
 
bool STM32ButtonController::sample(uint8_t& key)
{
    uint8_t state = getKeyState();
    if (state == previousState)
    {
      return false;
    }
    previousState = state;
    if (state != 0)
    {
        key = state;
        return true;
    }
    return false;
}
 
uint8_t STM32ButtonController::getKeyState(void)
{
  if (HAL_GPIO_ReadPin(BUTTON_CENTER_GPIO_Port, BUTTON_CENTER_Pin) == GPIO_PIN_RESET)
      return '5';
  if (HAL_GPIO_ReadPin(BUTTON_CENTER_GPIO_Port, BUTTON_LEFT_Pin) == GPIO_PIN_RESET)
      return '4';
  if (HAL_GPIO_ReadPin(BUTTON_CENTER_GPIO_Port, BUTTON_RIGHT_Pin) == GPIO_PIN_RESET)
      return '6';
  if (HAL_GPIO_ReadPin(BUTTON_CENTER_GPIO_Port, BUTTON_UP_Pin) == GPIO_PIN_RESET)
      return '8';
  if (HAL_GPIO_ReadPin(BUTTON_CENTER_GPIO_Port, BUTTON_DOWN_Pin) == GPIO_PIN_RESET)
      return '2';
 
  return 0;
}

STM32ButtonController.hpp

#ifndef STM32BUTTONCONTROLLER_HPP
#define STM32BUTTONCONTROLLER_HPP
 
#include <platform/driver/button/ButtonController.hpp>
 
/**
 * @class STM32ButtonController
 *
 * @brief This class specializes ButtonController Interface.
 *
 * @sa touchgfx::ButtonController
 */
 
class STM32ButtonController : public touchgfx::ButtonController
{
public:
 
    STM32ButtonController() {}
 
    /**
      * @fn virtual void STM32ButtonController::init() = 0;
      *
      * @brief Initializes button controller.
      *
      *        Initializes button controller.
      */
    virtual void init();
 
    /**
    * @fn virtual bool STM32ButtonController::sample(uint8_t& key);
    *
    * @brief Checks whether the touch screen is being touched, and if so, what coordinates.
    *
    *        Checks whether the touch screen is being touched, and if so, what coordinates.
    *
    * @param [out] key The index of the key pressed
    *
    * @return True if a button has been pressed, otherwise false.
    */
    virtual bool sample(uint8_t& key);
 
private:
    uint8_t getKeyState(void);
 
    uint8_t previousState;
};
 
#endif

Then you have to tell TouchGFX to use it. For this you write the following in TouchGFXHAL.cpp

#include <STM32ButtonController.hpp>
 
//...
 
static STM32ButtonController bc;
 
//...
 
void TouchGFXHAL::initialize()
{
 
    /* Initialize TouchGFX Engine */
    TouchGFXGeneratedHAL::initialize();
 
    setButtonController(&bc);
 
}
 

As for the creation of a keypad in the UI, you can get inspiration from the example Keyboard Example available in TouchGFX Designer.

The main files you would need to change are :

  • KeyboardLayout.hpp
  • KeyboardKeyMapping.hpp
  • CustomKeyboard.hpp/.cpp

/Alexandre

NMung.1
Associate II

Merci Beaucoup....I am going to implement it right now; then give you the feedback on how it performs. Yet the I do have another question Sir. Can I add libraries via the extern "C"? for example my custom alphanumeric keypad library....

extern "C"
{
  #include "main.h"
// can I add my keypad custom library
}

extern "C" is used for C code you want to include in c++ files.

main.h is C programming, this is why we wrap it with extern "C"

On the other hand, your custom keypad should be c++ code so you don't need to add extern "C"

/Alexandre

NMung.1
Associate II

thank you very much Alexandre RENOUX

I do appreciate your help

NMung.1
Associate II
..\Src\STM32ButtonController.cpp(35): error:  #20: identifier "KEY_2_PIN" is undefined
    if (HAL_GPIO_ReadPin(GPIOC, KEY_2_PIN) == GPIO_PIN_SET)
..\Src\STM32ButtonController.cpp(37): error:  #20: identifier "KEY_1_PIN" is undefined
    if (HAL_GPIO_ReadPin(GPIOH, KEY_1_PIN) == GPIO_PIN_SET)
..\Src\STM32ButtonController.cpp(39): error:  #20: identifier "KEY_0_PIN" is undefined
    if (HAL_GPIO_ReadPin(GPIOH, KEY_0_PIN) == GPIO_PIN_SET)
..\Src\STM32ButtonController.cpp: 1 warning, 3 errors
"demo\demo.axf" - 3 Error(s), 1 Warning(s).
Target not created.
Build Time Elapsed:  00:00:03

I've initialized the pin within the cube for the target as Keil.

But still, after compiling the code with keil; I do have this popping up. the GPIO are not define.

NMung.1
Associate II
uint8_t STM32ButtonController::getKeyState(void)
 
{
 
 if (HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13)==0x01)
 
   return '0';
 
 if (HAL_GPIO_ReadPin(GPIOH,GPIO_PIN_2) ==0x01)
 
   return '1';
 
 if (HAL_GPIO_ReadPin(GPIOH,GPIO_PIN_3) == GPIO_PIN_SET)
 
   return '2';

Build started: Project: demo

*** Using Compiler 'V5.06 update 6 (build 750)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin'

*** Warning: You are compiling one or more files of source type C++ and have selected 'use MicroLIB'. MicroLIB does not support C++!

  

Build target 'demo'

linking...

Program Size: Code=46616 RO-data=858612 RW-data=604 ZI-data=22828  

FromELF: creating hex file...

"demo\demo.axf" - 0 Error(s), 0 Warning(s).

Build Time Elapsed: 00:00:01

============================================================================================================================================================================================================================================================================

I've figured it out. thank you

😀