-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLEDS.cpp
72 lines (63 loc) · 1.63 KB
/
LEDS.cpp
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
#include <Arduino.h>
#include "PINS.h"
#include "MCP.h"
#include "LEDS.h"
void LEDs::Init()
{
// the 595 controls the 8 data LEDs
pinMode(PIN_LEDS_DS, OUTPUT);
pinMode(PIN_LEDS_ST, OUTPUT);
pinMode(PIN_LEDS_SH, OUTPUT);
ShiftOut(0x00);
m_LastData = 0;
// the 4 control LEDs have a direct link
for (int Pin = MCP::eInput; Pin <= MCP::eRun; Pin++)
{
pinMode(m_pDirectControlPins[Pin], OUTPUT);
digitalWrite(m_pDirectControlPins[Pin], LOW);
}
m_LastControl = 0;
}
void LEDs::ShiftOut(byte LEDs)
{
digitalWrite(PIN_LEDS_ST, LOW);
// shift out the bits:
shiftOut(PIN_LEDS_DS, PIN_LEDS_SH, LSBFIRST, LEDs);
//take the latch pin high so the LEDs will light up:
digitalWrite(PIN_LEDS_ST, HIGH);
}
void LEDs::Display(byte Data, byte Control)
{
// update the data and control LEDs
if (Data != m_LastData)
{
ShiftOut(Data);
m_LastData = Data;
}
if (Control != m_LastControl)
{
for (int Pin = MCP::eInput; Pin < MCP::eRun; Pin++)
{
digitalWrite(m_pDirectControlPins[Pin], bitRead(Control, Pin)?HIGH:LOW);
}
if (bitRead(Control, MCP::eRun))
{
// the Run LED can do PWM, use the upper 4 bits; 0000 = max brightness (255) 1111 = min (16)
analogWrite(m_pDirectControlPins[MCP::eRun], 0xFF - (Control & 0xF0));
}
else
{
digitalWrite(m_pDirectControlPins[MCP::eRun], LOW);
}
m_LastControl = Control;
}
}
// pin mapping in the same order as MCP::tMode
byte LEDs::m_pDirectControlPins[] =
{
PIN_LED_INP,
PIN_LED_ADDR,
PIN_LED_MEM,
PIN_LED_RUN_PWM
};
LEDs leds = LEDs();