Skip to content

Commit 379b137

Browse files
committed
Moved minigame to it's own file
1 parent 07fd7d7 commit 379b137

File tree

6 files changed

+272
-245
lines changed

6 files changed

+272
-245
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ SRC_FILES += \
149149
$(SDK_ROOT)/components/libraries/fstorage/nrf_fstorage.c \
150150
$(SDK_ROOT)/components/libraries/fstorage/nrf_fstorage_sd.c \
151151
buzzer/nrf_pwm.c \
152+
minigame.c \
152153

153154
# Include folders common to all targets
154155
INC_FOLDERS += \
@@ -327,6 +328,8 @@ CFLAGS += -DCONFIG_NFCT_PINS_AS_GPIOS
327328
CFLAGS += -DAPP_TIMER_V2 -DAPP_TIMER_V2_RTC1_ENABLED
328329
# Use only static filesystem buffers
329330
CFLAGS += -DLFS_NO_MALLOC
331+
# Switch if hardware has display
332+
CFLAGS += -DHAS_DISPLAY=1
330333

331334
# C++ flags common to all targets
332335
CXXFLAGS += $(OPT)

buzzer/nrf_pwm.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@
66
#include "nrf_sdm.h"
77
#endif
88

9+
#include "nrf_delay.h"
10+
void set_frequency_and_duty_cycle(uint32_t frequency, uint32_t duty_cycle_percent)
11+
{
12+
nrf_pwm_set_max_value((16000000 + (frequency / 2)) / frequency);
13+
nrf_pwm_set_value(0, (16000000 / frequency) * duty_cycle_percent / 100);
14+
}
15+
16+
void beep_speaker_blocking(int duration_ms, int duty_haha_doodie)
17+
{
18+
nrf_pwm_set_enabled(true);
19+
set_frequency_and_duty_cycle((uint32_t)2000, duty_haha_doodie);
20+
nrf_delay_ms(duration_ms);
21+
set_frequency_and_duty_cycle((uint32_t)2000, 0);
22+
nrf_pwm_set_enabled(false);
23+
}
24+
925
static uint32_t pwm_max_value, pwm_next_value[PWM_MAX_CHANNELS], pwm_next_max_value, pwm_io_ch[PWM_MAX_CHANNELS], pwm_running[PWM_MAX_CHANNELS];
1026
static bool pwm_modified[PWM_MAX_CHANNELS];
1127
static uint8_t pwm_gpiote_channel[PWM_MAX_CHANNELS];

buzzer/nrf_pwm.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,8 @@ void nrf_pwm_set_max_value(uint32_t max_value);
5656

5757
void nrf_pwm_set_enabled(bool enabled);
5858

59+
void set_frequency_and_duty_cycle(uint32_t frequency, uint32_t duty_cycle_percent);
60+
61+
void beep_speaker_blocking(int duration_ms, int duty_haha_duty);
62+
5963
#endif

main.c

Lines changed: 5 additions & 245 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ const nrf_drv_twi_t m_twi_master = NRF_DRV_TWI_INSTANCE(0);
8484

8585
#include "lwgps/lwgps.h"
8686

87+
#include "minigame.h"
88+
8789
////////////////////////////////////////
8890
// GPS handle
8991
////////////////////////////////////////
@@ -95,11 +97,7 @@ static LOG_FREESK8 log_message_freesk8;
9597
////////////////////////////////////////
9698
// Button input
9799
////////////////////////////////////////
98-
#include "nrf_gpio.h"
99-
#define PIN_BUTTON 10
100-
#define PIN_BUTTON2 9
101-
#define isButtonPressed !nrf_gpio_pin_read(PIN_BUTTON)
102-
#define isButton2Pressed !nrf_gpio_pin_read(PIN_BUTTON2)
100+
#include "button_input.h"
103101

104102
////////////////////////////////////////
105103
// ESC data
@@ -113,7 +111,6 @@ static LOG_HEADER log_message_header;
113111
////////////////////////////////////////
114112
// Display
115113
////////////////////////////////////////
116-
#define HAS_DISPLAY 1
117114
#if HAS_DISPLAY
118115
static volatile bool update_display = false; // Set to true to trigger I2C communication with OLED
119116
static char display_text_buffer[32] = {0};
@@ -172,11 +169,7 @@ bool is_melody_playing_pause = false;
172169
uint32_t melody_next_note = 0;
173170
int *melody;
174171

175-
void set_frequency_and_duty_cycle(uint32_t frequency, uint32_t duty_cycle_percent)
176-
{
177-
nrf_pwm_set_max_value((16000000 + (frequency / 2)) / frequency);
178-
nrf_pwm_set_value(0, (16000000 / frequency) * duty_cycle_percent / 100);
179-
}
172+
180173

181174
uint32_t app_timer_ms(uint32_t ticks)
182175
{
@@ -361,16 +354,6 @@ void buzzer_init(void)
361354
nrf_pwm_init(&pwm_config);
362355
}
363356

364-
//TODO: replace uses of beep_speaker_blocking
365-
void beep_speaker_blocking(int duration_ms, int duty_haha_duty)
366-
{
367-
nrf_pwm_set_enabled(true);
368-
set_frequency_and_duty_cycle((uint32_t)3100, 50);
369-
nrf_delay_ms(duration_ms);
370-
set_frequency_and_duty_cycle((uint32_t)3100, 0);
371-
nrf_pwm_set_enabled(false);
372-
}
373-
374357
////////////////////////////////////////
375358
//LITTLEFS
376359
////////////////////////////////////////
@@ -2316,230 +2299,7 @@ void user_cfg_get(void)
23162299
}
23172300
}
23182301

2319-
////////////////////////////////////////
2320-
// Game that needs it's own source file
2321-
////////////////////////////////////////
2322-
2323-
#if HAS_DISPLAY
2324-
// Dont ask
2325-
// frame counter
2326-
unsigned int frame = 0;
2327-
// scrore string buffer
2328-
char text[16];
2329-
2330-
// 'skate_push', 24x23px
2331-
const unsigned char skate_push[] =
2332-
{0x00, 0x00, 0x70, 0x00, 0x00, 0xf8, 0x00, 0x00, 0xf8, 0x00, 0x7c, 0xf8, 0x01, 0xff, 0x70, 0x07,
2333-
0xff, 0x00, 0x06, 0x3f, 0x00, 0x00, 0x7e, 0x00, 0x00, 0xfd, 0xe0, 0x01, 0xfb, 0xe0, 0x01, 0xf0,
2334-
0x00, 0x00, 0xf0, 0x00, 0x06, 0x78, 0x00, 0x7f, 0x3c, 0x00, 0xfe, 0x1c, 0x00, 0xfc, 0x1c, 0x00,
2335-
0x00, 0x1c, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x1c, 0x00, 0x0f, 0xff, 0xe0, 0x03, 0xff, 0x80, 0x02,
2336-
0x82, 0x80, 0x03, 0x03, 0x00};
2337-
// 'skate_push_2', 24x23px
2338-
const unsigned char skate_push2[] =
2339-
{0x00, 0x01, 0xc0, 0x00, 0x03, 0xe0, 0x00, 0x03, 0xe0, 0x00, 0x7f, 0xe0, 0x01, 0xff, 0xc0, 0x07,
2340-
0xff, 0x00, 0x06, 0x3f, 0x00, 0x00, 0x7e, 0x60, 0x00, 0xfd, 0xe0, 0x01, 0xf9, 0x80, 0x01, 0xf0,
2341-
0x00, 0x00, 0xf0, 0x00, 0x00, 0x78, 0x00, 0x01, 0xfc, 0x00, 0x01, 0xdc, 0x00, 0x01, 0xdc, 0x00,
2342-
0x01, 0xdc, 0x00, 0x01, 0xdc, 0x00, 0x03, 0x9c, 0x00, 0x0f, 0xff, 0xe0, 0x03, 0xff, 0x80, 0x02,
2343-
0x82, 0x80, 0x03, 0x03, 0x00};
2344-
// 'skate_jump', 24x23px
2345-
const unsigned char skate_jump[] =
2346-
{0x00, 0x70, 0x00, 0x00, 0x78, 0x00, 0x00, 0xf8, 0xc0, 0x00, 0x73, 0xc0, 0x00, 0x27, 0x00, 0x00,
2347-
0xfe, 0x00, 0x01, 0xf8, 0x00, 0x01, 0xf8, 0x00, 0x03, 0xf8, 0x00, 0x03, 0xf8, 0x00, 0x03, 0xf8,
2348-
0x00, 0x01, 0xfc, 0x00, 0x00, 0xfe, 0x00, 0x00, 0xc6, 0x00, 0x01, 0xc6, 0x00, 0x01, 0x86, 0x30,
2349-
0x01, 0x86, 0x60, 0x03, 0x87, 0xc0, 0x03, 0xfd, 0xc0, 0x1f, 0xc1, 0xc0, 0x03, 0x80, 0xc0, 0x02,
2350-
0x80, 0x00, 0x03, 0x80, 0x00};
2351-
// 'traffic_cone', 24x24px
2352-
const unsigned char traffic_cone[] =
2353-
{0x00, 0x20, 0x00, 0x00, 0x20, 0x00, 0x00, 0x70, 0x00, 0x00, 0x70, 0x00, 0x00, 0x70, 0x00, 0x00,
2354-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0xfc, 0x00, 0x01, 0xfc,
2355-
0x00, 0x01, 0xfc, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2356-
0x02, 0x02, 0x00, 0x07, 0xff, 0x00, 0x07, 0xff, 0x40, 0x77, 0xff, 0x70, 0x3b, 0xfe, 0xe0, 0x04,
2357-
0x79, 0x80, 0x01, 0xfc, 0x00, 0x00, 0x70, 0x00};
2358-
2359-
// cloud_1 w: 24 h: 7
2360-
const unsigned char cloud_1[] =
2361-
{ 0x00, 0xF0, 0x00, 0x73, 0x0C, 0x00, 0x88, 0x03,
2362-
0x00, 0x81, 0x04, 0x80, 0x86, 0x00, 0x80, 0x79,
2363-
0x83, 0x00, 0x00, 0x7C, 0x00 };
2364-
2365-
// skater_tumble, 32x20px
2366-
const unsigned char skater_tumble[] =
2367-
{0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x70, 0x00,
2368-
0x00, 0x00, 0x70, 0xe0, 0x00, 0x00, 0x71, 0xf0, 0x02, 0x00, 0x39, 0xf0, 0x07, 0xf8, 0x1d, 0xf0,
2369-
0x07, 0xfc, 0x1e, 0xe0, 0x00, 0xfe, 0x0f, 0x00, 0x00, 0x0e, 0x1f, 0x80, 0x00, 0x07, 0x3f, 0x80,
2370-
0x00, 0x37, 0xff, 0xc0, 0x00, 0xff, 0xfe, 0xe0, 0x01, 0xff, 0xfc, 0xe0, 0x03, 0xdf, 0xf8, 0xe0,
2371-
0x0f, 0x87, 0xf0, 0xe0, 0x0f, 0x01, 0xe0, 0xe0, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00};
2372-
2373-
// distance ran
2374-
int d, delta;
2375-
int cloud_1_y;
2376-
int d_jump, d_jump_t;
2377-
int d_run;
2378-
int d_tumble_t;
2379-
int ox;
2380-
// Really, don't ask
2381-
void play_game(){
2382-
d = 0;
2383-
delta = 0;
2384-
cloud_1_y = 2;
2385-
d_jump = 0;
2386-
d_jump_t = 0;
2387-
d_tumble_t = 0;
2388-
d_run = 0;
2389-
ox = 130;
2390-
2391-
while(true)
2392-
{
2393-
if (!d_run && isButtonPressed) {
2394-
//TODO: why can't I do this?
2395-
//while(isButtonPressed)
2396-
//{
2397-
// nrf_delay_ms(100);
2398-
//}
2399-
d_run = 1;
2400-
}
2401-
2402-
if (d_tumble_t && isButtonPressed) {
2403-
d = 0;
2404-
delta = 0;
2405-
cloud_1_y = 2;
2406-
d_jump = 0;
2407-
d_jump_t = 0;
2408-
d_tumble_t = 0;
2409-
d_run = 0;
2410-
ox = 130;
2411-
while(isButtonPressed)
2412-
{
2413-
nrf_delay_ms(100);
2414-
}
2415-
continue;
2416-
}
2417-
2418-
if (++frame>16000) frame = 0;
2419-
2420-
// increase distance whilst running
2421-
if (d_run && (++delta > 4)) {
2422-
delta = 0; ++d;
2423-
}
2424-
2425-
// obstacles
2426-
if (d_run) {
2427-
ox -= (frame%2)*(d/100) + 2;
2428-
if (ox < -15) ox += 140 + rand() % 60;
2429-
}
2430-
2431-
// jump!
2432-
if (!d_jump_t && isButtonPressed) {
2433-
d_jump_t = 1;
2434-
d_jump=5;
2435-
2436-
beep_speaker_blocking(40, 20);
2437-
2438-
} else if (d_jump_t) {
2439-
++d_jump_t;
2440-
2441-
if (d_jump_t<4) {
2442-
d_jump +=4;
2443-
} else if (d_jump_t<9) {
2444-
d_jump +=2;
2445-
} else if (d_jump_t<13) {
2446-
d_jump +=1;
2447-
} else if (d_jump_t == 16 || d_jump_t == 18) {
2448-
d_jump +=1;
2449-
} else if (d_jump_t == 20 || d_jump_t == 22) {
2450-
d_jump -=1;
2451-
} else if (d_jump_t>38) {
2452-
d_jump = 0;
2453-
d_jump_t = 0;
2454-
} else if (d_jump_t>32) {
2455-
d_jump -=4;
2456-
} else if (d_jump_t>29) {
2457-
d_jump -=2;
2458-
} else if (d_jump_t>25) {
2459-
d_jump -=1;
2460-
}
2461-
}
2462-
2463-
// hit detect
2464-
if (!d_tumble_t && ox > -10 && ox <10 && d_jump_t < 5) {
2465-
d_tumble_t = 1;
2466-
}
24672302

2468-
if (d_tumble_t) {
2469-
if (d_tumble_t == 1) {
2470-
beep_speaker_blocking(40,10);
2471-
} else if (d_tumble_t == 6) {
2472-
beep_speaker_blocking(200,20);
2473-
}
2474-
2475-
++d_tumble_t;
2476-
if (d_jump > -4) {
2477-
d_jump -= 1;
2478-
ox -= 1;
2479-
} else {
2480-
d_run = 0;
2481-
}
2482-
}
2483-
2484-
SSD1306_clearDisplay();
2485-
2486-
// hud
2487-
2488-
// score
2489-
sprintf(text,"%d",d);
2490-
Adafruit_GFX_setCursor(100, 0);
2491-
Adafruit_GFX_print(text);
2492-
2493-
// parallax clouds
2494-
Adafruit_GFX_drawBitmap(128 -(d%128),cloud_1_y,cloud_1,24,7,WHITE);
2495-
2496-
if (d%128 == 0) {
2497-
cloud_1_y = rand() % 10;
2498-
}
2499-
2500-
// terrain
2501-
if (d_jump > 4) {
2502-
Adafruit_GFX_drawLine(0,30,128,30,WHITE);
2503-
} else {
2504-
Adafruit_GFX_drawLine(0,30,3,30,WHITE);
2505-
Adafruit_GFX_drawLine(12,30,127,30,WHITE);
2506-
}
2507-
2508-
// obstacles
2509-
Adafruit_GFX_drawBitmap(ox,8,traffic_cone,24,24,WHITE);
2510-
2511-
2512-
// dino
2513-
int dy = 9-d_jump;
2514-
2515-
// tumbles!
2516-
if (d_tumble_t) {
2517-
Adafruit_GFX_drawBitmap(0,dy,skater_tumble,32,20,WHITE);
2518-
2519-
// runs!
2520-
} else {
2521-
//Adafruit_GFX_drawBitmap(0,dy,dino_top,24,18,WHITE);
2522-
2523-
// Run, Dino, Run!
2524-
if (d_run && !d_jump) {
2525-
if ((frame%8)/4) {
2526-
Adafruit_GFX_drawBitmap(0,dy,skate_push,24,23,WHITE);
2527-
} else {
2528-
Adafruit_GFX_drawBitmap(0,dy,skate_push2,24,23,WHITE);
2529-
}
2530-
} else if (d_run && d_jump) {
2531-
Adafruit_GFX_drawBitmap(0,dy,skate_jump,24,23,WHITE);
2532-
} else {
2533-
Adafruit_GFX_drawBitmap(0,dy,skate_push2,24,23,WHITE);
2534-
}
2535-
}
2536-
2537-
SSD1306_display(); //Game is allowed to update display when it wants
2538-
nrf_delay_ms(16);
2539-
}
2540-
}
2541-
//You didn't listen when I said don't ask, did you?
2542-
#endif
25432303

25442304

25452305
char ble_pin[7] = {0};
@@ -2616,7 +2376,7 @@ int main(void) {
26162376

26172377
// Initialize PWM for piezo output
26182378
buzzer_init();
2619-
beep_speaker_blocking(75,50); //Play tone blocking, allowing time for OLED to init
2379+
beep_speaker_blocking(75,10); //Play tone blocking, allowing time for OLED to init
26202380

26212381
// Init I2C for RTC and OLED
26222382
ret_code_t err_code = twi_master_init();

0 commit comments

Comments
 (0)