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
CrossHairs/CrossHairs.cpp@8:475b80f88ceb, 2019-05-08 (annotated)
- Committer:
- adat80
- Date:
- Wed May 08 23:40:34 2019 +0000
- Revision:
- 8:475b80f88ceb
- Parent:
- 5:8bd09c675f28
Final Submission. I have read and agreed with Statement of Academic Integrity.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
adat80 | 1:3916f272663e | 1 | #include "CrossHairs.h" |
adat80 | 1:3916f272663e | 2 | |
adat80 | 1:3916f272663e | 3 | |
adat80 | 1:3916f272663e | 4 | int crossHairsSprite[7][7] = { |
adat80 | 1:3916f272663e | 5 | { 0,0,1,1,1,0,0 }, |
adat80 | 1:3916f272663e | 6 | { 0,1,0,0,0,1,0 }, |
adat80 | 1:3916f272663e | 7 | { 1,0,0,1,0,0,1 }, |
adat80 | 1:3916f272663e | 8 | { 1,0,1,1,1,0,1 }, |
adat80 | 1:3916f272663e | 9 | { 1,0,0,1,0,0,1 }, |
adat80 | 1:3916f272663e | 10 | { 0,1,0,0,0,1,0 }, |
adat80 | 1:3916f272663e | 11 | { 0,0,1,1,1,0,0 }, |
adat80 | 1:3916f272663e | 12 | }; |
adat80 | 1:3916f272663e | 13 | |
adat80 | 1:3916f272663e | 14 | CrossHairs::CrossHairs() |
adat80 | 1:3916f272663e | 15 | { |
adat80 | 1:3916f272663e | 16 | |
adat80 | 1:3916f272663e | 17 | } |
adat80 | 1:3916f272663e | 18 | |
adat80 | 1:3916f272663e | 19 | CrossHairs::~CrossHairs() |
adat80 | 1:3916f272663e | 20 | { |
adat80 | 1:3916f272663e | 21 | |
adat80 | 1:3916f272663e | 22 | } |
adat80 | 1:3916f272663e | 23 | |
adat80 | 1:3916f272663e | 24 | void CrossHairs::init(int speed) |
adat80 | 1:3916f272663e | 25 | { |
adat80 | 5:8bd09c675f28 | 26 | //set the cross hairs x and y to centre of display |
adat80 | 1:3916f272663e | 27 | _x = WIDTH/2 - (7/2); |
adat80 | 1:3916f272663e | 28 | _y = HEIGHT/2 - (7/2); |
adat80 | 5:8bd09c675f28 | 29 | |
adat80 | 5:8bd09c675f28 | 30 | //set the cross hairs max speed in pixels per frame |
adat80 | 1:3916f272663e | 31 | _speed = speed; |
adat80 | 1:3916f272663e | 32 | |
adat80 | 1:3916f272663e | 33 | |
adat80 | 1:3916f272663e | 34 | |
adat80 | 1:3916f272663e | 35 | } |
adat80 | 1:3916f272663e | 36 | |
adat80 | 1:3916f272663e | 37 | void CrossHairs::draw(N5110 &lcd) |
adat80 | 1:3916f272663e | 38 | { |
adat80 | 1:3916f272663e | 39 | // x origin, y origin, rows, cols, sprite |
adat80 | 1:3916f272663e | 40 | lcd.drawSprite(_x,_y,7,7,(int *)crossHairsSprite); |
adat80 | 1:3916f272663e | 41 | |
adat80 | 1:3916f272663e | 42 | } |
adat80 | 1:3916f272663e | 43 | |
adat80 | 5:8bd09c675f28 | 44 | void CrossHairs::update(float angle,float mag) |
adat80 | 1:3916f272663e | 45 | { |
adat80 | 5:8bd09c675f28 | 46 | //Correcting JoyStick angle to match screen angles |
adat80 | 1:3916f272663e | 47 | angle = angle - 90.0f; |
adat80 | 1:3916f272663e | 48 | |
adat80 | 1:3916f272663e | 49 | //Conversion To Radians |
adat80 | 1:3916f272663e | 50 | double angleRads = (angle/360.0f)*2.0f*3.14f; |
adat80 | 1:3916f272663e | 51 | |
adat80 | 5:8bd09c675f28 | 52 | //incriment x and y based on an angle and magnitude |
adat80 | 1:3916f272663e | 53 | _x+= _speed*mag*cos(angleRads); |
adat80 | 1:3916f272663e | 54 | _y += _speed*mag*sin(angleRads); |
adat80 | 1:3916f272663e | 55 | |
adat80 | 5:8bd09c675f28 | 56 | //check if cross hairs are outside screen boundary |
adat80 | 5:8bd09c675f28 | 57 | //if they are outside the boundary set the problem coordinate to the boundary |
adat80 | 1:3916f272663e | 58 | int halfCrossHairs = 3; |
adat80 | 1:3916f272663e | 59 | if (_x < -halfCrossHairs) { |
adat80 | 1:3916f272663e | 60 | _x = -halfCrossHairs; |
adat80 | 1:3916f272663e | 61 | } else if (_x > 84-halfCrossHairs) { |
adat80 | 1:3916f272663e | 62 | _x = 84-halfCrossHairs; |
adat80 | 1:3916f272663e | 63 | } |
adat80 | 1:3916f272663e | 64 | if (_y < -halfCrossHairs) { |
adat80 | 1:3916f272663e | 65 | _y = -halfCrossHairs; |
adat80 | 1:3916f272663e | 66 | } else if (_y > 48-halfCrossHairs) { |
adat80 | 1:3916f272663e | 67 | _y = 48-halfCrossHairs; |
adat80 | 1:3916f272663e | 68 | } |
adat80 | 1:3916f272663e | 69 | |
adat80 | 1:3916f272663e | 70 | } |
adat80 | 1:3916f272663e | 71 | |
adat80 | 1:3916f272663e | 72 | |
adat80 | 1:3916f272663e | 73 | |
adat80 | 1:3916f272663e | 74 | Vector2D CrossHairs::get_pos() |
adat80 | 1:3916f272663e | 75 | { |
adat80 | 1:3916f272663e | 76 | Vector2D p = {_x,_y}; |
adat80 | 1:3916f272663e | 77 | return p; |
adat80 | 1:3916f272663e | 78 | } |
adat80 | 1:3916f272663e | 79 | |
adat80 | 1:3916f272663e | 80 | void CrossHairs::set_pos(Vector2D p) |
adat80 | 1:3916f272663e | 81 | { |
adat80 | 1:3916f272663e | 82 | _x = p.x; |
adat80 | 1:3916f272663e | 83 | _y = p.y; |
adat80 | 1:3916f272663e | 84 | } |