-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled_fullcolor_9.h
124 lines (97 loc) · 3.21 KB
/
led_fullcolor_9.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
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
114
115
116
117
118
119
120
121
122
123
124
#ifndef led_fullcolor_9_h
#define led_fullcolor_9_h
#include "arduino.h"
#include "digiRW.h"
enum LedColor {
RED = B001,
GREEN = B010,
BLUE = B100,
CYAN = B110,
YELLOW = B011,
MAGENTA = B101,
WHITE = B111,
OFF = B000
};
namespace Led{
LedColor output[9];
int interval;
const int cat_pins[]={0,1,2,3,4,10,11,12,13};
const int ano_pins[]={7,5,6};
//public
void init(); //初期化
void setInterval(int val); //ダイナミック点灯制御の切り替え周期[ms]
void set(byte bit,LedColor color); //{bit}番目のLEDを{color}色に設定
void setAll(LedColor color); //すべてのLEDを{color}色に設定
void lighting(); //設定された状態に発光({interval}[ms]以内に定期的に呼び出しする必要あり)
void lighting(LedColor data[]); //{data}の定義どおりに発光({interval}[ms]以内に定期的に呼び出しする必要あり)
void lightingWhile(unsigned long wait); //設定された状態に{wait}[ms]発光
void lightingWhile(LedColor data[],unsigned long wait); //{data}のどおりに{wait}[ms]発光
//private
void copy9(LedColor in[],LedColor out[]); //要素9のLedColor配列{in}を同サイズの{out}にコピー
void colorWrite(LedColor color); //アノード側LEDを{color}に設定して出力
void ledWrite(LedColor data[],byte bit); //カソード側LEDを{data}のどおり出力(ただしアノード側状態を{bit}で指定)
}
void Led::init(){
for(int i=0; i<9; i++){
pinMode( cat_pins[i], OUTPUT);
digiWrite(cat_pins[i],LOW);
}
for(int i=0; i<3; i++) pinMode( ano_pins[i], OUTPUT);
setAll(OFF);
interval=10;
}
void Led::setInterval(int val){
interval=val;
}
void Led::set(byte bit,LedColor color){
output[bit]=color;
}
void Led::setAll(LedColor color){
for(int i=0;i<9;i++) output[i]=color;
}
void Led::copy9(LedColor in[],LedColor out[]){
for(int i=0;i<9;i++) out[i]=in[i];
}
//色ごとのLEDのON/OFF出力(Anode:正論理)
void Led::colorWrite(LedColor color){
digiWrite( ano_pins[0], color&RED);
digiWrite( ano_pins[1], color&GREEN);
digiWrite( ano_pins[2], color&BLUE);
}
//9個のLEDのON/OFF出力(Cathode:負論理)
void Led::ledWrite(LedColor data[],byte bit){
for(int i=0;i<9;i++) digiWrite( cat_pins[i], !bitRead(data[i],bit));
}
void Led::lighting(){
static unsigned long last_time=0;
//前回実行(青色発光開始時)からinterval[ms]待つ
while( (millis()-last_time) < (unsigned long)interval );
//red
colorWrite(OFF);
ledWrite(output,0);
colorWrite(RED);
delay(interval);
//green
colorWrite(OFF);
ledWrite(output,1);
colorWrite(GREEN);
delay(interval);
//BLUE
colorWrite(OFF);
ledWrite(output,2);
colorWrite(BLUE);
last_time = millis();
}
void Led::lighting(LedColor data[]){
copy9(data,output);
lighting();
}
void Led::lightingWhile(LedColor data[],unsigned long wait){
unsigned long begin = millis();
while( (millis()-begin)>wait ) lighting(data);
}
void Led::lightingWhile(unsigned long wait){
unsigned long begin = millis();
while( (millis()-begin)>wait ) lighting();
}
#endif