Buggy bois / Mbed 2 deprecated headache

Dependencies:   mbed

Committer:
mazdo25
Date:
Sat Mar 09 14:27:48 2019 +0000
Revision:
3:01b5e80d842d
Parent:
1:813f4b17ae65
Child:
4:208f5279143a
Initialization working, sensors not

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mazdo25 1:813f4b17ae65 1 class lineSensor
mazdo25 1:813f4b17ae65 2 {
mazdo25 1:813f4b17ae65 3
mazdo25 1:813f4b17ae65 4 private:
mazdo25 3:01b5e80d842d 5
mazdo25 1:813f4b17ae65 6 DigitalOut emitter;
mazdo25 1:813f4b17ae65 7 AnalogIn receiver;
mazdo25 1:813f4b17ae65 8 float static const vREF = 3.3f;
mazdo25 1:813f4b17ae65 9 float lineVoltage;
mazdo25 3:01b5e80d842d 10 Timeout sampler;
mazdo25 1:813f4b17ae65 11
mazdo25 1:813f4b17ae65 12 public:
mazdo25 3:01b5e80d842d 13 float static const sampleTime = 0.1f;
mazdo25 1:813f4b17ae65 14
mazdo25 1:813f4b17ae65 15 lineSensor(PinName E, PinName R):emitter(E), receiver(R)
mazdo25 1:813f4b17ae65 16 {
mazdo25 3:01b5e80d842d 17 sampler.detach();
mazdo25 3:01b5e80d842d 18 }
mazdo25 1:813f4b17ae65 19
mazdo25 1:813f4b17ae65 20 //Turn on the emitter i.e emit a light
mazdo25 1:813f4b17ae65 21 void turnOn(void)
mazdo25 1:813f4b17ae65 22 {
mazdo25 1:813f4b17ae65 23 emitter.write(1);
mazdo25 1:813f4b17ae65 24 }
mazdo25 1:813f4b17ae65 25
mazdo25 1:813f4b17ae65 26 //turn off the emitter i.e don't emit a light
mazdo25 1:813f4b17ae65 27 void turnOff(void)
mazdo25 1:813f4b17ae65 28 {
mazdo25 1:813f4b17ae65 29 emitter.write(0);
mazdo25 1:813f4b17ae65 30 }
mazdo25 1:813f4b17ae65 31
mazdo25 1:813f4b17ae65 32 //toggle the emitter i.e if its on turn it off, if its off turn it on
mazdo25 1:813f4b17ae65 33 void toggleEmitter(void)
mazdo25 1:813f4b17ae65 34 {
mazdo25 1:813f4b17ae65 35 emitter.write(!emitter.read());
mazdo25 1:813f4b17ae65 36 }
mazdo25 1:813f4b17ae65 37
mazdo25 1:813f4b17ae65 38 //return an int representing the state of the emitter i.e is it on or off where 1 represents on and 0 represents off
mazdo25 1:813f4b17ae65 39 int returnEmitterState(void)
mazdo25 1:813f4b17ae65 40 {
mazdo25 1:813f4b17ae65 41 return emitter.read();
mazdo25 1:813f4b17ae65 42 }
mazdo25 1:813f4b17ae65 43
mazdo25 3:01b5e80d842d 44 void sample(void)
mazdo25 3:01b5e80d842d 45 {
mazdo25 3:01b5e80d842d 46 turnOn();
mazdo25 3:01b5e80d842d 47 sampler.attach(callback(this, &lineSensor::turnOff),sampleTime);
mazdo25 3:01b5e80d842d 48 calcLineVoltage();
mazdo25 3:01b5e80d842d 49 }
mazdo25 3:01b5e80d842d 50
mazdo25 1:813f4b17ae65 51 void calcLineVoltage(void)
mazdo25 1:813f4b17ae65 52 {
mazdo25 1:813f4b17ae65 53 lineVoltage = receiver.read() * vREF;
mazdo25 1:813f4b17ae65 54 }
mazdo25 1:813f4b17ae65 55
mazdo25 1:813f4b17ae65 56 float returnLineVoltage(void)
mazdo25 1:813f4b17ae65 57 {
mazdo25 1:813f4b17ae65 58 return lineVoltage;
mazdo25 1:813f4b17ae65 59 }
mazdo25 1:813f4b17ae65 60
mazdo25 1:813f4b17ae65 61 };