Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed FATFileSystem
CaMove/CaMove.cpp@5:860087ff295e, 2019-04-21 (annotated)
- Committer:
- rottenegg
- Date:
- Sun Apr 21 00:41:35 2019 +0000
- Revision:
- 5:860087ff295e
- Parent:
- 4:34bf3587cf42
- Child:
- 8:e3a76a808a4c
CaMove Library Update, CaMove Documentation Done, WDplayer Documentation Update.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rottenegg | 4:34bf3587cf42 | 1 | #include "CaMove.h" |
rottenegg | 4:34bf3587cf42 | 2 | #include "FilePaths.h" |
rottenegg | 5:860087ff295e | 3 | //Constructor |
rottenegg | 5:860087ff295e | 4 | CaMove::CaMove(PinName Button, |
rottenegg | 5:860087ff295e | 5 | PinName Pot_h, |
rottenegg | 5:860087ff295e | 6 | PinName Pot_v) |
rottenegg | 5:860087ff295e | 7 | : //Creating Class Objects |
rottenegg | 5:860087ff295e | 8 | _check(new InterruptIn(Button)), |
rottenegg | 5:860087ff295e | 9 | _hoz(new AnalogIn(Pot_h)), |
rottenegg | 5:860087ff295e | 10 | _ver(new AnalogIn(Pot_v)) |
rottenegg | 5:860087ff295e | 11 | { |
rottenegg | 5:860087ff295e | 12 | _check->mode(PullDown); |
rottenegg | 5:860087ff295e | 13 | _check->rise(callback(this,&CaMove::intercheck)); |
rottenegg | 4:34bf3587cf42 | 14 | }; |
rottenegg | 5:860087ff295e | 15 | //Destructor |
rottenegg | 4:34bf3587cf42 | 16 | CaMove::~CaMove() { |
rottenegg | 5:860087ff295e | 17 | delete _check; |
rottenegg | 5:860087ff295e | 18 | delete _hoz; |
rottenegg | 5:860087ff295e | 19 | delete _ver; |
rottenegg | 4:34bf3587cf42 | 20 | }; |
rottenegg | 4:34bf3587cf42 | 21 | |
rottenegg | 5:860087ff295e | 22 | void CaMove::init(int x,int y,Direction D) { |
rottenegg | 5:860087ff295e | 23 | _h = x; |
rottenegg | 5:860087ff295e | 24 | _v = y; |
rottenegg | 5:860087ff295e | 25 | _itr = 0; |
rottenegg | 5:860087ff295e | 26 | _fc = D; |
rottenegg | 5:860087ff295e | 27 | _trg = false; |
rottenegg | 5:860087ff295e | 28 | _treg = -1; |
rottenegg | 5:860087ff295e | 29 | }; |
rottenegg | 5:860087ff295e | 30 | |
rottenegg | 5:860087ff295e | 31 | void CaMove::move(N5110 &lcd) { |
rottenegg | 5:860087ff295e | 32 | //If statements to check joystick movement and has in bluilt collsion detection |
rottenegg | 5:860087ff295e | 33 | //collsion detection check 4 sides for black pixels |
rottenegg | 5:860087ff295e | 34 | if (_hoz->read() > 0.6f && lcd.getPixel((_h-1),(_v+7)) == 0) { |
rottenegg | 4:34bf3587cf42 | 35 | _h = _h - 2; |
rottenegg | 5:860087ff295e | 36 | _fc = Lt; |
rottenegg | 4:34bf3587cf42 | 37 | } |
rottenegg | 5:860087ff295e | 38 | if (_hoz->read() < 0.4f && lcd.getPixel((_h+10),(_v+7)) == 0) { |
rottenegg | 4:34bf3587cf42 | 39 | _h = _h + 2; |
rottenegg | 5:860087ff295e | 40 | _fc = Rt; |
rottenegg | 4:34bf3587cf42 | 41 | } |
rottenegg | 5:860087ff295e | 42 | if (_ver->read() > 0.6f && lcd.getPixel((_h+5),(_v-1)) == 0) { |
rottenegg | 4:34bf3587cf42 | 43 | _v = _v - 2; |
rottenegg | 5:860087ff295e | 44 | _fc = Fd; |
rottenegg | 4:34bf3587cf42 | 45 | } |
rottenegg | 5:860087ff295e | 46 | if (_ver->read() < 0.4f && lcd.getPixel((_h+5),(_v+14)) == 0) { |
rottenegg | 4:34bf3587cf42 | 47 | _v = _v + 2; |
rottenegg | 5:860087ff295e | 48 | _fc = Bd; |
rottenegg | 4:34bf3587cf42 | 49 | } |
rottenegg | 4:34bf3587cf42 | 50 | if (_itr == 3) { |
rottenegg | 4:34bf3587cf42 | 51 | _itr = -1; |
rottenegg | 4:34bf3587cf42 | 52 | } |
rottenegg | 4:34bf3587cf42 | 53 | _itr++; |
rottenegg | 5:860087ff295e | 54 | //Switch case allows charater to move diagonally and retains its facing direction when stationary |
rottenegg | 5:860087ff295e | 55 | switch (_fc) { |
rottenegg | 4:34bf3587cf42 | 56 | case Lt: |
rottenegg | 4:34bf3587cf42 | 57 | Bitmap::renderBMP(L[_itr],lcd,_h,_v); |
rottenegg | 4:34bf3587cf42 | 58 | break; |
rottenegg | 4:34bf3587cf42 | 59 | |
rottenegg | 4:34bf3587cf42 | 60 | case Rt: |
rottenegg | 4:34bf3587cf42 | 61 | Bitmap::renderBMP(R[_itr],lcd,_h,_v); |
rottenegg | 4:34bf3587cf42 | 62 | break; |
rottenegg | 4:34bf3587cf42 | 63 | |
rottenegg | 4:34bf3587cf42 | 64 | case Fd: |
rottenegg | 4:34bf3587cf42 | 65 | Bitmap::renderBMP(F[_itr],lcd,_h,_v); |
rottenegg | 4:34bf3587cf42 | 66 | break; |
rottenegg | 4:34bf3587cf42 | 67 | |
rottenegg | 4:34bf3587cf42 | 68 | case Bd: |
rottenegg | 4:34bf3587cf42 | 69 | Bitmap::renderBMP(B[_itr],lcd,_h,_v); |
rottenegg | 4:34bf3587cf42 | 70 | break; |
rottenegg | 4:34bf3587cf42 | 71 | } |
rottenegg | 4:34bf3587cf42 | 72 | }; |
rottenegg | 4:34bf3587cf42 | 73 | |
rottenegg | 4:34bf3587cf42 | 74 | |
rottenegg | 5:860087ff295e | 75 | bool CaMove::in_screen() { |
rottenegg | 4:34bf3587cf42 | 76 | if (_h >= 84 || (_h + 9) <= 0 || _v <= 0 || (_v + 13) >= 48) { |
rottenegg | 5:860087ff295e | 77 | return false; |
rottenegg | 5:860087ff295e | 78 | } else { |
rottenegg | 4:34bf3587cf42 | 79 | return true; |
rottenegg | 4:34bf3587cf42 | 80 | } |
rottenegg | 4:34bf3587cf42 | 81 | }; |
rottenegg | 5:860087ff295e | 82 | //Sets values into vector |
rottenegg | 5:860087ff295e | 83 | void CaMove::set_region(int xmin, int ymin, int xl, int yl) { |
rottenegg | 5:860087ff295e | 84 | _vreg.push_back((xmin + xl)); |
rottenegg | 5:860087ff295e | 85 | _vreg.push_back(xmin); |
rottenegg | 5:860087ff295e | 86 | _vreg.push_back((ymin + yl)); |
rottenegg | 5:860087ff295e | 87 | _vreg.push_back(ymin); |
rottenegg | 4:34bf3587cf42 | 88 | }; |
rottenegg | 4:34bf3587cf42 | 89 | |
rottenegg | 4:34bf3587cf42 | 90 | void CaMove::delete_regions() { |
rottenegg | 5:860087ff295e | 91 | _vreg.clear(); |
rottenegg | 4:34bf3587cf42 | 92 | }; |
rottenegg | 4:34bf3587cf42 | 93 | |
rottenegg | 4:34bf3587cf42 | 94 | void CaMove::intercheck() { |
rottenegg | 5:860087ff295e | 95 | _trg = false; |
rottenegg | 5:860087ff295e | 96 | if (_vreg.size() > 0) { |
rottenegg | 5:860087ff295e | 97 | for(int Vvalue = 0; Vvalue < _vreg.size(); Vvalue = Vvalue +4) { |
rottenegg | 5:860087ff295e | 98 | //Function only will check the facing side for interative regions |
rottenegg | 5:860087ff295e | 99 | //If Statements check that sides position with all x- y region ranges to see if charater has hit that region. |
rottenegg | 5:860087ff295e | 100 | bool LowerH = (_h - 1) <= _vreg[Vvalue] && (_h-1) >= _vreg[Vvalue + 1]; |
rottenegg | 5:860087ff295e | 101 | bool UpperH = (_h + 10) <= _vreg[Vvalue] && (_h + 10) >= _vreg[Vvalue + 1]; |
rottenegg | 5:860087ff295e | 102 | bool MidH = (_h + 5) <= _vreg[Vvalue] && (_h + 5) >= _vreg[Vvalue + 1]; |
rottenegg | 5:860087ff295e | 103 | bool LowerV = (_v - 1) >= _vreg[Vvalue + 3] && (_v - 1) <= _vreg[Vvalue + 2]; |
rottenegg | 5:860087ff295e | 104 | bool UpperV = (_v+14) >= _vreg[Vvalue + 3] && (_v + 14) <= _vreg[Vvalue + 2]; |
rottenegg | 5:860087ff295e | 105 | bool MidV = (_v+7) >= _vreg[Vvalue + 3] && (_v + 7) <= _vreg[Vvalue + 2]; |
rottenegg | 5:860087ff295e | 106 | //The region number is found depending on its position in the vector |
rottenegg | 5:860087ff295e | 107 | //e.g elements 0 to 3 are in region 0 and elements 4 to 7 are in region 1 |
rottenegg | 5:860087ff295e | 108 | switch (_fc) { |
rottenegg | 4:34bf3587cf42 | 109 | case Lt: //L |
rottenegg | 4:34bf3587cf42 | 110 | if (LowerH && MidV) { |
rottenegg | 5:860087ff295e | 111 | _trg = true; |
rottenegg | 5:860087ff295e | 112 | _treg = Vvalue / 4; |
rottenegg | 4:34bf3587cf42 | 113 | }; |
rottenegg | 4:34bf3587cf42 | 114 | break; |
rottenegg | 4:34bf3587cf42 | 115 | |
rottenegg | 4:34bf3587cf42 | 116 | case Rt: //R |
rottenegg | 4:34bf3587cf42 | 117 | if (UpperH && MidV) { |
rottenegg | 5:860087ff295e | 118 | _trg = true; |
rottenegg | 5:860087ff295e | 119 | _treg = Vvalue / 4; |
rottenegg | 4:34bf3587cf42 | 120 | } |
rottenegg | 4:34bf3587cf42 | 121 | break; |
rottenegg | 4:34bf3587cf42 | 122 | |
rottenegg | 4:34bf3587cf42 | 123 | case Fd: //F |
rottenegg | 4:34bf3587cf42 | 124 | if (MidH && LowerV) { |
rottenegg | 5:860087ff295e | 125 | _trg = true; |
rottenegg | 5:860087ff295e | 126 | _treg = Vvalue / 4; |
rottenegg | 4:34bf3587cf42 | 127 | } |
rottenegg | 4:34bf3587cf42 | 128 | break; |
rottenegg | 4:34bf3587cf42 | 129 | |
rottenegg | 4:34bf3587cf42 | 130 | case Bd: //B |
rottenegg | 4:34bf3587cf42 | 131 | if (MidH && UpperV) { |
rottenegg | 5:860087ff295e | 132 | _trg = true; |
rottenegg | 5:860087ff295e | 133 | _treg = Vvalue / 4; |
rottenegg | 4:34bf3587cf42 | 134 | } |
rottenegg | 4:34bf3587cf42 | 135 | break; |
rottenegg | 4:34bf3587cf42 | 136 | } |
rottenegg | 4:34bf3587cf42 | 137 | } |
rottenegg | 4:34bf3587cf42 | 138 | } |
rottenegg | 5:860087ff295e | 139 | }; |
rottenegg | 5:860087ff295e | 140 | //Functions revert to default values evrytime they are called. |
rottenegg | 5:860087ff295e | 141 | bool CaMove::is_trg() { |
rottenegg | 5:860087ff295e | 142 | bool temp = _trg; |
rottenegg | 5:860087ff295e | 143 | _trg = false; |
rottenegg | 4:34bf3587cf42 | 144 | return temp; |
rottenegg | 4:34bf3587cf42 | 145 | |
rottenegg | 4:34bf3587cf42 | 146 | }; |
rottenegg | 5:860087ff295e | 147 | |
rottenegg | 5:860087ff295e | 148 | int CaMove::get_treg() { |
rottenegg | 5:860087ff295e | 149 | int temp = _treg; |
rottenegg | 5:860087ff295e | 150 | _treg = -1; |
rottenegg | 5:860087ff295e | 151 | return temp; |
rottenegg | 5:860087ff295e | 152 | } |
rottenegg | 5:860087ff295e | 153 |