Craig Evans
/
ELEC2645_Pong2021
Pong game for ELEC1620 board.
Diff: main.cpp
- Revision:
- 1:d63a63f0d397
- Parent:
- 0:be41a15e7a86
- Child:
- 2:482d74ef09c8
--- a/main.cpp Fri Dec 11 12:25:25 2020 +0000 +++ b/main.cpp Fri Mar 05 16:58:05 2021 +0000 @@ -1,40 +1,69 @@ -/* mbed Microcontroller Library - * Copyright (c) 2019 ARM Limited - * SPDX-License-Identifier: Apache-2.0 - */ +///////////// includes ///////////////////// #include "mbed.h" #include "platform/mbed_thread.h" #include "Joystick.h" #include "N5110.h" - - -//VCC,SCE,RST,D/C,MOSI,SCLK,LED +#include "ShiftReg.h" +#include "PongEngine.h" +#include "Utils.h" +///////////// defines ///////////////////// +#define PADDLE_WIDTH 2 +#define PADDLE_HEIGHT 8 +#define BALL_SIZE 2 +#define BALL_SPEED 3 +///////////// objects /////////////////// N5110 lcd(p14,p8,p9,p10,p11,p13,p21); +Joystick joystick(p20,p19); +DigitalIn buttonA(p29); +BusOut leds(LED4,LED3,LED2,LED1); +ShiftReg seven_seg; +PongEngine pong; +///////////// prototypes /////////////// +void init(); +void render(); +void welcome(); +//////////////////////////////////////// -// y x -Joystick joystick(p20,p19); +int main() { + init(); // initialise devices and objects + welcome(); // waiting for the user to start + render(); // first draw the initial frame + thread_sleep_for(500); // and wait for one frame period (100 ms - 10 fps + + while (1) { + // read the joystick input and store in a struct + UserInput input = {joystick.get_direction(),joystick.get_mag()}; + pong.update(input); // update the game engine based on input + render(); // draw frame on screen + thread_sleep_for(500); // 100 ms - 10 f.p.s + } +} -int main() -{ - // initialise the LCD and joystick +void init() { + seven_seg.write(0x00); // turn of 7-seg display lcd.init(); lcd.setContrast(0.5); joystick.init(); - - while (1) { - // read the joystick to get the x- and y- values - Vector2D coord = joystick.get_mapped_coord(); - printf("Coord = %f | %f\n",coord.x,coord.y); - - lcd.clear(); // clear buffer at the start of the loop - char buffer[14]={0}; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) - sprintf(buffer,"x = %.3f",coord.x); // print formatted data to buffer - lcd.printString(buffer,0,2); // display on screen - sprintf(buffer,"y = %.3f",coord.y); // print formatted data to buffer - lcd.printString(buffer,0,3); // display on screen - lcd.refresh(); // need to fresh the screen to get the message to appear - - thread_sleep_for(200); + pong.init(2,8,2,2,2); // paddle x position, paddle_height,paddle_width,ball_size,speed +} + +void render() { // clear screen, re-draw and refresh + lcd.clear(); + pong.draw(lcd); + lcd.refresh(); +} + +void welcome() { // splash screen + lcd.printString(" Pong! ",0,1); + lcd.printString(" Press A ",0,4); + lcd.refresh(); + + // wait flashing LEDs until button A is pressed + while ( buttonA.read() == 0) { + leds = 0b1111; + thread_sleep_for(100); + leds = 0b0000; + thread_sleep_for(100); } -} +} \ No newline at end of file