-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDigitalHeadUnitDriver.cpp
More file actions
113 lines (92 loc) · 3.95 KB
/
DigitalHeadUnitDriver.cpp
File metadata and controls
113 lines (92 loc) · 3.95 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "Arduino.h"
#include "HeadUnitDriver.h"
#include "PulseGenerator.h"
#include <Stopwatch.h>
// --------------------------------------------------------------------------------------------------------------------
// Constants
// --------------------------------------------------------------------------------------------------------------------
// Time of one tick of the communication in microseconds (uS).
const uint32_t TickTimeMicroSeconds = 562;
// This is the address that will be sent to the head unit that the kenwood style radios understand.
const uint8_t KenwoodAddress = 0xB9;
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Constructor
// --------------------------------------------------------------------------------------------------------------------
DigitalHeadUnitDriver::DigitalHeadUnitDriver(
uint8_t digitalMosfetPin,
uint32_t commandPauseMS,
KenwoodCommand buttonPressCommand
)
: _mosfetPin(digitalMosfetPin)
, _commandPauseMS(commandPauseMS)
, _forceRun(false)
, _runKnobPressed(false)
, _count(0)
, _state(InternalState::Off)
, _knobPressedCommand(buttonPressCommand)
{
PulseGenerator::Instance().AssignPin(_mosfetPin);
}
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// IHeadUnitDriver interface methods
// --------------------------------------------------------------------------------------------------------------------
void DigitalHeadUnitDriver::StartDriver()
{
}
void DigitalHeadUnitDriver::UpdateCounters()
{
}
void DigitalHeadUnitDriver::RunIteration()
{
if (_runKnobPressed
&& !PulseGenerator::Instance().IsSendInProgress())
{
WriteData(KenwoodAddress, _knobPressedCommand);
_runKnobPressed = false;
// Here we exit as we don't want to run an increment/decrement command right in a row after running the press
// command. The increment/decrement will be caught on the next iteration.
return;
}
if (
_count != 0
&& !PulseGenerator::Instance().IsSendInProgress())
{
_forceRun = false;
// Run one command per iteration; note that
KenwoodCommand command = _state == InternalState::Increasing
? KenwoodCommand::VolumeUp
: KenwoodCommand::VolumeDown;
WriteData(KenwoodAddress, command);
int increment = _state == InternalState::Increasing ? -1 : 1;
_count += increment;
_state == _count == 0
? InternalState::Off
: _count > 0
? InternalState::Increasing
: InternalState::Decreasing;
}
}
void DigitalHeadUnitDriver::HandleKnobChange(int delta, InternalState newState)
{
if (_count == 0)
{
_state = newState;
_forceRun = true;
}
_count += newState == InternalState::Increasing ? delta : -delta;
}
void DigitalHeadUnitDriver::HandleKnobPressed()
{
_runKnobPressed = true;
}
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Write methods - idea taken from https://init6.pomorze.pl/projects/kenwood_ford/
// --------------------------------------------------------------------------------------------------------------------
void DigitalHeadUnitDriver::WriteData(uint8_t addr, KenwoodCommand command)
{
PulseGenerator::Instance().SendPulseSequence(PulseFormat::NEC, addr, command);
}
// --------------------------------------------------------------------------------------------------------------------