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
Diff: CaMove/CaMove.cpp
- Revision:
- 5:860087ff295e
- Parent:
- 4:34bf3587cf42
- Child:
- 8:e3a76a808a4c
--- a/CaMove/CaMove.cpp Thu Apr 18 21:44:05 2019 +0000 +++ b/CaMove/CaMove.cpp Sun Apr 21 00:41:35 2019 +0000 @@ -1,41 +1,58 @@ #include "CaMove.h" #include "FilePaths.h" - -CaMove::CaMove(PinName pin) : _check(pin) { - _check.mode(PullDown); - _check.rise(callback(this,&CaMove::intercheck)); - _h = 40; - _v = 20; - _itr = 0; - Faceing = Lt; - Trig = false; +//Constructor +CaMove::CaMove(PinName Button, + PinName Pot_h, + PinName Pot_v) + : //Creating Class Objects + _check(new InterruptIn(Button)), + _hoz(new AnalogIn(Pot_h)), + _ver(new AnalogIn(Pot_v)) + { + _check->mode(PullDown); + _check->rise(callback(this,&CaMove::intercheck)); }; - +//Destructor CaMove::~CaMove() { + delete _check; + delete _hoz; + delete _ver; }; -void CaMove::CMmove(N5110 &lcd, AnalogIn &hoz, AnalogIn &ver) { - if (hoz > 0.6f && lcd.getPixel((_h-1),(_v+7)) == 0) { +void CaMove::init(int x,int y,Direction D) { + _h = x; + _v = y; + _itr = 0; + _fc = D; + _trg = false; + _treg = -1; +}; + +void CaMove::move(N5110 &lcd) { + //If statements to check joystick movement and has in bluilt collsion detection + //collsion detection check 4 sides for black pixels + if (_hoz->read() > 0.6f && lcd.getPixel((_h-1),(_v+7)) == 0) { _h = _h - 2; - Faceing = Lt; + _fc = Lt; } - if (hoz < 0.4f && lcd.getPixel((_h+10),(_v+7)) == 0) { + if (_hoz->read() < 0.4f && lcd.getPixel((_h+10),(_v+7)) == 0) { _h = _h + 2; - Faceing = Rt; + _fc = Rt; } - if (ver > 0.6f && lcd.getPixel((_h+5),(_v-1)) == 0) { + if (_ver->read() > 0.6f && lcd.getPixel((_h+5),(_v-1)) == 0) { _v = _v - 2; - Faceing = Fd; + _fc = Fd; } - if (ver < 0.4f && lcd.getPixel((_h+5),(_v+14)) == 0) { + if (_ver->read() < 0.4f && lcd.getPixel((_h+5),(_v+14)) == 0) { _v = _v + 2; - Faceing = Bd; + _fc = Bd; } if (_itr == 3) { _itr = -1; } _itr++; - switch (Faceing) { + //Switch case allows charater to move diagonally and retains its facing direction when stationary + switch (_fc) { case Lt: Bitmap::renderBMP(L[_itr],lcd,_h,_v); break; @@ -55,73 +72,82 @@ }; -bool CaMove::inscreen() { +bool CaMove::in_screen() { if (_h >= 84 || (_h + 9) <= 0 || _v <= 0 || (_v + 13) >= 48) { + return false; + } else { return true; - } else { - return false; } }; - -void CaMove::set_region(int xmin, int ymax, int xl, int yl) { - intereact.push_back((xmin + xl)); - intereact.push_back(xmin); - intereact.push_back(ymax); - intereact.push_back((ymax - yl)); +//Sets values into vector +void CaMove::set_region(int xmin, int ymin, int xl, int yl) { + _vreg.push_back((xmin + xl)); + _vreg.push_back(xmin); + _vreg.push_back((ymin + yl)); + _vreg.push_back(ymin); }; void CaMove::delete_regions() { - intereact.clear(); + _vreg.clear(); }; void CaMove::intercheck() { - Trig = false; - if (intereact.size() > 0) { - for(int Vvalue = 0; Vvalue < intereact.size(); Vvalue = Vvalue +4) { - //if Statements - bool LowerH = (_h - 1) <= intereact[Vvalue] && (_h-1) >= intereact[Vvalue + 1]; - bool UpperH = (_h + 10) <= intereact[Vvalue] && (_h + 10) >= intereact[Vvalue + 1]; - bool MidH = (_h + 5) <= intereact[Vvalue] && (_h + 5) >= intereact[Vvalue + 1]; - bool LowerV = (_v - 1) >= intereact[Vvalue + 3] && (_v - 1) <= intereact[Vvalue + 2]; - bool UpperV = (_v+14) >= intereact[Vvalue + 3] && (_v + 14) <= intereact[Vvalue + 2]; - bool MidV = (_v+7) >= intereact[Vvalue + 3] && (_v + 7) <= intereact[Vvalue + 2]; - - switch (Faceing) { + _trg = false; + if (_vreg.size() > 0) { + for(int Vvalue = 0; Vvalue < _vreg.size(); Vvalue = Vvalue +4) { + //Function only will check the facing side for interative regions + //If Statements check that sides position with all x- y region ranges to see if charater has hit that region. + bool LowerH = (_h - 1) <= _vreg[Vvalue] && (_h-1) >= _vreg[Vvalue + 1]; + bool UpperH = (_h + 10) <= _vreg[Vvalue] && (_h + 10) >= _vreg[Vvalue + 1]; + bool MidH = (_h + 5) <= _vreg[Vvalue] && (_h + 5) >= _vreg[Vvalue + 1]; + bool LowerV = (_v - 1) >= _vreg[Vvalue + 3] && (_v - 1) <= _vreg[Vvalue + 2]; + bool UpperV = (_v+14) >= _vreg[Vvalue + 3] && (_v + 14) <= _vreg[Vvalue + 2]; + bool MidV = (_v+7) >= _vreg[Vvalue + 3] && (_v + 7) <= _vreg[Vvalue + 2]; + //The region number is found depending on its position in the vector + //e.g elements 0 to 3 are in region 0 and elements 4 to 7 are in region 1 + switch (_fc) { case Lt: //L if (LowerH && MidV) { - Trig = true; - tregion = Vvalue / 4; + _trg = true; + _treg = Vvalue / 4; }; break; case Rt: //R if (UpperH && MidV) { - Trig = true; - tregion = Vvalue / 4; + _trg = true; + _treg = Vvalue / 4; } break; case Fd: //F if (MidH && LowerV) { - Trig = true; - tregion = Vvalue / 4; + _trg = true; + _treg = Vvalue / 4; } break; case Bd: //B if (MidH && UpperV) { - Trig = true; - tregion = Vvalue / 4; + _trg = true; + _treg = Vvalue / 4; } break; } } } -} - -bool CaMove::get_Trig() { - bool temp = Trig; - Trig = false; +}; +//Functions revert to default values evrytime they are called. +bool CaMove::is_trg() { + bool temp = _trg; + _trg = false; return temp; }; + +int CaMove::get_treg() { + int temp = _treg; + _treg = -1; + return temp; +} +