My ELEC2645 joystick project Tetris Game NAME: JIANWEI CHEN SID: 200879849

Dependencies:   N5110 SDFileSystem mbed

Committer:
cjw851102
Date:
Thu May 05 09:18:01 2016 +0000
Revision:
4:463abe5f5135
Final version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cjw851102 4:463abe5f5135 1 /**
cjw851102 4:463abe5f5135 2 @file main.h
cjw851102 4:463abe5f5135 3 @brief Header file containing functions prototypes, defines and global variables for menu and pointer
cjw851102 4:463abe5f5135 4 @brief Menu is construct using finit state mechine.
cjw851102 4:463abe5f5135 5 @brief The input for finit state mechine is the positer position.
cjw851102 4:463abe5f5135 6 @brief Revision 1.0.
cjw851102 4:463abe5f5135 7 @author JIANWEI CHEN
cjw851102 4:463abe5f5135 8 @date May 2016
cjw851102 4:463abe5f5135 9 */
cjw851102 4:463abe5f5135 10 #ifndef MAIN_H
cjw851102 4:463abe5f5135 11 #define MAIN_H
cjw851102 4:463abe5f5135 12
cjw851102 4:463abe5f5135 13 #include "mbed.h"
cjw851102 4:463abe5f5135 14 // VCC, SCE, RST, D/C, MOSI,SCLK, LED
cjw851102 4:463abe5f5135 15 N5110 lcd(PTE26,PTA0,PTC4,PTD0,PTD2,PTD1,PTC3);
cjw851102 4:463abe5f5135 16 // MOSI, MISO, SCK, CS
cjw851102 4:463abe5f5135 17 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
cjw851102 4:463abe5f5135 18
cjw851102 4:463abe5f5135 19 FILE *fp; /*!< File pointer */
cjw851102 4:463abe5f5135 20
cjw851102 4:463abe5f5135 21 DigitalOut red_led(PTA1);
cjw851102 4:463abe5f5135 22 DigitalOut green_led(PTC2);
cjw851102 4:463abe5f5135 23 InterruptIn button(PTB18);
cjw851102 4:463abe5f5135 24 PwmOut buzzer (PTA2);
cjw851102 4:463abe5f5135 25
cjw851102 4:463abe5f5135 26 ////////////////////////////////////////////////////////
cjw851102 4:463abe5f5135 27 // Variables
cjw851102 4:463abe5f5135 28 ////////////////////////////////////////////////////////
cjw851102 4:463abe5f5135 29 /**
cjw851102 4:463abe5f5135 30 Create ticker object for menu
cjw851102 4:463abe5f5135 31 @brief ticker to control the speed of updating the menu
cjw851102 4:463abe5f5135 32 */
cjw851102 4:463abe5f5135 33 Ticker menuTimer;
cjw851102 4:463abe5f5135 34
cjw851102 4:463abe5f5135 35 volatile int g_menuTimer_flag = 0; /*!< Flag for menu ticker */
cjw851102 4:463abe5f5135 36 volatile int g_button_flag = 0; /*!< Flag for button interrupt */
cjw851102 4:463abe5f5135 37
cjw851102 4:463abe5f5135 38 /**State of menu
cjw851102 4:463abe5f5135 39 @brief Variable control the state of menu
cjw851102 4:463abe5f5135 40 @note state 0 - main menu
cjw851102 4:463abe5f5135 41 @note state 1 - game level choosing menu
cjw851102 4:463abe5f5135 42 @note state 2 - show highest score menu
cjw851102 4:463abe5f5135 43 @note state 3 - game with easy level
cjw851102 4:463abe5f5135 44 @note state 4 - game with hard level
cjw851102 4:463abe5f5135 45 */
cjw851102 4:463abe5f5135 46 int state=0;
cjw851102 4:463abe5f5135 47
cjw851102 4:463abe5f5135 48 int pointer_position=0;/*!< Variable control the position of pointer */
cjw851102 4:463abe5f5135 49
cjw851102 4:463abe5f5135 50 /**struct for state
cjw851102 4:463abe5f5135 51 */
cjw851102 4:463abe5f5135 52 struct State {
cjw851102 4:463abe5f5135 53 int nextState[3]; // array of next states
cjw851102 4:463abe5f5135 54 };
cjw851102 4:463abe5f5135 55 typedef const struct State STyp;
cjw851102 4:463abe5f5135 56
cjw851102 4:463abe5f5135 57 STyp fsm[5] = {
cjw851102 4:463abe5f5135 58 {1,2,0}, // states chosen according to inputs(pointer position)
cjw851102 4:463abe5f5135 59 {3,4,0},
cjw851102 4:463abe5f5135 60 {0,0,0},
cjw851102 4:463abe5f5135 61 {0,0,0},
cjw851102 4:463abe5f5135 62 {0,0,0}
cjw851102 4:463abe5f5135 63 };
cjw851102 4:463abe5f5135 64
cjw851102 4:463abe5f5135 65 //////////////////////////////////////////////////////////////////
cjw851102 4:463abe5f5135 66 // Functions
cjw851102 4:463abe5f5135 67 //////////////////////////////////////////////////////////////////
cjw851102 4:463abe5f5135 68
cjw851102 4:463abe5f5135 69 /**
cjw851102 4:463abe5f5135 70 Menu isr function
cjw851102 4:463abe5f5135 71 */
cjw851102 4:463abe5f5135 72 void menuTimer_isr();
cjw851102 4:463abe5f5135 73
cjw851102 4:463abe5f5135 74 /**
cjw851102 4:463abe5f5135 75 Initialise the program and lcd, mbed initial settings
cjw851102 4:463abe5f5135 76 */
cjw851102 4:463abe5f5135 77 void init();
cjw851102 4:463abe5f5135 78
cjw851102 4:463abe5f5135 79 /**
cjw851102 4:463abe5f5135 80 Button interrupt isr function
cjw851102 4:463abe5f5135 81 */
cjw851102 4:463abe5f5135 82 void button_isr();
cjw851102 4:463abe5f5135 83
cjw851102 4:463abe5f5135 84 /**Drawing the pointer at (x,y)
cjw851102 4:463abe5f5135 85 @brief Drawing the pointer image at (x,y) according to inputs
cjw851102 4:463abe5f5135 86 @param x - x co-ordinate of lcd
cjw851102 4:463abe5f5135 87 @param y - y co-ordinate of lcd
cjw851102 4:463abe5f5135 88 @param fill - fill = 0 white, fill = 1 black
cjw851102 4:463abe5f5135 89 */
cjw851102 4:463abe5f5135 90 void pointer_image(int x,int y,int fill);//0-white 1-black
cjw851102 4:463abe5f5135 91
cjw851102 4:463abe5f5135 92 /** Move the pointer
cjw851102 4:463abe5f5135 93 @brief Function to control the pointer according to joystick
cjw851102 4:463abe5f5135 94 */
cjw851102 4:463abe5f5135 95 void pointer();
cjw851102 4:463abe5f5135 96
cjw851102 4:463abe5f5135 97
cjw851102 4:463abe5f5135 98 /////////////////////////////////////////////////
cjw851102 4:463abe5f5135 99 /// Function Definitions
cjw851102 4:463abe5f5135 100 /////////////////////////////////////////////////
cjw851102 4:463abe5f5135 101 void menuTimer_isr()
cjw851102 4:463abe5f5135 102 {
cjw851102 4:463abe5f5135 103 g_menuTimer_flag = 1;
cjw851102 4:463abe5f5135 104 }
cjw851102 4:463abe5f5135 105
cjw851102 4:463abe5f5135 106
cjw851102 4:463abe5f5135 107 void button_isr()
cjw851102 4:463abe5f5135 108 {
cjw851102 4:463abe5f5135 109 g_button_flag=1;
cjw851102 4:463abe5f5135 110 }
cjw851102 4:463abe5f5135 111
cjw851102 4:463abe5f5135 112
cjw851102 4:463abe5f5135 113 void pointer()
cjw851102 4:463abe5f5135 114 {
cjw851102 4:463abe5f5135 115 updateJoystick(); // get joystick direction
cjw851102 4:463abe5f5135 116 if (joystick.direction==UP) {
cjw851102 4:463abe5f5135 117 if(pointer_position==0) { // pointer at the top
cjw851102 4:463abe5f5135 118 pointer_position=0;
cjw851102 4:463abe5f5135 119 } else {
cjw851102 4:463abe5f5135 120 pointer_position--;
cjw851102 4:463abe5f5135 121 }
cjw851102 4:463abe5f5135 122 } else if(joystick.direction==DOWN) {
cjw851102 4:463abe5f5135 123 if(pointer_position==1&&(state!=1)) { //only game level choosing menu have 3 pointer positions
cjw851102 4:463abe5f5135 124 pointer_position=1;
cjw851102 4:463abe5f5135 125 } else if (pointer_position==2&&(state==1)) {
cjw851102 4:463abe5f5135 126 pointer_position=2;
cjw851102 4:463abe5f5135 127 } else {
cjw851102 4:463abe5f5135 128 pointer_position++;
cjw851102 4:463abe5f5135 129 }
cjw851102 4:463abe5f5135 130 }
cjw851102 4:463abe5f5135 131
cjw851102 4:463abe5f5135 132 if(pointer_position==0) {
cjw851102 4:463abe5f5135 133 pointer_image(10,27,1); // draw pointer
cjw851102 4:463abe5f5135 134 pointer_image(10,35,0); // clear pointer at other positions
cjw851102 4:463abe5f5135 135 pointer_image(10,43,0);
cjw851102 4:463abe5f5135 136 } else if(pointer_position==1) {
cjw851102 4:463abe5f5135 137 pointer_image(10,27,0);
cjw851102 4:463abe5f5135 138 pointer_image(10,35,1);
cjw851102 4:463abe5f5135 139 pointer_image(10,43,0);
cjw851102 4:463abe5f5135 140 } else if(pointer_position==2) {
cjw851102 4:463abe5f5135 141 pointer_image(10,35,0);
cjw851102 4:463abe5f5135 142 pointer_image(10,43,1);
cjw851102 4:463abe5f5135 143 }
cjw851102 4:463abe5f5135 144
cjw851102 4:463abe5f5135 145 }
cjw851102 4:463abe5f5135 146
cjw851102 4:463abe5f5135 147
cjw851102 4:463abe5f5135 148 void pointer_image(int x,int y,int fill)
cjw851102 4:463abe5f5135 149 {
cjw851102 4:463abe5f5135 150 if(fill==0) {// draw white pointer
cjw851102 4:463abe5f5135 151 lcd.drawLine(x,y,x-4,y+2,0);
cjw851102 4:463abe5f5135 152 lcd.drawLine(x,y,x-4,y-2,0);
cjw851102 4:463abe5f5135 153 } else { // draw black pointer
cjw851102 4:463abe5f5135 154 lcd.drawLine(x,y,x-4,y-2,1);
cjw851102 4:463abe5f5135 155 lcd.drawLine(x,y,x-4,y+2,1);
cjw851102 4:463abe5f5135 156 }
cjw851102 4:463abe5f5135 157 }
cjw851102 4:463abe5f5135 158
cjw851102 4:463abe5f5135 159
cjw851102 4:463abe5f5135 160 void init(){
cjw851102 4:463abe5f5135 161 wait(2.0); // short delay for power to settle
cjw851102 4:463abe5f5135 162 // initialise sd card
cjw851102 4:463abe5f5135 163 fp = fopen("/sd/topscore.txt", "r");
cjw851102 4:463abe5f5135 164 fclose(fp); // ensure you close the file after reading
cjw851102 4:463abe5f5135 165 lcd.init(); // initialise lcd
cjw851102 4:463abe5f5135 166 lcd.normalMode(); // normal colour mode
cjw851102 4:463abe5f5135 167 lcd.setBrightness(1.0); // put LED backlight on 100%
cjw851102 4:463abe5f5135 168
cjw851102 4:463abe5f5135 169 lcd.printString("Calibrating",0,0);
cjw851102 4:463abe5f5135 170 lcd.printString("Joystick........",0,1);
cjw851102 4:463abe5f5135 171 lcd.printString("PLEASE DO NOT",0,3);
cjw851102 4:463abe5f5135 172 lcd.printString("MOVE JOYSTICK.....",0,4);
cjw851102 4:463abe5f5135 173 lcd.refresh();
cjw851102 4:463abe5f5135 174 calibrateJoystick();
cjw851102 4:463abe5f5135 175 wait(4.0);
cjw851102 4:463abe5f5135 176
cjw851102 4:463abe5f5135 177 lcd.clear();
cjw851102 4:463abe5f5135 178 lcd.refresh();
cjw851102 4:463abe5f5135 179 button.mode(PullDown);
cjw851102 4:463abe5f5135 180 red_led=0;
cjw851102 4:463abe5f5135 181 green_led=0;
cjw851102 4:463abe5f5135 182 button.fall(&button_isr);
cjw851102 4:463abe5f5135 183 menuTimer.attach(&menuTimer_isr,0.1); // attach menu ticker
cjw851102 4:463abe5f5135 184 buzzer.period(1.0/4000.0); // set buzzer period to 4 kKz
cjw851102 4:463abe5f5135 185 }
cjw851102 4:463abe5f5135 186 #endif