380 views|1 replies

126

Posts

1

Resources
The OP
 

Some code sharing about STM32 joystick (five-way switch) [Copy link]

Regarding JOYSTICK, I have done some development on GFX02Z1 before, so I can use the control code of Nathan Xi.

.Cpp file:

/*
 * MB1818ButtonController.cpp
 *
 *  Created on: Apr 30, 2024
 *      Author: user
 */
#include <MB1818ButtonController.hpp>
#include <main.h>
#include <touchgfx/hal/HAL.hpp>

void MB1818ButtonController::init()
{
    previousState = 0;
}

bool MB1818ButtonController::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 MB1818ButtonController::getKeyState(void)
{
    uint8_t keyPressed = 0;

    if (touchgfx::HAL::getInstance()->getDisplayOrientation() == touchgfx::ORIENTATION_PORTRAIT)
    {
        if (HAL_GPIO_ReadPin(KEY_CENTER_GPIO_Port, KEY_CENTER_Pin) == GPIO_PIN_RESET)
        {
            keyPressed = '5'; // Corresponds to keyboard keypad center
        }
        else if (HAL_GPIO_ReadPin(KEY_LEFT_GPIO_Port, KEY_LEFT_Pin) == GPIO_PIN_RESET)
        {
            keyPressed = '4'; // Corresponds to keyboard keypad left
        }
        else if (HAL_GPIO_ReadPin(KEY_RIGHT_GPIO_Port, KEY_RIGHT_Pin) == GPIO_PIN_RESET)
        {
            keyPressed = '6'; // Corresponds to keyboard keypad right
        }
        else if (HAL_GPIO_ReadPin(KEY_UP_GPIO_Port, KEY_UP_Pin) == GPIO_PIN_RESET)
        {
            keyPressed = '8'; // Corresponds to keyboard keypad up
        }
        else if (HAL_GPIO_ReadPin(KEY_DOWN_GPIO_Port, KEY_DOWN_Pin) == GPIO_PIN_RESET)
        {
            keyPressed = '2'; // Corresponds to keyboard keypad down
        }
        //Blue user button on Nucleo boad
        else if (HAL_GPIO_ReadPin(BUTTON_USER_GPIO_Port, BUTTON_USER_Pin) == GPIO_PIN_RESET)
        {
            keyPressed = '0';
        }
    }
    else
    {
        if (HAL_GPIO_ReadPin(KEY_CENTER_GPIO_Port, KEY_CENTER_Pin) == GPIO_PIN_RESET)
        {
            keyPressed = '5'; // Corresponds to keyboard keypad enter
        }
        else if (HAL_GPIO_ReadPin(KEY_LEFT_GPIO_Port, KEY_LEFT_Pin) == GPIO_PIN_RESET)
        {
            keyPressed = '8'; // Corresponds to keyboard keypad up
        }
        else if (HAL_GPIO_ReadPin(KEY_RIGHT_GPIO_Port, KEY_RIGHT_Pin) == GPIO_PIN_RESET)
        {
            keyPressed = '2'; // Corresponds to keyboard keypad down
        }
        else if (HAL_GPIO_ReadPin(KEY_UP_GPIO_Port, KEY_UP_Pin) == GPIO_PIN_RESET)
        {
            keyPressed = '6'; // Corresponds to keyboard keypad right
        }
        else if (HAL_GPIO_ReadPin(KEY_DOWN_GPIO_Port, KEY_DOWN_Pin) == GPIO_PIN_RESET)
        {
            keyPressed = '4'; // Corresponds to keyboard keypad left
        }
        //Blue user button on Nucleo boad
        else if (HAL_GPIO_ReadPin(BUTTON_USER_GPIO_Port, BUTTON_USER_Pin) == GPIO_PIN_RESET)
        {
            keyPressed = '0';
        }
    }

    return keyPressed;
}

.hpp file:

/*
 * MB1818ButtonController.hpp
 *
 *  Created on: Apr 30, 2024
 *      Author: user
 */

#ifndef APPLICATION_USER_CORE_MB1818BUTTONCONTROLLER_HPP_
#define APPLICATION_USER_CORE_MB1818BUTTONCONTROLLER_HPP_

#include <platform/driver/button/ButtonController.hpp>

class MB1818ButtonController : public touchgfx::ButtonController
{
    virtual void init();
    virtual bool sample(uint8_t& key);

private:
    uint8_t getKeyState(void);
    uint8_t previousState;
};
#endif /* APPLICATION_USER_CORE_MB1818BUTTONCONTROLLER_HPP_ */

However, one thing to note is that this part of the code is C++ code for TOUCHGFX, and it needs to be modified appropriately when used.

This post is from stm32/stm8

Latest reply

Thanks for sharing some code of these STM32 joystick (five-way switch)   Details Published on 2024-8-4 09:10
Personal signature

没用比没有强

 

6547

Posts

0

Resources
2
 

Thanks for sharing some code of these STM32 joystick (five-way switch)

This post is from stm32/stm8
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list