Select missions with right encoder
This commit is contained in:
@ -1,8 +0,0 @@
|
||||
#ifndef __INC_GRAPHICS_H
|
||||
#define __INC_GRAPHICS_H
|
||||
|
||||
#include "tx_port.h"
|
||||
|
||||
void graphics_thread_entry(ULONG _);
|
||||
|
||||
#endif // __INC_GRAPHICS_H
|
||||
@ -4,9 +4,17 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void HX8357D_WriteData(uint8_t *data, size_t data_len);
|
||||
void HX8357D_WriteReg(uint8_t addr, uint8_t *data, size_t data_len);
|
||||
void HX8357D_Init();
|
||||
void HX8357D_Mode_RGB666();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // __HX8357D_H
|
||||
|
||||
@ -31,7 +31,7 @@ extern "C" {
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
#include "tx_api.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
@ -42,6 +42,7 @@ extern "C" {
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
/* USER CODE BEGIN EC */
|
||||
extern volatile int ltdc_cb_triggered;
|
||||
extern TX_QUEUE ui_queue;
|
||||
/* USER CODE END EC */
|
||||
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
@ -61,12 +62,16 @@ void Error_Handler(void);
|
||||
/* Private defines -----------------------------------------------------------*/
|
||||
#define ENC1A_Pin GPIO_PIN_3
|
||||
#define ENC1A_GPIO_Port GPIOE
|
||||
#define ENC1A_EXTI_IRQn EXTI3_IRQn
|
||||
#define ENC1B_Pin GPIO_PIN_4
|
||||
#define ENC1B_GPIO_Port GPIOE
|
||||
#define ENC1B_EXTI_IRQn EXTI4_IRQn
|
||||
#define ENC2A_Pin GPIO_PIN_5
|
||||
#define ENC2A_GPIO_Port GPIOE
|
||||
#define ENC2A_EXTI_IRQn EXTI9_5_IRQn
|
||||
#define ENC2B_Pin GPIO_PIN_6
|
||||
#define ENC2B_GPIO_Port GPIOE
|
||||
#define ENC2B_EXTI_IRQn EXTI9_5_IRQn
|
||||
#define BTN1_Pin GPIO_PIN_0
|
||||
#define BTN1_GPIO_Port GPIOF
|
||||
#define BTN2_Pin GPIO_PIN_1
|
||||
|
||||
@ -52,6 +52,9 @@ void MemManage_Handler(void);
|
||||
void BusFault_Handler(void);
|
||||
void UsageFault_Handler(void);
|
||||
void DebugMon_Handler(void);
|
||||
void EXTI3_IRQHandler(void);
|
||||
void EXTI4_IRQHandler(void);
|
||||
void EXTI9_5_IRQHandler(void);
|
||||
void TIM6_DAC_IRQHandler(void);
|
||||
void LTDC_IRQHandler(void);
|
||||
/* USER CODE BEGIN EFP */
|
||||
|
||||
@ -2,5 +2,15 @@
|
||||
#define __INC_STW_DEFINES_H
|
||||
|
||||
#define NUM_MISSIONS 7
|
||||
typedef enum {
|
||||
MISSION_NONE = 0,
|
||||
MISSION_ACCEL = 1,
|
||||
MISSION_SKIDPAD = 2,
|
||||
MISSION_AUTOX = 3,
|
||||
MISSION_TRACKDRIVE = 4,
|
||||
MISSION_EBS = 5,
|
||||
MISSION_INSPECTION = 6,
|
||||
MISSION_MANUAL = 7
|
||||
} Mission;
|
||||
|
||||
#endif // __INC_STW_DEFINES_H
|
||||
|
||||
28
Core/Inc/ui.h
Normal file
28
Core/Inc/ui.h
Normal file
@ -0,0 +1,28 @@
|
||||
#ifndef __INC_ui_H
|
||||
#define __INC_ui_H
|
||||
|
||||
#include "tx_port.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define NUM_BUTTONS 6
|
||||
#define NUM_ENCS 2
|
||||
#define BUTTON_MIN_PRESS_TIME 50 // ms
|
||||
#define ENC_MAX_PHASE 50 // ms
|
||||
|
||||
typedef enum { UMK_BTN_RELEASED, UMK_ENC_CW, UMK_ENC_CCW } UIMessageKind;
|
||||
|
||||
typedef struct {
|
||||
UIMessageKind kind;
|
||||
int number;
|
||||
} UIMessage;
|
||||
|
||||
void ui_thread_entry(ULONG _);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // __INC_ui_H
|
||||
@ -1,17 +0,0 @@
|
||||
#include "graphics.h"
|
||||
|
||||
#include "stm32h7xx_hal.h"
|
||||
#include "tx_api.h"
|
||||
|
||||
#include "hx8357d.h"
|
||||
#include "main.h"
|
||||
|
||||
void graphics_thread_entry(ULONG _) {
|
||||
HX8357D_Init();
|
||||
HX8357D_Mode_RGB666();
|
||||
|
||||
while (1) {
|
||||
HAL_GPIO_TogglePin(STATUS2_GPIO_Port, STATUS2_Pin);
|
||||
tx_thread_sleep(100);
|
||||
}
|
||||
}
|
||||
@ -135,6 +135,8 @@ int main(void) {
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
HX8357D_Init();
|
||||
HX8357D_Mode_RGB666();
|
||||
/* USER CODE END 2 */
|
||||
|
||||
MX_ThreadX_Init();
|
||||
@ -395,8 +397,7 @@ static void MX_LTDC_Init(void) {
|
||||
pLayerCfg.Alpha0 = 0;
|
||||
pLayerCfg.BlendingFactor1 = LTDC_BLENDING_FACTOR1_CA;
|
||||
pLayerCfg.BlendingFactor2 = LTDC_BLENDING_FACTOR2_CA;
|
||||
// pLayerCfg.FBStartAdress = (uint32_t)&image_data_ft_logo_rainbow_rgb565;
|
||||
pLayerCfg.FBStartAdress = (uint32_t)0x24040000;
|
||||
pLayerCfg.FBStartAdress = 0;
|
||||
pLayerCfg.ImageWidth = 320;
|
||||
pLayerCfg.ImageHeight = 480;
|
||||
pLayerCfg.Backcolor.Blue = 0;
|
||||
@ -717,7 +718,7 @@ static void MX_GPIO_Init(void) {
|
||||
|
||||
/*Configure GPIO pins : ENC1A_Pin ENC1B_Pin ENC2A_Pin ENC2B_Pin */
|
||||
GPIO_InitStruct.Pin = ENC1A_Pin | ENC1B_Pin | ENC2A_Pin | ENC2B_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
|
||||
|
||||
@ -758,6 +759,16 @@ static void MX_GPIO_Init(void) {
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(BOOT0_SET_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI3_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI3_IRQn);
|
||||
|
||||
HAL_NVIC_SetPriority(EXTI4_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI4_IRQn);
|
||||
|
||||
HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
@ -160,6 +160,49 @@ void DebugMon_Handler(void)
|
||||
/* please refer to the startup file (startup_stm32h7xx.s). */
|
||||
/******************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief This function handles EXTI line3 interrupt.
|
||||
*/
|
||||
void EXTI3_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN EXTI3_IRQn 0 */
|
||||
|
||||
/* USER CODE END EXTI3_IRQn 0 */
|
||||
HAL_GPIO_EXTI_IRQHandler(ENC1A_Pin);
|
||||
/* USER CODE BEGIN EXTI3_IRQn 1 */
|
||||
|
||||
/* USER CODE END EXTI3_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles EXTI line4 interrupt.
|
||||
*/
|
||||
void EXTI4_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN EXTI4_IRQn 0 */
|
||||
|
||||
/* USER CODE END EXTI4_IRQn 0 */
|
||||
HAL_GPIO_EXTI_IRQHandler(ENC1B_Pin);
|
||||
/* USER CODE BEGIN EXTI4_IRQn 1 */
|
||||
|
||||
/* USER CODE END EXTI4_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles EXTI line[9:5] interrupts.
|
||||
*/
|
||||
void EXTI9_5_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN EXTI9_5_IRQn 0 */
|
||||
|
||||
/* USER CODE END EXTI9_5_IRQn 0 */
|
||||
HAL_GPIO_EXTI_IRQHandler(ENC2A_Pin);
|
||||
HAL_GPIO_EXTI_IRQHandler(ENC2B_Pin);
|
||||
/* USER CODE BEGIN EXTI9_5_IRQn 1 */
|
||||
|
||||
/* USER CODE END EXTI9_5_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles TIM6 global interrupt, DAC1_CH1 and DAC1_CH2 underrun error interrupts.
|
||||
*/
|
||||
|
||||
97
Core/Src/ui.c
Normal file
97
Core/Src/ui.c
Normal file
@ -0,0 +1,97 @@
|
||||
#include "ui.h"
|
||||
|
||||
#include "stm32h7a3xx.h"
|
||||
#include "stm32h7xx_hal.h"
|
||||
#include "stm32h7xx_hal_gpio.h"
|
||||
#include "tx_api.h"
|
||||
|
||||
#include "hx8357d.h"
|
||||
#include "main.h"
|
||||
#include <stdint.h>
|
||||
|
||||
void ui_thread_entry(ULONG _) {
|
||||
GPIO_TypeDef *button_ports[NUM_BUTTONS] = {BTN1_GPIO_Port, BTN2_GPIO_Port,
|
||||
BTN3_GPIO_Port, BTN4_GPIO_Port,
|
||||
BTN5_GPIO_Port, BTN6_GPIO_Port};
|
||||
uint16_t button_pins[NUM_BUTTONS] = {BTN1_Pin, BTN2_Pin, BTN3_Pin,
|
||||
BTN4_Pin, BTN5_Pin, BTN6_Pin};
|
||||
GPIO_PinState button_states[NUM_BUTTONS] = {GPIO_PIN_RESET};
|
||||
uint32_t button_change_times[NUM_BUTTONS] = {HAL_GetTick()};
|
||||
|
||||
while (1) {
|
||||
for (int i = 0; i < NUM_BUTTONS; i++) {
|
||||
GPIO_PinState state = HAL_GPIO_ReadPin(button_ports[i], button_pins[i]);
|
||||
if (state != button_states[i]) {
|
||||
uint32_t now = HAL_GetTick();
|
||||
if (state == GPIO_PIN_RESET && now - button_change_times[i]) {
|
||||
// Button release event!
|
||||
UIMessage msg = {.kind = UMK_BTN_RELEASED, .number = i};
|
||||
tx_queue_send(&ui_queue, &msg, TX_NO_WAIT);
|
||||
}
|
||||
button_change_times[i] = now;
|
||||
button_states[i] = state;
|
||||
}
|
||||
}
|
||||
// Release so other threads can get scheduled
|
||||
tx_thread_sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
|
||||
// This gets called when an edge on one of the encoder pins is detected.
|
||||
static uint32_t last_change[NUM_ENCS * 2] = {0};
|
||||
static GPIO_PinState last_state[NUM_ENCS * 2] = {GPIO_PIN_RESET};
|
||||
|
||||
if (GPIO_Pin == ENC2B_Pin) {
|
||||
// ENC2A and ENC2B share an interrupt line, so the HAL calls this callback
|
||||
// once with each pin. Since we already handled the interrupt once for
|
||||
// ENC2A, we can just ignore it now.
|
||||
}
|
||||
|
||||
uint32_t now = HAL_GetTick();
|
||||
|
||||
uint16_t pin_a, pin_b;
|
||||
int idx_a, idx_b;
|
||||
UIMessage msg;
|
||||
if (GPIO_Pin == ENC1A_Pin || GPIO_Pin == ENC1B_Pin) {
|
||||
pin_a = ENC1A_Pin;
|
||||
pin_b = ENC1B_Pin;
|
||||
idx_a = 0;
|
||||
idx_b = 1;
|
||||
msg.number = 0;
|
||||
} else {
|
||||
pin_a = ENC2A_Pin;
|
||||
pin_b = ENC2B_Pin;
|
||||
idx_a = 2;
|
||||
idx_b = 3;
|
||||
msg.number = 1;
|
||||
}
|
||||
|
||||
// All encoder pins are on port E
|
||||
GPIO_PinState state_a = HAL_GPIO_ReadPin(GPIOE, pin_a);
|
||||
GPIO_PinState state_b = HAL_GPIO_ReadPin(GPIOE, pin_b);
|
||||
int a_changed = state_a != last_state[idx_a];
|
||||
int b_changed = state_b != last_state[idx_b];
|
||||
last_state[idx_a] = state_a;
|
||||
last_state[idx_b] = state_b;
|
||||
|
||||
if (state_a == GPIO_PIN_RESET && state_b == GPIO_PIN_RESET) {
|
||||
// Second falling edge, direction depends on which pin changed last
|
||||
if (a_changed && b_changed) {
|
||||
// This shouldn't happen. Ignore this event.
|
||||
last_change[idx_a] = now;
|
||||
last_change[idx_b] = now;
|
||||
return;
|
||||
} else if (a_changed) {
|
||||
last_change[idx_a] = now;
|
||||
msg.kind = UMK_ENC_CCW;
|
||||
} else if (b_changed) {
|
||||
last_change[idx_b] = now;
|
||||
msg.kind = UMK_ENC_CW;
|
||||
} else {
|
||||
// This shouldn't happen. Ignore this event.
|
||||
return;
|
||||
}
|
||||
tx_queue_send(&ui_queue, &msg, TX_NO_WAIT);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user