updated 7seg controls for new 7 seg boards
Dependencies: PixelArray WS2812 mbed
Fork of frdm_pong_table_controller by
Diff: main.cpp
- Revision:
- 2:ff409472bc08
- Parent:
- 1:24bc4d8ed2ae
- Child:
- 3:51604b5710f1
--- a/main.cpp Mon Mar 20 12:12:46 2017 +0000 +++ b/main.cpp Tue Jul 04 15:51:03 2017 +0000 @@ -2,7 +2,7 @@ #include "WS2812.h" #include "PixelArray.h" -#define WS2812_BUF 112 +#define WS2812_BUF 122 #define NUM_COLORS 6 #define NUM_LEDS_PER_COLOR 1 @@ -23,6 +23,8 @@ AnalogIn robotBreakBeam(A0); AnalogIn playerBreakBeam(A1); DigitalOut led_green(LED_GREEN, 1); +DigitalIn PB1(PTC6); +DigitalIn PB2(PTA4); // SERIAL Serial pc(USBTX, USBRX); // tx, rx @@ -30,21 +32,15 @@ // LED STRIPS // See the program page for information on the timing numbers // The given numbers are for the K64F -WS2812 robotScoreLED(D9, WS2812_BUF, 0, 5, 5, 0); -WS2812 playerScoreLED(D8,WS2812_BUF, 0, 5, 5, 0); -WS2812 robotGoalLED(D5,WS2812_BUF, 0, 5, 5, 0); -WS2812 playerGoalLED(D3,WS2812_BUF, 0, 5, 5, 0); +WS2812 robotScoreLED(D3, WS2812_BUF, 0, 5, 5, 0); +WS2812 playerScoreLED(D5,WS2812_BUF, 0, 5, 5, 0); // LED Variables bool seg1A, seg1B, seg1C, seg1D, seg1E, seg1F, seg1G; -int seg1Array[112]; +int seg1Array[122]; int mainArray[11][122]; PixelArray robotScorePx(WS2812_BUF); PixelArray playerScorePx(WS2812_BUF); -PixelArray robotGoalPx(WS2812_BUF); -PixelArray playerGoalPx(WS2812_BUF); -const int PLAYERGOALLEDS = 55; -const int ROBOTGOALLEDS = 55; // Score counters int robotScore; @@ -61,14 +57,13 @@ // FUNCTION DECLERATIONS void Write7Seg(int num); void SetLEDArray(int x); -void WriteRobotGoal(bool scored); -void WritePlayerGoal(bool scored); + void WriteScores(); void HandleGoal(bool hasRobotScored); void WritePxScores(int line_num, bool isRobot); void WritePxGoal(unsigned int colour,bool isRobot); void HandleWin(); -void WriteGoalsOff(); + void setup() @@ -78,14 +73,11 @@ led_green = 0; // Set brightness of the 4 LED strips - robotScoreLED.setII(0x19); + robotScoreLED.setII(0xB0); robotScoreLED.useII(WS2812::GLOBAL); - playerScoreLED.setII(0x19); + playerScoreLED.setII(0xB0); playerScoreLED.useII(WS2812::GLOBAL); - robotGoalLED.setII(0x19); - robotGoalLED.useII(WS2812::GLOBAL); - playerGoalLED.setII(0x80); - playerGoalLED.useII(WS2812::GLOBAL); + // Fills 2D array with data for(int i=0; i<10; i++) { @@ -99,14 +91,11 @@ // Set LEDS to start values WriteScores(); - wait(0.01); - WritePlayerGoal(true); - wait(0.01); - WriteRobotGoal(true); + - wait(5); + wait(1); - WriteGoalsOff(); + // Turn off green LED led_green = 1; } @@ -116,7 +105,7 @@ { switch (num) { case 0 : - // Off + // 0 seg1A = 1; seg1B = 1; seg1C = 1; @@ -275,67 +264,36 @@ void SetLEDArray(int x) { for (int i = 0; i < WS2812_BUF; i++) { - if (i < 16) { + if (i < 18) { mainArray[x][i] = seg1A; } - if ( i >= 16 && i < 32) { + if ( i >= 18 && i < 35) { mainArray[x][i] = seg1B; } - if (i >= 32 && i < 48) { + if (i >= 35 && i < 52) { mainArray[x][i] = seg1C; } - if (i >= 48 && i < 64) { + if (i >= 52 && i < 70) { mainArray[x][i]= seg1D; } - if ( i >= 64 && i < 80) { + if ( i >= 70 && i < 87) { mainArray[x][i] = seg1E; } - if (i >= 80 && i < 96) { + if (i >= 87 && i < 104) { mainArray[x][i] = seg1F; } - if ( i >= 96 && i < 112) { + if ( i >= 104 && i < 122) { mainArray[x][i] = seg1G; } }// FOR LOOP } -// Write to player 1 LED (ROBOT) -void WriteRobotGoal(bool scored) -{ - if(scored == true) { - WritePxGoal(BLUE,true); - } else { - WritePxGoal(PURPLE,true); - } - robotGoalLED.write(robotGoalPx.getBuf()); -} - -// Write to player 1 LED (ROBOT) -void WritePlayerGoal(bool scored) -{ - if(scored == true) { - WritePxGoal(BLUE,false); - } else { - WritePxGoal(PURPLE,false); - } - playerGoalLED.write(playerGoalPx.getBuf()); -} - -// Write OFF to both goals -void WriteGoalsOff() -{ - WritePxGoal(OFF,true); - robotGoalLED.write(robotGoalPx.getBuf()); - WritePxGoal(OFF,false); - playerGoalLED.write(playerGoalPx.getBuf()); -} - // Update both Score LEDs with the current score void WriteScores() { @@ -357,7 +315,7 @@ } if (mainArray[line_num][i] == 1) { - robotScorePx.Set(i,WHITE); + robotScorePx.Set(i,BLUE); } } } else { @@ -367,32 +325,13 @@ } if (mainArray[line_num][i] == 1) { - playerScorePx.Set(i,WHITE); + playerScorePx.Set(i,BLUE); } } } } -// Write Goal pixel array, using input colour for the specified goal -// INPUTS: colour, isRobot -// -// colour - This is the colour that you wish to set the array to -// isRobot - This specifies which goal to set the colours of, based on if it is the Robot side goal or not -// -void WritePxGoal(unsigned int colour,bool isRobot) -{ - if(isRobot == true) { - for (int i = 0; i < WS2812_BUF; i++) { - robotGoalPx.Set(i,colour); - } - } else { - for (int i = 0; i < WS2812_BUF; i++) { - playerGoalPx.Set(i,colour); - } - } -} - // Write the goal LEDs and score LEDs when a goal is recorded. Also check to see if the goal being scored // is the final point of the game and the game is over. // INPUTS: hasRobotScored @@ -403,28 +342,18 @@ { if(hasRobotScored == true) { robotScore++; - WriteRobotGoal(true); - wait(0.01); - WritePlayerGoal(false); - wait(0.01); WriteScores(); } else { playerScore++; - WriteRobotGoal(false); - wait(0.01); - WritePlayerGoal(true); - wait(0.01); WriteScores(); } - wait(5); + wait(1); // If either player reaches the score limit, then call HandleWin(). If not, then turn goal LEDs off if(robotScore == scoreLimit || playerScore == scoreLimit) { HandleWin(); - } else { - WriteGoalsOff(); - } + } } // Handle behaviour when either the player or robot has won a game. @@ -438,28 +367,16 @@ isRobotWinner = false; pc.printf("Player won!"); - // Animation for goal LEDs. Turns off the robot Goal LEDs, working from in the middle point outwards. - for(int i = 0; i < (ROBOTGOALLEDS / 2); i++) { - robotGoalPx.Set((ROBOTGOALLEDS /2) - i, OFF); - robotGoalPx.Set((ROBOTGOALLEDS /2) + i, OFF); - robotGoalLED.write(robotGoalPx.getBuf()); - wait(0.15); - } + - WriteGoalsOff(); + } else { pc.printf("Robot won!"); - // Animation for goal LEDs. Turns off the player Goal LEDs, working from in the middle point outwards. - for(int i = 0; i < (PLAYERGOALLEDS / 2); i++) { - playerGoalPx.Set((PLAYERGOALLEDS /2) - i, OFF); - playerGoalPx.Set((PLAYERGOALLEDS /2) + i, OFF); - playerGoalLED.write(playerGoalPx.getBuf()); - wait(0.25); - } + - WriteGoalsOff(); + } // Reset scores then write to LEDs @@ -477,13 +394,13 @@ double pbbValue = playerBreakBeam; // Read Player Break beam // IF PLAYER HAS SCORED A GOAL - if ((prevRbbValue - rbbValue)>0.03) { + if (((prevRbbValue - rbbValue)>0.03)|| (PB1==0)) { pc.printf("Player has scored a goal \n\r"); HandleGoal(false); } // IF ROBOT HAS SCORED A GOAL - if ((prevPbbValue - pbbValue) > 0.03) { + if (((prevPbbValue - pbbValue) > 0.03)|| (PB2==0)) { pc.printf("Robot has scored a goal \n\r"); HandleGoal(true); }