Sound update
Dependencies: 4DGL-uLCD-SE Physac-MBED PinDetect SDFileSystem mbed-rtos mbed
Revision 20:73e4f12277bd, committed 19 months ago
- Comitter:
- jstephens78
- Date:
- Mon Dec 05 20:51:36 2022 +0000
- Parent:
- 19:6d9bee043ede
- Child:
- 21:df9e8f45e14c
- Commit message:
- All four threads launch, air hockey is the default
Changed in this revision
--- a/Physac-MBED.lib Sun Dec 04 23:31:28 2022 +0000 +++ b/Physac-MBED.lib Mon Dec 05 20:51:36 2022 +0000 @@ -1,1 +1,1 @@ -https://os.mbed.com/users/jstephens78/code/Physac-MBED/#e39efa4f4f58 +https://os.mbed.com/users/jstephens78/code/Physac-MBED/#ebc0214989c0
--- a/globals.h Sun Dec 04 23:31:28 2022 +0000 +++ b/globals.h Mon Dec 05 20:51:36 2022 +0000 @@ -7,13 +7,17 @@ #include "uLCD_4DGL.h" #include "bluefruit_controller.h" - - +// Global state flags extern volatile bool game1; extern volatile bool game2; // Debug systems extern Serial pc; +extern Mutex pc_mutex; +#define PRINTF(...) \ + pc_mutex.lock(); \ + pc.printf(__VA_ARGS__); \ + pc_mutex.unlock(); // Display extern uLCD_4DGL uLCD;
--- a/hockey/hockey.cpp Sun Dec 04 23:31:28 2022 +0000
+++ b/hockey/hockey.cpp Mon Dec 05 20:51:36 2022 +0000
@@ -1,22 +1,20 @@
#include "hockey.h"
#include "globals.h"
+// Custom memory allocation methods
#define PHYSAC_NO_THREADS
#define PHYSAC_STANDALONE
#define PHYSAC_IMPLEMENTATION
#define _STDBOOL_H
#include "physac.h"
-#define SCREEN_WIDTH 128
-#define SCREEN_HEIGHT 128
-
// Controls size of gamepieces in the hockey arena. This influences both
// rendering and collision, so be careful adjusting dimensions too small
#define HOCKEY_PUCK_RADIUS 4
#define HOCKEY_PADDLE_W 8
#define HOCKEY_PADDLE_H 24
-// Physics sim properties.
+// Physics sim properties
// HOCKEY_PHYSICS_STEPS number of physics ticks between frames
// HOCKEY_PHYSICS_DELTA how many ms the physics engine believes pass per
// update. This is arbitrary. Pick what behaves well.
@@ -25,6 +23,10 @@
#define HOCKEY_PHYSICS_DELTA 1.66
#define HOCKEY_PUCK_VELOCITY (5.0f / HOCKEY_PHYSICS_STEPS)
+// Screen-space constants:
+#define SCREEN_WIDTH 128
+#define SCREEN_HEIGHT 128
+
// Controls the dimensions of the arena. You can adjust the screen margin above
// and below the court, and the width of the goal.
#define HOCKEY_ARENA_TOP 16
@@ -366,15 +368,14 @@
// Set up Physac objects & simulation
initPhysicsObjects();
- while (true) {
-
+ while (true) {
// Wait until the game starts
while (game2 == false) {
- printf("Waiting thread 2\r\n");
- Thread::wait(100);
- game2 = true;
+
+ PRINTF("[HOCKEY] Idle\r\n");
+ Thread::wait(500);
}
-
+
// Reset game state
red_score = 0;
blue_score = 0;
@@ -406,7 +407,8 @@
// Render the game
drawDynamicObjects();
- Thread::yield();
+ PRINTF("[HOCKEY] Delta %f\r\n", dt);
+ Thread::wait(100);
}
}
--- a/main.cpp Sun Dec 04 23:31:28 2022 +0000
+++ b/main.cpp Mon Dec 05 20:51:36 2022 +0000
@@ -3,12 +3,25 @@
#include "globals.h"
#include "hockey.h"
+#include "tetris.h"
//#include "Speaker.h"
#include "SDFileSystem.h"
#include "uLCD_4DGL.h"
+
+
+///////////////////////////
+// GLOBALS.H DEFINITIONS //
+///////////////////////////
+// The following variables are declared as extern in "globals.h", and we define
+// them here:
+volatile bool game1 = false;
+volatile bool game2 = false;
+
Serial pc(USBTX, USBRX);
+Mutex pc_mutex;
+
//uLCD_4DGL uLCD(p28, p27, p29);
uLCD_4DGL uLCD(p9,p10,p30);
Mutex uLCD_mutex;
@@ -16,22 +29,32 @@
BluefruitController blue(p13,p14);
BusIn navSwitch(p15, p16, p17, p18, p19);
-SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
-
-DigitalOut myled(LED1);
-//AnalogOut DACout(p20);
+SDFileSystem sd(p5, p6, p7, p8, "sd");
PwmOut Rgb(p23);
PwmOut rGb(p24);
PwmOut rgB(p25);
PwmOut PWMout(p21);
+//AnalogOut DACout(p20);
//wave_player waver(&DACout);
-volatile bool game1 = false;
-volatile bool game2 = false;
-/*
-void thread3(void const *argument)
+
+////////////////////////////////
+// MAIN.CPP LOCAL DEFINITIONS //
+////////////////////////////////
+// The following variables are *not* from "globals.h" and are not meant for
+// reference outside of main.cpp
+DigitalOut myled(LED1);
+
+
+
+//////////////////////
+// MAIN.CPP THREADS //
+//////////////////////
+void audioThread()
{
+ while(true) Thread::wait(1000);
+ /*
FILE *wave_file;
PWMout.period(1.0/400000.0);
while (game1 == false && game2 == false) {
@@ -49,12 +72,11 @@
waver.play(wave_file);
fclose(wave_file);
}
+ */
}
-*/
-void thread4(void const *argument)
+void ledThread()
{
- /*
float x = 0.0;
while(1) {
//get a new random number for PWM
@@ -75,21 +97,30 @@
Thread::wait(4000.0 * rand() / float(RAND_MAX));
}
}
- */
}
int main()
{
- printf("Starting thread 2\r\n");
- game2 = true;
- Thread thread2(hockeyGame);
- //Thread thread3(thread3);
-// Thread thread4(thread4);
+ // Launch four threads
+ Thread thread1(tetrisGame, osPriorityHigh);
+ Thread thread2(hockeyGame, osPriorityHigh);
+ Thread thread3(audioThread, osPriorityLow);
+ Thread thread4(ledThread, osPriorityLow);
+ // The main thread goes on to blink LEDs
while (true) {
myled = 1;
Thread::wait(500);
myled = 0;
Thread::wait(500);
+
+ PRINTF("[MAIN] Thread stacks: %lu %lu %lu %lu\r\n",
+ thread1.used_stack(),
+ thread2.used_stack(),
+ thread3.used_stack(),
+ thread4.used_stack());
+
+ // Launch into the air hockey game
+ if (game2 != true) game2 = true;
}
}
--- a/tetris/tetris.cpp Sun Dec 04 23:31:28 2022 +0000
+++ b/tetris/tetris.cpp Mon Dec 05 20:51:36 2022 +0000
@@ -290,6 +290,10 @@
DOWN_KEY.setSampleFrequency(); // FIX
uLCD.baudrate(3000000);
+ while (true) {
+ Thread::wait(200);
+ }
+
//myShiftbrite.RGB(0,0,0);
//myShiftbrite.RGB(0,0,0);