-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResistor.h
75 lines (61 loc) · 2.3 KB
/
Resistor.h
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#pragma once
#include "Definitions.h"
class Resistor
{
public:
static bool DetectResistor()
{
for (size_t Index = 0; Index != ArrayCount(MutuallyExclusivePins); ++Index)
{
auto
FirstPin = MutuallyExclusivePins[Index].First,
SecondPin = MutuallyExclusivePins[Index].Second,
ThirdPin = MutuallyExclusivePins[Index].Third;
if (IsReadingValid(SetUpAndExclusivelyMeasureCurrent(FirstPin, SecondPin, ThirdPin)))
{
Display::GetInstance().setCursor(48, 16);
Display::GetInstance().println("Resistor");
// Resistance
auto Resistance = SetUpAndMeasureResistance(FirstPin, SecondPin);
Display::GetInstance().setCursor(48, 32);
Display::GetInstance().println(String("R: ") + DoubleToString(Resistance) + static_cast<char>(233));
// Voltage drop
auto VDrop = MeasureVoltage(FirstPin, SecondPin);
Display::GetInstance().setCursor(48, 40);
Display::GetInstance().println(String("Vdrop: ") + DoubleToString(VDrop) + "V");
// Pin 1
Display::GetInstance().setCursor(4, 32);
Display::GetInstance().print(FirstPin);
// Pin 2
Display::GetInstance().setCursor(39, 32);
Display::GetInstance().print(SecondPin);
Display::GetInstance().drawBitmap(0, 16, RESISTOR, 48, 48, WHITE);
StaticJsonBuffer<JSON_OBJECT_SIZE(3)> Buffer;
auto & Root = Buffer.createObject();
Root["Component"] = "Resistor";
Root["Resistance"] = Resistance;
Root["Voltage drop"] = VDrop;
FirmataConnection::SendResultToHost(Root);
return true;
}
}
return false;
}
private:
static double SetUpAndMeasureResistance(uint8_t FromPin, uint8_t ToPin)
{
digitalWrite(FromPin, HIGH);
digitalWrite(ToPin, LOW);
delay(StablisationDelay);
double AverageResistance = 0;
for (auto Counter = 0; Counter != 100; ++Counter)
{
double ProbedComponentVoltage = MeasureVoltage(FromPin, ToPin);
double Current = (RefVoltage - ProbedComponentVoltage) / (PinResistances[FromPin] + PinResistances[ToPin]);
AverageResistance += ProbedComponentVoltage / Current;
}
digitalWrite(FromPin, LOW);
digitalWrite(ToPin, LOW);
return (AverageResistance / 100.f);
}
};