Skip to content

Commit 4c7222e

Browse files
committed
Add buzzer support
A buzzer can be connected between pins 2 and ground of the Arduino board. It will be used to alert the user when they need to look at the game.
1 parent 2e0c610 commit 4c7222e

File tree

6 files changed

+48
-12
lines changed

6 files changed

+48
-12
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ all: swsh.hex usb-iface.hex
88
# Put program definitions (.o => src/<prog>.elf) here
99
# make <prog>.hex will generate the final program and make flash-<prog> will
1010
# flash it.
11-
src/swsh.elf: src/swsh/swsh.o src/lib/automation.o src/lib/automation-utils.o src/lib/led-button.o
11+
src/swsh.elf: src/swsh/swsh.o src/lib/automation.o src/lib/automation-utils.o src/lib/user-io.o
1212

1313
flash-%: %.hex
1414
avrdude -p atmega328p -c avrispmkii -P usb -U flash:w:$<:i

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ Switch controller using an Arduino UNO R3.
66

77
The API allows sending button presses/stick movements on the emulated
88
controller, to read the state of a physical button connected between the
9-
Arduino’s pins 12 and ground, and to control the Arduino’s on-board LED.
9+
Arduino’s pins 12 and ground, and to control the Arduino’s on-board LED as well
10+
as an external buzzer.
1011

1112
Pokémon Sword/Shield automation
1213
-------------------------------
@@ -67,6 +68,9 @@ Required hardware
6768
be tested. (Note: DFU programming may also stop working once this program is
6869
flashed onto the Arduino, so you should not attempt it if you do not have
6970
access to an external programmer)
71+
- A buzzer can be optionally attached between pins 2 and GND of the Arduino
72+
board, in order for the automation process to notify the user when something
73+
needs their attention.
7074

7175
Required software
7276
-----------------

src/lib/led-button.c renamed to src/lib/user-io.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "led-button.h"
1+
#include "user-io.h"
22

33
#include <avr/io.h>
44
#include <util/delay.h>
@@ -9,12 +9,16 @@
99
/* Button on digital pin 12 port B */
1010
#define PORTB_BUTTON (1 << 4)
1111

12+
/* Buzzer (digital pin 2) on port D */
13+
#define PORTD_BUZZER (1 << 2)
14+
1215

1316
/* Initializes the LED/button interface. */
1417
void init_led_button(void)
1518
{
16-
/* Configure LED as output, button as input */
19+
/* Configure LED as output, buzzer as output, button as input */
1720
DDRB = (DDRB | PORTB_LED) & (~PORTB_BUTTON);
21+
DDRD |= PORTD_BUZZER;
1822

1923
/* Enable pullup on button */
2024
PORTB |= PORTB_BUTTON;
@@ -71,3 +75,12 @@ uint8_t blink_led(uint16_t on_time_ms, uint16_t off_time_ms, uint8_t count,
7175

7276
return button_presses;
7377
}
78+
79+
80+
/* Emit a brief beep the buzzer. */
81+
void beep(void)
82+
{
83+
PORTD |= PORTD_BUZZER;
84+
_delay_ms(1);
85+
PORTD &= ~PORTD_BUZZER;
86+
}

src/lib/led-button.h renamed to src/lib/user-io.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
/*
2-
* LED and button interface
2+
* I/O user interface
33
*
44
* This file provides functions to provide a basic user interface using the
5-
* Arduino UNO LED (L) and a push button connected beween pins 12 and GND.
5+
* Arduino UNO LED (L) a push button connected beween pins 12 and GND, and
6+
* an optional buzzer connected between pins 2 and GND.
67
*
78
* The two other LEDs on the UNO board (RX/TX) are only accessible by the
89
* USB interface and can be set using the automation API (see automation.h)
910
*/
1011

11-
#ifndef LED_BUTTON_H
12-
#define LED_BUTTON_H
12+
#ifndef USER_IO_H
13+
#define USER_IO_H
1314

1415
#include <stdint.h>
1516
#include <stdbool.h>
1617

1718
/*
18-
* Initializes the LED/button interface. Must be called before calling
19-
* other functions in this file.
19+
* Initializes the IO interface. Must be called before calling other functions
20+
* in this file.
2021
*/
2122
void init_led_button(void);
2223

@@ -36,4 +37,9 @@ uint8_t count_button_presses(uint16_t wait_ms);
3637
uint8_t blink_led(uint16_t on_time_ms, uint16_t off_time_ms, uint8_t count,
3738
bool wait_for_first_press);
3839

40+
/*
41+
* Emit a brief beep the buzzer.
42+
*/
43+
void beep(void);
44+
3945
#endif

src/swsh/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ Requirements
88

99
You will need an Arduino UNO R3, an external Arduino programmer, and a
1010
pushbutton inserted in the Arduino board, between pins 13 and GND (on the top
11-
row). See [the main README](../../README.md#required-hardware) for details.
11+
row).
12+
13+
You can additionally install a buzzer between pins 2 and GND.
14+
15+
See [the main README](../../README.md#required-hardware) for details.
1216

1317
Installation
1418
------------

src/swsh/swsh.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <util/delay.h>
66

77
#include "automation-utils.h"
8-
#include "led-button.h"
8+
#include "user-io.h"
99

1010
/* Static functions */
1111
static void temporary_control(void);
@@ -21,6 +21,9 @@ int main(void)
2121
init_automation();
2222
init_led_button();
2323

24+
/* Initial beep to confirm that the buzzer works */
25+
beep();
26+
2427
/* Wait for the user to press the button (should be on the Switch main menu) */
2528
blink_led(100, 100, 1, true);
2629

@@ -122,6 +125,9 @@ void max_raid(void)
122125
set_text_speed(/* fast_speed */ false, /* save */ true);
123126

124127
for (;;) {
128+
/* Ask the user to look at the light pillar color */
129+
beep();
130+
125131
/* Drop the Wishing Piece in the den */
126132
use_wishing_piece_and_pause();
127133

@@ -155,6 +161,9 @@ void max_raid(void)
155161
);
156162

157163
for (;;) {
164+
/* Ask the user to look at the Pokémon in the Max Raid Battle */
165+
beep();
166+
158167
/* Do the user wants to do this Raid? */
159168
if (blink_led(250, 250, 10, false)) {
160169
/* Restore the clock */

0 commit comments

Comments
 (0)