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
Doodler/Doodler.cpp@5:8814d6de77d0, 2019-04-13 (annotated)
- Committer:
- el17m2h
- Date:
- Sat Apr 13 17:37:52 2019 +0000
- Revision:
- 5:8814d6de77d0
- Parent:
- 4:8ec314f806ae
- Child:
- 6:848d1e4c1a31
Created an update function for the doodler to change its position depending on the user moving the joystick left or right.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17m2h | 4:8ec314f806ae | 1 | #include "Doodler.h" |
el17m2h | 4:8ec314f806ae | 2 | |
el17m2h | 4:8ec314f806ae | 3 | Doodler::Doodler(){ |
el17m2h | 4:8ec314f806ae | 4 | } |
el17m2h | 4:8ec314f806ae | 5 | Doodler::~Doodler(){ |
el17m2h | 4:8ec314f806ae | 6 | } |
el17m2h | 4:8ec314f806ae | 7 | |
el17m2h | 5:8814d6de77d0 | 8 | void Doodler::init(int radius){ |
el17m2h | 4:8ec314f806ae | 9 | |
el17m2h | 5:8814d6de77d0 | 10 | // initial position of doodler at middle |
el17m2h | 5:8814d6de77d0 | 11 | _x = (70/2)+6; // middle |
el17m2h | 5:8814d6de77d0 | 12 | _y = 43; // if doodler is above the middle, the screen shifts up (floors shift down) |
el17m2h | 5:8814d6de77d0 | 13 | _radius = radius; |
el17m2h | 5:8814d6de77d0 | 14 | _speed =0.5; // default speed |
el17m2h | 4:8ec314f806ae | 15 | } |
el17m2h | 4:8ec314f806ae | 16 | |
el17m2h | 4:8ec314f806ae | 17 | void Doodler::draw(N5110 &lcd){ |
el17m2h | 4:8ec314f806ae | 18 | lcd.drawCircle(_x, _y, _radius, FILL_BLACK); |
el17m2h | 4:8ec314f806ae | 19 | } |
el17m2h | 4:8ec314f806ae | 20 | |
el17m2h | 5:8814d6de77d0 | 21 | void Doodler::update(Direction d, float mag){ |
el17m2h | 5:8814d6de77d0 | 22 | _speed = int(mag*10.0f); |
el17m2h | 5:8814d6de77d0 | 23 | |
el17m2h | 5:8814d6de77d0 | 24 | if (d == W){ // if direction is left |
el17m2h | 5:8814d6de77d0 | 25 | _x-= _speed; |
el17m2h | 5:8814d6de77d0 | 26 | } else if (d == E){ |
el17m2h | 5:8814d6de77d0 | 27 | _x+= _speed; |
el17m2h | 5:8814d6de77d0 | 28 | } |
el17m2h | 5:8814d6de77d0 | 29 | // checking doodler does not leave screen: |
el17m2h | 5:8814d6de77d0 | 30 | if (_x > WIDTH-4){ |
el17m2h | 5:8814d6de77d0 | 31 | _x = WIDTH-4; |
el17m2h | 5:8814d6de77d0 | 32 | } |
el17m2h | 5:8814d6de77d0 | 33 | if (_x < _radius){ |
el17m2h | 5:8814d6de77d0 | 34 | _x = _radius; |
el17m2h | 5:8814d6de77d0 | 35 | } |
el17m2h | 4:8ec314f806ae | 36 | } |
el17m2h | 4:8ec314f806ae | 37 | |
el17m2h | 4:8ec314f806ae | 38 | |
el17m2h | 4:8ec314f806ae | 39 | Vector2D Doodler::get_pos(){ |
el17m2h | 4:8ec314f806ae | 40 | Vector2D p = {_x,_y}; |
el17m2h | 4:8ec314f806ae | 41 | return p; |
el17m2h | 4:8ec314f806ae | 42 | } |