ELEC2645 (2018/19) / Mbed 2 deprecated el17st

Dependencies:   mbed FATFileSystem

Committer:
rottenegg
Date:
Sun Apr 21 00:41:35 2019 +0000
Revision:
5:860087ff295e
Parent:
4:34bf3587cf42
Child:
6:6f84347e9e14
CaMove Library Update, CaMove Documentation Done, WDplayer Documentation Update.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rottenegg 4:34bf3587cf42 1 #ifndef CAMOVE_H
rottenegg 4:34bf3587cf42 2 #define CAMOVE_H
rottenegg 4:34bf3587cf42 3
rottenegg 5:860087ff295e 4 /** CaMove Class
rottenegg 5:860087ff295e 5 @brief Class for Charater Animation and Movement Interation.
rottenegg 5:860087ff295e 6 @brief Controls Interrupts for Interation Button and The Joystick for Charater Movement
rottenegg 5:860087ff295e 7 @brief Uses the FilePaths.h to supply File Location Information and uses Bitmap.h to create animations on the LCD
rottenegg 5:860087ff295e 8
rottenegg 5:860087ff295e 9 @author Saad Tayyab
rottenegg 5:860087ff295e 10 @date 2th April 2019
rottenegg 5:860087ff295e 11
rottenegg 5:860087ff295e 12 @code
rottenegg 5:860087ff295e 13 //Required Libraries
rottenegg 5:860087ff295e 14 #include "mbed.h"
rottenegg 5:860087ff295e 15 #include "CaMove.h"
rottenegg 5:860087ff295e 16 #include "N5110.h"
rottenegg 5:860087ff295e 17 #include "SDFileSystem.h"
rottenegg 5:860087ff295e 18
rottenegg 5:860087ff295e 19 //Define SD Card
rottenegg 5:860087ff295e 20 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
rottenegg 5:860087ff295e 21
rottenegg 5:860087ff295e 22 //Define LCD
rottenegg 5:860087ff295e 23 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
rottenegg 5:860087ff295e 24
rottenegg 5:860087ff295e 25 //Define CaMove
rottenegg 5:860087ff295e 26 CaMove CM(PTB9,PTB11,PTB10);
rottenegg 5:860087ff295e 27
rottenegg 5:860087ff295e 28 //A mini game instance where charater can click two black boxes in either corner
rottenegg 5:860087ff295e 29 //depending on clicked box the charater will either enter a new instance where he
rottenegg 5:860087ff295e 30 //can leave the screen and when he does the program ends or the instance where the
rottenegg 5:860087ff295e 31 //screen fades to black. The program ends with a circle on sceen.
rottenegg 5:860087ff295e 32
rottenegg 5:860087ff295e 33 int main() {
rottenegg 5:860087ff295e 34 //Initalize and Set Spawn Values
rottenegg 5:860087ff295e 35 CM.init(40,20,Fd);
rottenegg 5:860087ff295e 36 lcd.init();
rottenegg 5:860087ff295e 37
rottenegg 5:860087ff295e 38 //Setting Interative Regions
rottenegg 5:860087ff295e 39 CM.set_region(6,6,8,8);
rottenegg 5:860087ff295e 40 CM.set_region(60,30,8,8);
rottenegg 5:860087ff295e 41
rottenegg 5:860087ff295e 42 //While Loop that ends when Interative Region triggered
rottenegg 5:860087ff295e 43 while(!CM.is_trg()) {
rottenegg 5:860087ff295e 44 lcd.clear();
rottenegg 5:860087ff295e 45 lcd.drawRect(2,2,70,40,FILL_TRANSPARENT);
rottenegg 5:860087ff295e 46 lcd.drawRect(3,3,68,38,FILL_TRANSPARENT);
rottenegg 5:860087ff295e 47 lcd.drawRect(4,4,8,8,FILL_BLACK);
rottenegg 5:860087ff295e 48 lcd.drawRect(62,32,8,8,FILL_BLACK);
rottenegg 5:860087ff295e 49
rottenegg 5:860087ff295e 50 //Move the Charater and Update Movement Animation
rottenegg 5:860087ff295e 51 CM.move(lcd);
rottenegg 5:860087ff295e 52 lcd.refresh();
rottenegg 5:860087ff295e 53 wait(0.1);
rottenegg 5:860087ff295e 54 }
rottenegg 5:860087ff295e 55 lcd.clear();
rottenegg 5:860087ff295e 56 //Interative Region 1 Triggered
rottenegg 5:860087ff295e 57 if (CM.get_treg() == 0) {
rottenegg 5:860087ff295e 58
rottenegg 5:860087ff295e 59 //Delete Interartive Regions
rottenegg 5:860087ff295e 60 CM.delete_regions();
rottenegg 5:860087ff295e 61 CM.init(40,20,Bd);
rottenegg 5:860087ff295e 62
rottenegg 5:860087ff295e 63 //While Loop Dependent on if Charater is In Screen
rottenegg 5:860087ff295e 64 while(CM.in_screen()) {
rottenegg 5:860087ff295e 65 lcd.clear();
rottenegg 5:860087ff295e 66 CM.move(lcd);
rottenegg 5:860087ff295e 67 lcd.refresh();
rottenegg 5:860087ff295e 68 wait(0.2);
rottenegg 5:860087ff295e 69 }
rottenegg 5:860087ff295e 70 }
rottenegg 5:860087ff295e 71
rottenegg 5:860087ff295e 72 //Interative Region 2 Triggered
rottenegg 5:860087ff295e 73 if (CM.get_treg() == 1) {
rottenegg 5:860087ff295e 74 for(int i = 0; i < 84; i++) {
rottenegg 5:860087ff295e 75 lcd.drawRect(0,0,i,48,FILL_BLACK);
rottenegg 5:860087ff295e 76 lcd.refresh();
rottenegg 5:860087ff295e 77 wait(0.05);
rottenegg 5:860087ff295e 78 }
rottenegg 5:860087ff295e 79 }
rottenegg 5:860087ff295e 80 //Ending Screen
rottenegg 5:860087ff295e 81 lcd.clear();
rottenegg 5:860087ff295e 82 lcd.drawCircle(40,20,5,FILL_TRANSPARENT);
rottenegg 5:860087ff295e 83 lcd.refresh();
rottenegg 5:860087ff295e 84 }
rottenegg 5:860087ff295e 85 @endcode
rottenegg 5:860087ff295e 86 */
rottenegg 5:860087ff295e 87
rottenegg 5:860087ff295e 88
rottenegg 4:34bf3587cf42 89 #include "mbed.h"
rottenegg 4:34bf3587cf42 90 #include "N5110.h"
rottenegg 4:34bf3587cf42 91 #include "Bitmap.h"
rottenegg 4:34bf3587cf42 92 #include <vector>
rottenegg 4:34bf3587cf42 93
rottenegg 5:860087ff295e 94 /** Enum for Charater Facing Direction */
rottenegg 5:860087ff295e 95 enum Direction {
rottenegg 5:860087ff295e 96 Lt, /**< Charater Facing Left. */
rottenegg 5:860087ff295e 97 Rt, /**< Charater Facing Right. */
rottenegg 5:860087ff295e 98 Fd, /**< Charater Facing Forward. */
rottenegg 5:860087ff295e 99 Bd /**< Charater Facing Backward. */
rottenegg 5:860087ff295e 100 };
rottenegg 4:34bf3587cf42 101
rottenegg 4:34bf3587cf42 102 class CaMove {
rottenegg 5:860087ff295e 103 public :
rottenegg 5:860087ff295e 104 /** Constructor
rottenegg 5:860087ff295e 105 *
rottenegg 5:860087ff295e 106 *@param Button Pin for the button to control interations.
rottenegg 5:860087ff295e 107 *@param Pot_h Pin for the joystick horizontal potentiometer.
rottenegg 5:860087ff295e 108 *@param Pot_v Pin for the joystick vertical potentiometer.
rottenegg 5:860087ff295e 109 *
rottenegg 5:860087ff295e 110 */
rottenegg 5:860087ff295e 111 CaMove(PinName Button,
rottenegg 5:860087ff295e 112 PinName Pot_h,
rottenegg 5:860087ff295e 113 PinName Pot_v);
rottenegg 5:860087ff295e 114 /** Destructor */
rottenegg 4:34bf3587cf42 115 ~CaMove();
rottenegg 5:860087ff295e 116 /** Function to initilze the charater spawn point.
rottenegg 5:860087ff295e 117 *
rottenegg 5:860087ff295e 118 *@param x X-location on screen to spawn at (0 to 84).
rottenegg 5:860087ff295e 119 *@param y Y-location on screen to spawn at (0 to 48).
rottenegg 5:860087ff295e 120 *@param D In which direction the charater should spawn.
rottenegg 5:860087ff295e 121 *
rottenegg 5:860087ff295e 122 */
rottenegg 5:860087ff295e 123 void init(int x,int y,Direction D);
rottenegg 5:860087ff295e 124 /** Function that moves the charater but also manages all animations related to its movement and collisions. Double Black lines cannot be crossed and Single Black lines can only be crossed one way.
rottenegg 5:860087ff295e 125 *
rottenegg 5:860087ff295e 126 *@param lcd N5110 Class Object.
rottenegg 5:860087ff295e 127 *
rottenegg 5:860087ff295e 128 */
rottenegg 5:860087ff295e 129 void move(N5110 &lcd);
rottenegg 5:860087ff295e 130 /** Function to check if charater is in the screen.
rottenegg 5:860087ff295e 131 *
rottenegg 5:860087ff295e 132 *@returns Returns true if charater is on the screen otherwise false.
rottenegg 5:860087ff295e 133 *
rottenegg 5:860087ff295e 134 */
rottenegg 5:860087ff295e 135 bool in_screen();
rottenegg 5:860087ff295e 136 /** Function to set an interative region (rectangle areas only). Everytime it is run creates an additional new region.
rottenegg 5:860087ff295e 137 *
rottenegg 5:860087ff295e 138 *@param xmin the top left x-location of region.
rottenegg 5:860087ff295e 139 *@param ymin the top left y-location of region.
rottenegg 5:860087ff295e 140 *@param xl Width of region.
rottenegg 5:860087ff295e 141 *@param yl Height of region.
rottenegg 5:860087ff295e 142 *
rottenegg 5:860087ff295e 143 */
rottenegg 5:860087ff295e 144 void set_region(int xmin, int ymin, int xl, int yl);
rottenegg 5:860087ff295e 145 /** Function to delete all interative regions. */
rottenegg 4:34bf3587cf42 146 void delete_regions();
rottenegg 5:860087ff295e 147 /** Function that checks if the charater triggered an interative region (set to run on button press). */
rottenegg 4:34bf3587cf42 148 void intercheck();
rottenegg 5:860087ff295e 149 /** Function that checks if an interative region was triggered.
rottenegg 5:860087ff295e 150 *
rottenegg 5:860087ff295e 151 *@returns Returns true only when an interactive region was triggered for that instance but otherwise remains false. */
rottenegg 5:860087ff295e 152 bool is_trg();
rottenegg 5:860087ff295e 153 /** Function that tells which interactive was triggered.
rottenegg 5:860087ff295e 154 *
rottenegg 5:860087ff295e 155 *@returns Returns an integer based on which region was triggered, regions are labelled according to order of declaration in program counts from 0. Will return -1 if no regions triggered.
rottenegg 5:860087ff295e 156 *
rottenegg 5:860087ff295e 157 */
rottenegg 5:860087ff295e 158 int get_treg();
rottenegg 5:860087ff295e 159
rottenegg 5:860087ff295e 160 private :
rottenegg 5:860087ff295e 161 //Hardware Pins
rottenegg 5:860087ff295e 162 InterruptIn *_check;
rottenegg 5:860087ff295e 163 AnalogIn *_hoz;
rottenegg 5:860087ff295e 164 AnalogIn *_ver;
rottenegg 5:860087ff295e 165 //Enum declaration
rottenegg 5:860087ff295e 166 Direction _fc;
rottenegg 5:860087ff295e 167 //basic variables
rottenegg 4:34bf3587cf42 168 int _h;
rottenegg 4:34bf3587cf42 169 int _v;
rottenegg 4:34bf3587cf42 170 short int _itr;
rottenegg 5:860087ff295e 171 //Vector for storing interactive region x - y locations.
rottenegg 5:860087ff295e 172 std::vector<int> _vreg;
rottenegg 5:860087ff295e 173 //Volatile variables for ISR Interrupts
rottenegg 5:860087ff295e 174 volatile int _treg;
rottenegg 5:860087ff295e 175 volatile bool _trg;
rottenegg 4:34bf3587cf42 176 };
rottenegg 4:34bf3587cf42 177
rottenegg 5:860087ff295e 178 #endif