Files
esp32-learning/08-74HC595-Snake/08-74HC595-Snake.ino

75 lines
1.9 KiB
Arduino
Raw Normal View History

2026-06-12 12:40:08 -04:00
#include <Arduino.h>
#include "controls.h"
#include "error.h"
/*
Need 11 GPIO pins
- 74HC95 for the 8 segment
- 74HC595 for the rows of the 8x8
- 74HC9 for the cols of the 8x8
- 1x for the Joystick button
- 1x for the buzzer
Need 3 ADC pins
- Joy X
- Joy Y
- Difficulty potentiometer
*/
// Pins 1 and 2 are on ADC channel 0 and 1.
#define PIN_JOY_X 1
#define PIN_JOY_Y 2
// Pin 47 is GPIO only
#define PIN_JOY_Z 47
// We save 4 pins by updating all 3 shift registers at once
// with one clock and one update signal. Each shift register
// only needs its own data line.
#define PIN_74HC595_CLOCK 11
#define PIN_74HC595_UPDATE 12
#define PIN_74HC595_LEDROWS_DATA 18
#define PIN_74HC595_LEDCOLS_DATA 13
#define PIN_74HC595_SCOREBOARD_DATA 14
#define PIN_BUZZER 21
#define PIN_DIFFICULTY 9
#define PWM_FREQUENCY 1000
#define PWM_BITWIDTH 12
Joystick js;
void initSerial()
{
Serial.begin(115200);
}
void setup() {
memset(&js, 0x00, sizeof(Joystick));
initSerial();
if ( initJoystick(&js, PIN_JOY_X, PIN_JOY_Y, PIN_JOY_Z) != ERRNO_SUCCESS ) {
Serial.printf("Failed to initialized Joystick datastructure : %d\n", errno);
}
}
void loop() {
if ( readButton(&js.button) != ERRNO_SUCCESS ) Serial.printf("Failed to read calibration button : %d\n", errno);
if ( js.button.state == BUTTON_STATE_DOWN && js.state != JOYSTICK_STATE_CALIBRATING ) {
js.button.state = BUTTON_STATE_HELD;
js.state = JOYSTICK_STATE_CALIBRATING;
}
if ( js.state == JOYSTICK_STATE_CALIBRATING ) {
if ( calibrateJoystick(&js) != ERRNO_SUCCESS ) {
Serial.printf("Failed to calibrate joystick : %d\n", errno);
}
}
if ( ( js.state & JOYSTICK_STATE_CALIBRATED) == JOYSTICK_STATE_CALIBRATED ) {
if ( readJoystick(&js) != ERRNO_SUCCESS ) {
Serial.printf("Failed to read joystick state : %d\n", errno);
}
}
}