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: N5110 PinDetect PowerControl mbed
Joystick.cpp
00001 #include "Joystick.h" 00002 #include "mbed.h" 00003 #include <cmath> 00004 00005 /// @file Joystick.cpp 00006 00007 Joystick::Joystick(PinName x, PinName y, PinName button) 00008 { 00009 xPot = new AnalogIn(x); 00010 yPot = new AnalogIn(y); 00011 btn = new DigitalIn(button); 00012 } 00013 00014 Joystick::~Joystick() 00015 { 00016 delete xPot; 00017 delete yPot; 00018 delete btn; 00019 } 00020 00021 00022 // Make sure that the Joystick is centered while calibrating 00023 void Joystick::calibrate() 00024 { 00025 btn->mode(PullDown); 00026 centerX = *xPot; 00027 centerY = *yPot; 00028 } 00029 00030 00031 void Joystick::update() 00032 { 00033 // Get position of joystick compared to the center values (delta x and delta y). 00034 dx = *xPot - centerX; 00035 dy = *yPot - centerY; 00036 00037 if (abs(dx) < DEAD_ZONE && abs(dy) < DEAD_ZONE) 00038 dir = CENTER; 00039 else if (abs(dx) < DEAD_ZONE) // Inside horizontal dead zone. 00040 dir = (dy > DEAD_ZONE) ? UP : DOWN; 00041 else if (abs(dy) < DEAD_ZONE) // Inside vertical dead zone. 00042 dir = (dx > DEAD_ZONE) ? LEFT : RIGHT; 00043 else if (dx > 0) // To the left, outside both deadzones. 00044 dir = (dy > 0) ? UP_LEFT : DOWN_LEFT; 00045 else // To the right, outside both deadzones 00046 dir = (dy > 0) ? UP_RIGHT : DOWN_RIGHT; 00047 }
Generated on Tue Jul 12 2022 21:59:47 by
1.7.2