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: USBDevice mbed-rtos mbed
Fork of JoyStick by
main.cpp
- Committer:
- rvt
- Date:
- 2013-08-28
- Revision:
- 0:33bc88c4ab31
- Child:
- 1:5b2ab44eb31f
File content as of revision 0:33bc88c4ab31:
#include "mbed.h"
#include "USBHID.h"
#include "USBJoystick.h"
#include "MovingAverage.h"
#include "LowPassFilter.h"
#include "AntiLog.h"
#include "AnalogInFiltered.h"
#include "rtos.h"
#define TTY_DEBUG
// Value that defines when to start sending data this prevents the noise sending loads's of data over HID
#define DATA_CHANGE_TRIGGER 64
Serial pc(USBTX, USBRX); // tx, rx
// Activity led for HID data transmissions
DigitalOut HIDActivity(LED3);
//This report will contain data to be sent
HID_REPORT send_report;
HID_REPORT recv_report;
//USBJoyStick;
USBJoystick joystick;
// Structure that hold's the dataset of the input's
Mutex analogValueMutex;
struct AnalogData {
long value1;
long value2;
long value3;
long value4;
long value5;
} analogData;
void debug_thread(void const *args)
{
while (true) {
// Make a local copy
AnalogData localCopy;
AnalogData previous;
analogValueMutex.lock();
memcpy (&localCopy, &analogData, sizeof(AnalogData));
analogValueMutex.unlock();
// Send to USB
pc.printf("\x1B[0;0H");
pc.printf("Yoke and Pedals!\n\r");
pc.printf("Analog in p20: %d diff: %d \n\r",localCopy.value1,localCopy.value1-previous.value1);
pc.printf("Analog in p19: %d diff: %d \n\r",localCopy.value2,localCopy.value2-previous.value2);
pc.printf("Analog in p18: %d diff: %d \n\r",localCopy.value3,localCopy.value3-previous.value3);
pc.printf("Analog in p17: %d diff: %d \n\r",localCopy.value4,localCopy.value4-previous.value4);
pc.printf("Analog in p16: %d diff: %d \n\r",localCopy.value5,localCopy.value5-previous.value5);
memcpy (&previous, &localCopy, sizeof(AnalogData));
Thread::wait(1000);
}
}
void hid_thread(void const *args)
{
while (true) {
// TODO read buttons
uint8_t buttons=0;
uint8_t hat=0;
AnalogData localCopy;
// Wait for analog in to have some data
Thread::signal_wait(0x1);
// Make a local copy of the data
analogValueMutex.lock();
memcpy (&localCopy, &analogData, sizeof(AnalogData));
analogValueMutex.unlock();
// Update joystick's info
joystick.update(
localCopy.value2,
localCopy.value3,
localCopy.value4,
localCopy.value5,
buttons,
hat);
HIDActivity=!HIDActivity;
// Wait 50 ms to send a other USB update
Thread::wait(50);
}
}
int main()
{
analogData.value1=0;
analogData.value2=0;
analogData.value3=0;
analogData.value4=0;
analogData.value5=0;
#ifdef TTY_DEBUG
Thread _debugThread(debug_thread);
#endif
Thread _hid_thread(hid_thread);
// Initialise moving average filters
LowPassFilter lowPassFilter1(new AntiLog(new AnalogFilterInterface(),-5.0),0.95f); // The close the alpha value is to 1, the lower the cut-off frequency
LowPassFilter lowPassFilter2(new AntiLog(new AnalogFilterInterface(),-5.0),0.95f);
LowPassFilter lowPassFilter3(new AntiLog(new AnalogFilterInterface(),-5.0),0.95f);
LowPassFilter lowPassFilter4(new AntiLog(new AnalogFilterInterface(),-5.0),0.95f);
LowPassFilter lowPassFilter5(new AntiLog(new AnalogFilterInterface(),-5.0),0.95f);
// Initialise analog input and tell it what fulters to use
AnalogInFiltered ai1(&lowPassFilter1, p20);
AnalogInFiltered ai2(&lowPassFilter2, p19);
AnalogInFiltered ai3(&lowPassFilter3, p18);
AnalogInFiltered ai4(&lowPassFilter4, p17);
AnalogInFiltered ai5(&lowPassFilter5, p16);
while (true) {
// Measure analog in's
ai1.measure();
ai2.measure();
ai3.measure();
ai4.measure();
ai5.measure();
// test of any of the values have been changed, so we only update when data was actually changed
if (
false
|| ai1.getIsChanged(DATA_CHANGE_TRIGGER) // Value of 4 seems to wobble a bit, 8 is good
|| ai2.getIsChanged(DATA_CHANGE_TRIGGER)
|| ai3.getIsChanged(DATA_CHANGE_TRIGGER)
|| ai4.getIsChanged(DATA_CHANGE_TRIGGER)
// || ai5.getIsChanged(8)
) {
// Copy analog data to global data
analogValueMutex.lock();
analogData.value1 = ai1.getValue();
analogData.value2 = ai2.getValue();
analogData.value3 = ai3.getValue();
analogData.value4 = ai4.getValue();
analogData.value5 = ai5.getValue();
analogValueMutex.unlock();
// Signal that data has been changed
_hid_thread.signal_set(0x1);
}
Thread::wait(1);
}
}
