Send button state to CAN
This commit is contained in:
		@ -4,6 +4,20 @@
 | 
				
			|||||||
#include "events.h"
 | 
					#include "events.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "stm32g4xx_hal.h"
 | 
					#include "stm32g4xx_hal.h"
 | 
				
			||||||
 | 
					#include "stm32g4xx_hal_gpio.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef struct {
 | 
				
			||||||
 | 
					  GPIO_PinState left;
 | 
				
			||||||
 | 
					  GPIO_PinState right;
 | 
				
			||||||
 | 
					  GPIO_PinState r2d;
 | 
				
			||||||
 | 
					  GPIO_PinState enter;
 | 
				
			||||||
 | 
					  GPIO_PinState enc0a;
 | 
				
			||||||
 | 
					  GPIO_PinState enc0b;
 | 
				
			||||||
 | 
					  GPIO_PinState enc1a;
 | 
				
			||||||
 | 
					  GPIO_PinState enc1b;
 | 
				
			||||||
 | 
					} ButtonsState;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ButtonsState get_buttons_state();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void handle_button_press(const ButtonPressEvent* ev);
 | 
					void handle_button_press(const ButtonPressEvent* ev);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -4,13 +4,21 @@
 | 
				
			|||||||
#include "state.h"
 | 
					#include "state.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "stm32g4xx_hal.h"
 | 
					#include "stm32g4xx_hal.h"
 | 
				
			||||||
 | 
					#include "stm32g4xx_hal_def.h"
 | 
				
			||||||
#include "stm32g4xx_hal_fdcan.h"
 | 
					#include "stm32g4xx_hal_fdcan.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define CAN_ID_MISSION_SELECT 0x400
 | 
					#define CAN_ID_MISSION_SELECT 0x400
 | 
				
			||||||
 | 
					#define CAN_ID_BUTTONS 0x401
 | 
				
			||||||
#define CAN_ID_AS_MISSION_FB 0x410
 | 
					#define CAN_ID_AS_MISSION_FB 0x410
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define CAN_BUTTONS_LEFT (1 << 0)
 | 
				
			||||||
 | 
					#define CAN_BUTTONS_RIGHT (1 << 1)
 | 
				
			||||||
 | 
					#define CAN_BUTTONS_R2D (1 << 2)
 | 
				
			||||||
 | 
					#define CAN_BUTTONS_ENTER (1 << 3)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void vehicle_init(FDCAN_HandleTypeDef* handle);
 | 
					void vehicle_init(FDCAN_HandleTypeDef* handle);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void vehicle_send_mission_select(Mission mission);
 | 
					HAL_StatusTypeDef vehicle_send_mission_select(Mission mission);
 | 
				
			||||||
 | 
					HAL_StatusTypeDef vehicle_send_buttons();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif // __VEHICLE_H
 | 
					#endif // __VEHICLE_H
 | 
				
			||||||
 | 
				
			|||||||
@ -138,10 +138,7 @@ int main(void) {
 | 
				
			|||||||
      HAL_GPIO_WritePin(DISP_RESET_GPIO_Port, DISP_RESET_Pin, GPIO_PIN_SET);
 | 
					      HAL_GPIO_WritePin(DISP_RESET_GPIO_Port, DISP_RESET_Pin, GPIO_PIN_SET);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    handle_events();
 | 
					    handle_events();
 | 
				
			||||||
    if (now - last_can > 100) {
 | 
					    vehicle_send_buttons();
 | 
				
			||||||
      // vehicle_send_mission_select(MISSION_ACCELERATION);
 | 
					 | 
				
			||||||
      last_can = now;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    loop_delay();
 | 
					    loop_delay();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  /* USER CODE END 3 */
 | 
					  /* USER CODE END 3 */
 | 
				
			||||||
 | 
				
			|||||||
@ -5,30 +5,58 @@
 | 
				
			|||||||
#include "state.h"
 | 
					#include "state.h"
 | 
				
			||||||
#include "vehicle.h"
 | 
					#include "vehicle.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "stm32g4xx_hal.h"
 | 
				
			||||||
#include "stm32g4xx_hal_gpio.h"
 | 
					#include "stm32g4xx_hal_gpio.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <stdint.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define MIN_PRESS_INTERVAL 100
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void HAL_GPIO_EXTI_Callback(uint16_t pin) {
 | 
					void HAL_GPIO_EXTI_Callback(uint16_t pin) {
 | 
				
			||||||
 | 
					  static uint32_t last_presses[4] = {0, 0, 0, 0};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Event ev;
 | 
					  Event ev;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  GPIO_PinState state = HAL_GPIO_ReadPin(GPIOB, pin);
 | 
					  GPIO_PinState state = HAL_GPIO_ReadPin(GPIOB, pin);
 | 
				
			||||||
  ev.type = state;
 | 
					  ev.type = state;
 | 
				
			||||||
 | 
					  uint32_t now = HAL_GetTick();
 | 
				
			||||||
  switch (pin) {
 | 
					  switch (pin) {
 | 
				
			||||||
  case BTN0_Pin:
 | 
					  case BTN0_Pin: {
 | 
				
			||||||
 | 
					    if (now - last_presses[0] < MIN_PRESS_INTERVAL) {
 | 
				
			||||||
 | 
					      return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    last_presses[0] = now;
 | 
				
			||||||
    ev.type = EV_BTN_PRESS;
 | 
					    ev.type = EV_BTN_PRESS;
 | 
				
			||||||
    ev.btn_press = BTN_PRESS_PREV;
 | 
					    ev.btn_press = BTN_PRESS_PREV;
 | 
				
			||||||
    break;
 | 
					    break;
 | 
				
			||||||
  case BTN1_Pin:
 | 
					  }
 | 
				
			||||||
 | 
					  case BTN1_Pin: {
 | 
				
			||||||
 | 
					    if (now - last_presses[1] < MIN_PRESS_INTERVAL) {
 | 
				
			||||||
 | 
					      return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    last_presses[1] = now;
 | 
				
			||||||
    ev.type = EV_BTN_PRESS;
 | 
					    ev.type = EV_BTN_PRESS;
 | 
				
			||||||
    ev.btn_press = BTN_PRESS_R2D;
 | 
					    ev.btn_press = BTN_PRESS_R2D;
 | 
				
			||||||
    break;
 | 
					    break;
 | 
				
			||||||
  case BTN2_Pin:
 | 
					  }
 | 
				
			||||||
 | 
					  case BTN2_Pin: {
 | 
				
			||||||
 | 
					    if (now - last_presses[2] < MIN_PRESS_INTERVAL) {
 | 
				
			||||||
 | 
					      return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    last_presses[2] = now;
 | 
				
			||||||
    ev.type = EV_BTN_PRESS;
 | 
					    ev.type = EV_BTN_PRESS;
 | 
				
			||||||
    ev.btn_press = BTN_PRESS_OK;
 | 
					    ev.btn_press = BTN_PRESS_OK;
 | 
				
			||||||
    break;
 | 
					    break;
 | 
				
			||||||
  case BTN3_Pin:
 | 
					  }
 | 
				
			||||||
 | 
					  case BTN3_Pin: {
 | 
				
			||||||
 | 
					    if (now - last_presses[3] < MIN_PRESS_INTERVAL) {
 | 
				
			||||||
 | 
					      return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    last_presses[3] = now;
 | 
				
			||||||
    ev.type = EV_BTN_PRESS;
 | 
					    ev.type = EV_BTN_PRESS;
 | 
				
			||||||
    ev.btn_press = BTN_PRESS_NEXT;
 | 
					    ev.btn_press = BTN_PRESS_NEXT;
 | 
				
			||||||
    break;
 | 
					    break;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
  default:
 | 
					  default:
 | 
				
			||||||
    Error_Handler();
 | 
					    Error_Handler();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@ -75,3 +103,16 @@ void handle_button_press_mission_select(const ButtonPressEvent* ev) {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ButtonsState get_buttons_state() {
 | 
				
			||||||
 | 
					  ButtonsState result;
 | 
				
			||||||
 | 
					  result.left = HAL_GPIO_ReadPin(BTN0_GPIO_Port, BTN0_Pin);
 | 
				
			||||||
 | 
					  result.right = HAL_GPIO_ReadPin(BTN3_GPIO_Port, BTN3_Pin);
 | 
				
			||||||
 | 
					  result.r2d = HAL_GPIO_ReadPin(BTN1_GPIO_Port, BTN1_Pin);
 | 
				
			||||||
 | 
					  result.enter = HAL_GPIO_ReadPin(BTN2_GPIO_Port, BTN2_Pin);
 | 
				
			||||||
 | 
					  result.enc0a = HAL_GPIO_ReadPin(ENC0A_GPIO_Port, ENC0A_Pin);
 | 
				
			||||||
 | 
					  result.enc0b = HAL_GPIO_ReadPin(ENC0B_GPIO_Port, ENC0B_Pin);
 | 
				
			||||||
 | 
					  result.enc1a = HAL_GPIO_ReadPin(ENC1A_GPIO_Port, ENC1A_Pin);
 | 
				
			||||||
 | 
					  result.enc1b = HAL_GPIO_ReadPin(ENC1B_GPIO_Port, ENC1B_Pin);
 | 
				
			||||||
 | 
					  return result;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -2,6 +2,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#include "main.h"
 | 
					#include "main.h"
 | 
				
			||||||
#include "state.h"
 | 
					#include "state.h"
 | 
				
			||||||
 | 
					#include "user_inputs.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "stm32g4xx_hal_def.h"
 | 
					#include "stm32g4xx_hal_def.h"
 | 
				
			||||||
#include "stm32g4xx_hal_fdcan.h"
 | 
					#include "stm32g4xx_hal_fdcan.h"
 | 
				
			||||||
@ -47,7 +48,7 @@ void vehicle_init(FDCAN_HandleTypeDef* handle) {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void vehicle_send_mission_select(Mission mission) {
 | 
					HAL_StatusTypeDef vehicle_send_mission_select(Mission mission) {
 | 
				
			||||||
  // TODO: Automatically resend this until it's acknowledged
 | 
					  // TODO: Automatically resend this until it's acknowledged
 | 
				
			||||||
  FDCAN_TxHeaderTypeDef header;
 | 
					  FDCAN_TxHeaderTypeDef header;
 | 
				
			||||||
  header.Identifier = CAN_ID_MISSION_SELECT;
 | 
					  header.Identifier = CAN_ID_MISSION_SELECT;
 | 
				
			||||||
@ -62,9 +63,28 @@ void vehicle_send_mission_select(Mission mission) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  uint8_t data[1];
 | 
					  uint8_t data[1];
 | 
				
			||||||
  data[0] = mission;
 | 
					  data[0] = mission;
 | 
				
			||||||
  if (HAL_FDCAN_AddMessageToTxFifoQ(can, &header, data) != HAL_OK) {
 | 
					  return HAL_FDCAN_AddMessageToTxFifoQ(can, &header, data);
 | 
				
			||||||
    Error_Handler();
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					HAL_StatusTypeDef vehicle_send_buttons() {
 | 
				
			||||||
 | 
					  FDCAN_TxHeaderTypeDef header;
 | 
				
			||||||
 | 
					  header.Identifier = CAN_ID_BUTTONS;
 | 
				
			||||||
 | 
					  header.IdType = FDCAN_STANDARD_ID;
 | 
				
			||||||
 | 
					  header.TxFrameType = FDCAN_DATA_FRAME;
 | 
				
			||||||
 | 
					  header.DataLength = FDCAN_DLC_BYTES_1;
 | 
				
			||||||
 | 
					  header.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
 | 
				
			||||||
 | 
					  header.BitRateSwitch = FDCAN_BRS_OFF;
 | 
				
			||||||
 | 
					  header.FDFormat = FDCAN_CLASSIC_CAN;
 | 
				
			||||||
 | 
					  header.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
 | 
				
			||||||
 | 
					  header.MessageMarker = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  ButtonsState buttons = get_buttons_state();
 | 
				
			||||||
 | 
					  uint8_t data[1];
 | 
				
			||||||
 | 
					  data[0] = (buttons.left ? CAN_BUTTONS_LEFT : 0) |
 | 
				
			||||||
 | 
					            (buttons.right ? CAN_BUTTONS_RIGHT : 0) |
 | 
				
			||||||
 | 
					            (buttons.r2d ? CAN_BUTTONS_R2D : 0) |
 | 
				
			||||||
 | 
					            (buttons.enter ? CAN_BUTTONS_ENTER : 0);
 | 
				
			||||||
 | 
					  return HAL_FDCAN_AddMessageToTxFifoQ(can, &header, data);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* BEGIN CAN MESSAGE HANDLERS { */
 | 
					/* BEGIN CAN MESSAGE HANDLERS { */
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user