HandheldController/src/controls.cpp

44 lines
1.0 KiB
C++

#include "Arduino.h"
#include <stdint.h>
#include <Wire.h>
#include <Adafruit_ADS1X15.h>
Adafruit_ADS1115 ads1115;
void controlsInit() {
ads1115.begin();
ads1115.setGain(GAIN_ONE);
}
float map(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void getControlsState(float &throttle, int &direction, bool &horn, bool &bell, bool &lights) {
float throttle_volts = ads1115.computeVolts(ads1115.readADC_SingleEnded(0));
float direction_volts = ads1115.computeVolts(ads1115.readADC_SingleEnded(1));
float throt = map(throttle_volts, 1.0f, 2.0f, 0.0f, 1.0f);
if (throt > 1.0f) {
throt = 1.0f;
}
if (throt < 0.0f) {
throt = 0.0f;
}
if (direction_volts < 3.3/3.0) {
direction = -1;
throttle = throt;
} else if (direction_volts > (3.3 / 3.0) * 2.0) {
direction = 1;
throttle = throt;
} else {
direction = 0;
}
horn = false;
bell = false;
lights = false;
}