-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpot.cpp
More file actions
53 lines (45 loc) · 2.78 KB
/
pot.cpp
File metadata and controls
53 lines (45 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//Potentiometer* leftPot = new SamplingPotentiometer(A0, 3.3, 100.0);
//Potentiometer* rightPot = new SamplingPotentiometer(A1, 3.3, 100.0);
//leftPot->getCurrentSampleNorm(); will return 0-1 normalised value from pot
#include "mbed.h"
#include <math.h>
class Potentiometer //Begin Potentiometer class definition
{
private: //Private data member declaration
AnalogIn inputSignal; //Declaration of AnalogIn object
float VDD, currentSampleNorm, currentSampleVolts; //Float variables to speficy the value of VDD and most recent samples
public: // Public declarations
Potentiometer(PinName pin, float v) : inputSignal(pin), VDD(v) {} //Constructor - user provided pin name assigned to AnalogIn...
//VDD is also provided to determine maximum measurable voltage
float amplitudeVolts(void) //Public member function to measure the amplitude in volts
{
return (inputSignal.read()*VDD); //Scales the 0.0-1.0 value by VDD to read the input in volts
}
float amplitudeNorm(void) //Public member function to measure the normalised amplitude
{
return inputSignal.read(); //Returns the ADC value normalised to range 0.0 - 1.0
}
void sample(void) //Public member function to sample an analogue voltage
{
currentSampleNorm = inputSignal.read(); //Stores the current ADC value to the class's data member for normalised values (0.0 - 1.0)
currentSampleVolts = currentSampleNorm * VDD; //Converts the normalised value to the equivalent voltage (0.0 - 3.3 V) and stores this information
}
float getCurrentSampleVolts(void) //Public member function to return the most recent sample from the potentiometer (in volts)
{
return currentSampleVolts; //Return the contents of the data member currentSampleVolts
}
float getCurrentSampleNorm(void) //Public member function to return the most recent sample from the potentiometer (normalised)
{
return currentSampleNorm; //Return the contents of the data member currentSampleNorm
}
};
class SamplingPotentiometer : public Potentiometer {
private:
float samplingFrequency, samplingPeriod;
Ticker sampler;
public:
SamplingPotentiometer(PinName p, float v, float fs): Potentiometer(p, v), samplingFrequency(fs){
samplingPeriod = 1/samplingFrequency;
sampler.attach(callback(this, &SamplingPotentiometer::sample), samplingPeriod);
}
};