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-rtos mbed ssWi
joystick.cpp
00001 #include "joystick.hpp" 00002 00003 00004 void Joystick::tuneChannel (int nOfSamples, int channel) 00005 { 00006 if (channel<0 || channel>3) 00007 return; 00008 ranges[channel].maxV = std::numeric_limits<float>::min(); 00009 ranges[channel].minV = std::numeric_limits<float>::max(); 00010 for (int i=0; i<nOfSamples; i++) { 00011 float val = channels[channel]->read(); 00012 if (val>ranges[channel].maxV) 00013 ranges[channel].maxV = val; 00014 else if (val<ranges[channel].minV) 00015 ranges[channel].minV = val; 00016 wait(0.1); 00017 } 00018 } 00019 00020 void Joystick::tuneCentring (int nOfSamples, int channel) 00021 { 00022 if (channel<0 || channel>3) 00023 return; 00024 ranges[channel].center = 0.0; 00025 for (int i=0; i<nOfSamples; i++) { 00026 float val = channels[channel]->read(); 00027 ranges[channel].center += val; 00028 wait(0.1); 00029 } 00030 ranges[channel].center /= nOfSamples; 00031 } 00032 00033 00034 00035 float Joystick::read(int channel) 00036 { 00037 if (channel<0 || channel>3) 00038 return std::numeric_limits<float>::min(); 00039 mutexes[channel].lock(); 00040 float val = values[channel]; 00041 mutexes[channel].unlock(); 00042 return val; 00043 } 00044 00045 00046 void Joystick::update (int channel) 00047 { 00048 mutexes[channel].lock(); 00049 float val = channels[channel]->read(); 00050 mutexes[channel].unlock(); 00051 if (val>ranges[channel].center) 00052 values[channel] = -(val-ranges[channel].center)/ 00053 (ranges[channel].maxV-ranges[channel].center); 00054 else 00055 values[channel] = (ranges[channel].center-val)/ 00056 (ranges[channel].center-ranges[channel].minV); 00057 } 00058 00059 void Joystick::update () 00060 { 00061 update(JOYSTICK_SX_X); 00062 update(JOYSTICK_SX_Y); 00063 update(JOYSTICK_DX_X); 00064 update(JOYSTICK_DX_Y); 00065 }
Generated on Mon Jul 18 2022 23:15:17 by
1.7.2