Sound update
Dependencies: 4DGL-uLCD-SE Physac-MBED PinDetect SDFileSystem mbed-rtos mbed
Revision 23:214c0c828d98, committed 19 months ago
- Comitter:
- jsanchez307
- Date:
- Wed Dec 07 23:12:12 2022 +0000
- Parent:
- 22:601e6f9077e4
- Child:
- 24:3b29ae4d6171
- Commit message:
- Tetris and Hockey updated for Nav_Switch myNav
Changed in this revision
--- a/globals.h Tue Dec 06 22:48:35 2022 +0000 +++ b/globals.h Wed Dec 07 23:12:12 2022 +0000 @@ -8,6 +8,8 @@ #include "bluefruit_controller.h" #include "Nav_Switch.h" +extern Nav_Switch myNav; + // Global state flags extern volatile bool game1; extern volatile bool game2;
--- a/hockey/hockey.cpp Tue Dec 06 22:48:35 2022 +0000
+++ b/hockey/hockey.cpp Wed Dec 07 23:12:12 2022 +0000
@@ -40,7 +40,6 @@
#define _HOCKEY_GOAL_BOT (_HOCKEY_GOAL_TOP + HOCKEY_GOAL_HEIGHT)
-
PhysicsBody puck;
PhysicsBody paddle_a;
PhysicsBody paddle_b;
@@ -335,20 +334,23 @@
SetPhysicsBodyRotation(paddle_a, paddle_a->orient + input_sensitivity * 0.05);
}
+
+
+
// Player 2 Input
- if (!(navSwitch & 0b00001)) { // Up
+ if (myNav.up()) { // Up
paddle_b->position.y -= input_sensitivity;
}
- if (!(navSwitch & 0b01000)) { // down
+ if (myNav.down()) { // down
paddle_b->position.y += input_sensitivity;
}
- if (!(navSwitch & 0b00100)) { // left
+ if (myNav.left()) { // left
paddle_b->position.x -= input_sensitivity;
}
- if (!(navSwitch & 0b10000)) { // right
+ if (myNav.right()) { // right
paddle_b->position.x += input_sensitivity;
}
- if (!(navSwitch & 0b00010)) { // rotate
+ if (myNav.fire()) { // rotate
SetPhysicsBodyRotation(paddle_b, paddle_b->orient + 3.14159 / 4);
}
--- a/main.cpp Tue Dec 06 22:48:35 2022 +0000
+++ b/main.cpp Wed Dec 07 23:12:12 2022 +0000
@@ -23,11 +23,12 @@
Mutex pc_mutex;
//uLCD_4DGL uLCD(p28, p27, p29);
-uLCD_4DGL uLCD(p9,p10,p30);
+uLCD_4DGL uLCD(p13,p14,p30);
Mutex uLCD_mutex;
-BluefruitController blue(p13,p14);
-BusIn navSwitch(p15, p16, p17, p19, p20);
+BluefruitController blue(p28,p27);
+//BusIn navSwitch(p15, p16, p17, p19, p20);
+Nav_Switch myNav(p20, p16, p17, p15, p19); // pins(up, down, left, right, fire)
SDFileSystem sd(p5, p6, p7, p8, "sd");
PwmOut Rgb(p23);
@@ -110,7 +111,7 @@
// Launch four threads
Thread thread1(tetrisGame, osPriorityHigh);
Thread thread2(hockeyGame, osPriorityHigh);
- Thread thread3(audioThread, osPriorityLow);
+ //Thread thread3(audioThread, osPriorityLow);
Thread thread4(ledThread, osPriorityLow);
// The main thread goes on to blink LEDs
@@ -123,10 +124,10 @@
PRINTF("[MAIN] Thread stacks: %lu %lu %lu %lu\r\n",
thread1.used_stack(),
thread2.used_stack(),
- thread3.used_stack(),
+ //thread3.used_stack(),
thread4.used_stack());
// Launch into the air hockey game
- if (game2 != true) game2 = true;
+ if (game1 != true) game1 = true;
}
}
--- a/tetris/Nav_Switch.h Tue Dec 06 22:48:35 2022 +0000
+++ b/tetris/Nav_Switch.h Wed Dec 07 23:12:12 2022 +0000
@@ -1,3 +1,6 @@
+#ifndef __NAV_SWITCH_H
+#define __NAV_SWITCH_H
+
#include "mbed.h"
class Nav_Switch
@@ -21,7 +24,7 @@
BusIn _pins;
};
-Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire):
+inline Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire):
_pins(up, down, left, right, fire)
{
_pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise
@@ -55,3 +58,5 @@
{
return _pins.read();
}
+
+#endif
\ No newline at end of file
--- a/tetris/main.cpp Tue Dec 06 22:48:35 2022 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,292 +0,0 @@
-#include "mbed.h"
-#include "uLCD_4DGL.h"
-#include "Game.h"
-#include "Nav_Switch.h"
-
-uLCD_4DGL uLCD(p13, p14, p30);
-Nav_Switch myNav(p19, p16, p17, p15, p18); // pins(up, down, left, right, fire)
-
-int SinglePiecePoints = 50;
-int LineClearPoints = 200;
-int key_input = 0;
-bool gameStarted = false;
-void input_left() // FIX
-{
- if(!gameStarted)
- { gameStarted=true;
- return;
- }
- key_input = 1;
-}
-
-void input_right()
-{
- if(!gameStarted)
- { gameStarted=true;
- return;
- }
- key_input = 2;
-}
-
-void input_rotate()
-{
- if(!gameStarted)
- { gameStarted=true;
- return;
- }
- key_input = 3;
-}
-
-void input_down()
-{
- if(!gameStarted)
- { gameStarted=true;
- return;
- }
- key_input=4;
-}
-
-void clear_board()
-{
- uLCD.filled_rectangle(20,0,79,128,0);
-}
-
-void clear_next_piece()
-{
- uLCD.filled_rectangle(92,20,122,50,0);
-}
-
-void UpdateGameStatus(int points) // used to include int lines
-{
- uLCD.locate(13,12);
- uLCD.printf("Score");
- uLCD.locate(13,13);
- uLCD.printf("%d",points);
-}
-
-
-/*int RandomGen(char range)
-{
- pc.printf("%c",range);
- while(!pc.readable()) wait(0.5);
- char buffer[4];
- pc.gets(buffer,4);
- int i = buffer[0]-'0';
- return i;
-}*/
-
-int main()
-{
- /*while(1){
- uLCD.printf("test");
- }*/
-
-
-
-
- /*if (myNav.up()) input_rotate();
- if (myNav.down()) input_down();;
- if (myNav.left()) input_left();
- if (myNav.right()) input_right();;
- //check mouse left button click*/ // FIXQ
- //if (myNav.fire()) input_down; // FIX, CAN ADD MENU FUNCTIONALITY
-
-
-
- uLCD.text_width(2);
- uLCD.text_height(2);
- uLCD.color(WHITE);
- uLCD.printf("TETRIS");
- wait(2);
-
- uLCD.baudrate(3000000);
-
-
- /*pc.baud(9600);
- pc.format(8,SerialBase::None,1);
- pc.printf("0");
- while(!pc.readable()) wait(0.5);
- char buffer[4];
- pc.gets(buffer,4);*/
-
-
-
-
-
-
- bool isGameOver = false;
- int mScreenHeight = 128;
- Pieces mPieces;
- Board mBoard (&mPieces, mScreenHeight);
- int a = 1; // RandomGen('a')
- int b = 2;
- int c = 3;
- int d = 4;
- Game mGame (&mBoard, &mPieces, mScreenHeight, &uLCD,a,b,c,d);
-
-
- // ----- Main Loop -----
- int prevX=0;
- int prevY=0;
- int prevPiece=-1;
- int prevRot=0;
- Timer timer;
- timer.start();
- key_input=0;
- bool needErase = false;
- uLCD.cls();
-
-
-
- int piece = 0;
- int rotate = 0;
-
- UpdateGameStatus(mGame.GetPoints());
-
- while (1)
- {
- if (myNav.up()) input_rotate();
- if (myNav.down()) input_down();;
- if (myNav.left()) input_left();
- if (myNav.right()) input_right();;
- //check mouse left button click // FIXQ
- //if (myNav.fire()) input_down; // FIX, CAN ADD MENU FUNCTIONALITY
-
- if(isGameOver)
- {
- wait(1);
- uLCD.cls();
- uLCD.text_width(2);
- uLCD.text_height(2);
- uLCD.printf("GAME OVER");
- while(1);
- }
- // ----- Draw ----
- if(needErase)
- {
- mGame.ErasePiece(prevX,prevY,prevPiece,prevRot);
- needErase=false;
- }
- mGame.DrawScene();
-
- prevX=mGame.mPosX;
- prevY=mGame.mPosY;
- prevPiece=mGame.mPiece;
- prevRot=mGame.mRotation;
-
- // ----- Input -----
- switch (key_input)
- {
- case (2): //right
- {
- if (mBoard.IsPossibleMovement (mGame.mPosX + 1, mGame.mPosY, mGame.mPiece, mGame.mRotation))
- {mGame.mPosX++;needErase=true;}
- break;
- }
-
- case (1): //left
- {
- if (mBoard.IsPossibleMovement (mGame.mPosX - 1, mGame.mPosY, mGame.mPiece, mGame.mRotation))
- {mGame.mPosX--;needErase=true;}
- break;
- }
-
- case (4)://down
- {
- // Check collision from up to down
- while (mBoard.IsPossibleMovement(mGame.mPosX, mGame.mPosY, mGame.mPiece, mGame.mRotation)) { mGame.mPosY++; }
- needErase=true;
- mBoard.StorePiece (mGame.mPosX, mGame.mPosY - 1, mGame.mPiece, mGame.mRotation);
- mGame.AddPoints(SinglePiecePoints);
- int linesDeleted = mBoard.DeletePossibleLines ();
- if(linesDeleted>0)
- {
- mGame.AddClearedLines(linesDeleted);
- mGame.AddPoints(LineClearPoints*linesDeleted);
- //Thread t1(FlashLight);
- //PlayClearSound();
- clear_board();
- }
- UpdateGameStatus(mGame.GetPoints()); // FIX
-
- if (mBoard.IsGameOver())
- {
- isGameOver=true;
- uLCD.cls();
-
- }
-
- if(!isGameOver)
- {
- mGame.CreateNewPiece(piece,rotate);
- clear_next_piece();
-
- piece++;
- rotate++;
-
- if (piece==7) piece = 0;
- if (rotate==4) rotate = 0;
- }
- break;
- }
-
- case (3)://rotate
- {
- if (mBoard.IsPossibleMovement (mGame.mPosX, mGame.mPosY, mGame.mPiece, (mGame.mRotation + 1) % 4))
- {mGame.mRotation = (mGame.mRotation + 1) % 4;needErase=true;}
- break;
- }
-
- case (0):{break;}
- }
- key_input = 0;
-
- // ----- Vertical movement -----
-
- if(timer.read_ms()>WAIT_TIME)
- {
- needErase=true;
- if(!isGameOver)
- {
- if (mBoard.IsPossibleMovement (mGame.mPosX, mGame.mPosY + 1, mGame.mPiece, mGame.mRotation))
- {
- mGame.mPosY++;
- }
- else
- {
- mBoard.StorePiece (mGame.mPosX, mGame.mPosY, mGame.mPiece, mGame.mRotation);
- mGame.AddPoints(SinglePiecePoints);
- int linesDeleted = mBoard.DeletePossibleLines ();
- if(linesDeleted>0)
- {
- mGame.AddClearedLines(linesDeleted);
- mGame.AddPoints(LineClearPoints*linesDeleted);
- //Thread t1(FlashLight);
- //PlayClearSound();
- clear_board();
- }
- UpdateGameStatus(mGame.GetPoints());
-
- if (mBoard.IsGameOver())
- {
- isGameOver=true;
- uLCD.cls();
- }
-
- if(!isGameOver)
- {
- mGame.CreateNewPiece(piece,rotate);
- clear_next_piece(); // 0-6 Pieces, 0-3 rotations^^
- piece++;
- rotate++;
-
- if (piece==7) piece = 0;
- if (rotate==4) rotate = 0;
- }
- }
- }
- timer.reset();
- }
- wait(0.1);
- }
-}
--- a/tetris/tetris.cpp Tue Dec 06 22:48:35 2022 +0000
+++ b/tetris/tetris.cpp Wed Dec 07 23:12:12 2022 +0000
@@ -1,10 +1,11 @@
#include "mbed.h"
-#include "uLCD_4DGL.h"
+//#include "uLCD_4DGL.h"
#include "Game.h"
-#include "Nav_Switch.h"
+#include "globals.h"
+//#include "Nav_Switch.h"
-uLCD_4DGL uLCD(p13, p14, p30);
-Nav_Switch myNav(p19, p16, p17, p15, p18); // pins(up, down, left, right, fire)
+//uLCD_4DGL uLCD(p13, p14, p30);
+//Nav_Switch myNav(p20, p16, p17, p15, p19); // pins(up, down, left, right, fire)
int SinglePiecePoints = 50;
int LineClearPoints = 200;
@@ -77,13 +78,19 @@
void tetrisGame()
{
+ PRINTF("POINT 1");
+ while (game1 == false) {
+
+ PRINTF("[TETRIS] Idle\r\n");
+ Thread::wait(500);
+ }
+
/*while(1){
uLCD.printf("test");
}*/
-
/*if (myNav.up()) input_rotate();
if (myNav.down()) input_down();;
if (myNav.left()) input_left();
@@ -110,10 +117,6 @@
pc.gets(buffer,4);*/
-
-
-
-
bool isGameOver = false;
int mScreenHeight = 128;
Pieces mPieces;
@@ -137,7 +140,6 @@
uLCD.cls();
-
int piece = 0;
int rotate = 0;
@@ -145,11 +147,18 @@
while (1)
{
+ PRINTF("POINT 2");
+ // Wait until the game starts
+ while (game1 == false) {
+
+ PRINTF("[TETRIS] Idle\r\n");
+ Thread::wait(500);
+ }
+
if (myNav.up()) input_rotate();
if (myNav.down()) input_down();;
if (myNav.left()) input_left();
if (myNav.right()) input_right();;
- //check mouse left button click // FIXQ
//if (myNav.fire()) input_down; // FIX, CAN ADD MENU FUNCTIONALITY
if(isGameOver)