#include "main.h"
#include "i2c-lcd.h"
#include <stdbool.h>
//--- global variables ---
int credits = 0;
int time_remaining = 0;
bool is_game_active = false;
//--- GPIO definitions ---
#define STEPPER_X_STEP_PIN GPIO_PIN_...
#define STEPPER_X_DIR_PIN GPIO_PIN_...
// ... and for Y, Z axes
#define COIN_SENSOR_PIN GPIO_PIN_...
#define BUTTON_PIN GPIO_PIN_...
#define GRIPPER_PIN GPIO_PIN_...
//--- Function prototypes ---
void home_all_axes();
void move_motor(GPIO_TypeDef* port_step, uint16_t pin_step, GPIO_TypeDef* port_dir, uint16_t pin_dir, int steps, bool dir);
void open_gripper();
void close_gripper();
void update_lcd();
//--- main function ---
int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
lcd_init();
update_lcd(); // Initial LCD display
home_all_axes(); // Find home position on startup
while (1) {
// Check for coin insertion
if (HAL_GPIO_ReadPin(COIN_SENSOR_PORT, COIN_SENSOR_PIN) == GPIO_PIN_RESET) {
HAL_Delay(50); // Debounce
if (HAL_GPIO_ReadPin(COIN_SENSOR_PORT, COIN_SENSOR_PIN) == GPIO_PIN_RESET) {
credits++;
is_game_active = false;
time_remaining = 0;
update_lcd();
while (HAL_GPIO_ReadPin(COIN_SENSOR_PORT, COIN_SENSOR_PIN) == GPIO_PIN_RESET); // Wait for coin to pass
}
}
// Check for game start button press
if (credits > 0 && HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN) == GPIO_PIN_RESET) {
HAL_Delay(50); // Debounce
if (HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN) == GPIO_PIN_RESET) {
if (!is_game_active) {
credits--;
time_remaining = 25; // Game time in seconds
is_game_active = true;
update_lcd();
}
while (HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN) == GPIO_PIN_RESET); // Wait for button release
}
}
if (is_game_active) {
// Game logic
// Use buttons or joystick to control motor movement
// Example movement:
// if (joystick_right) { move_motor(STEPPER_X_PORT, STEPPER_X_STEP_PIN, STEPPER_X_DIR_PORT, STEPPER_X_DIR_PIN, 10, true); }
// Check if gripper button is pressed
// if (gripper_button_pressed) {
// close_gripper();
// HAL_Delay(1000);
// // Move Z axis up
// // Return to home position
// open_gripper();
// is_game_active = false;
// time_remaining = 0;
// home_all_axes();
// update_lcd();
// }
// Time countdown
// This part requires a timer interrupt or a non-blocking delay
// For now, a simple delay is used as a placeholder
HAL_Delay(1000);
time_remaining--;
update_lcd();
if (time_remaining <= 0) {
is_game_active = false;
home_all_axes();
update_lcd();
}
}
HAL_Delay(10); // Loop delay
}
}
//--- Helper Functions ---
void update_lcd() {
char time_str[16], credits_str[16];
// Clear display and set cursor to top left
lcd_clear();
lcd_put_cur(0, 0);
sprintf(time_str, "Time: %d sec", time_remaining);
lcd_send_string(time_str);
lcd_put_cur(1, 0);
sprintf(credits_str, "Credits: %d", credits);
lcd_send_string(credits_str);
}