-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.ts
240 lines (222 loc) · 6.71 KB
/
main.ts
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
* makecode Four Digit Display (TM1637) Package.
* From microbit/micropython Chinese community.
* http://www.micropython.org.cn
*/
/**
* Four Digit Display
*/
//% weight=100 color=#50A820 icon="8"
namespace TM1637 {
let TM1637_CMD1 = 0x40;
let TM1637_CMD2 = 0xC0;
let TM1637_CMD3 = 0x80;
let _SEGMENTS = [0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71];
/**
* TM1637 LED display
*/
export class TM1637LEDs {
buf: Buffer;
clk: DigitalPin;
dio: DigitalPin;
_ON: number;
brightness: number;
count: number; // number of LEDs
/**
* initial TM1637
*/
init(): void {
pins.digitalWritePin(this.clk, 0);
pins.digitalWritePin(this.dio, 0);
this._ON = 8;
this.buf = pins.createBuffer(this.count);
this.clear();
}
/**
* Start
*/
_start() {
pins.digitalWritePin(this.dio, 0);
pins.digitalWritePin(this.clk, 0);
}
/**
* Stop
*/
_stop() {
pins.digitalWritePin(this.dio, 0);
pins.digitalWritePin(this.clk, 1);
pins.digitalWritePin(this.dio, 1);
}
/**
* send command1
*/
_write_data_cmd() {
this._start();
this._write_byte(TM1637_CMD1);
this._stop();
}
/**
* send command3
*/
_write_dsp_ctrl() {
this._start();
this._write_byte(TM1637_CMD3 | this._ON | this.brightness);
this._stop();
}
/**
* send a byte to 2-wire interface
*/
_write_byte(b: number) {
for (let i = 0; i < 8; i++) {
pins.digitalWritePin(this.dio, (b >> i) & 1);
pins.digitalWritePin(this.clk, 1);
pins.digitalWritePin(this.clk, 0);
}
pins.digitalWritePin(this.clk, 1);
pins.digitalWritePin(this.clk, 0);
}
/**
* set TM1637 intensity, range is [0-8], 0 is off.
* @param val the brightness of the TM1637, eg: 7
*/
//% blockId="TM1637_set_intensity" block="%tm|set intensity %val"
//% weight=50 blockGap=8
//% parts="TM1637"
intensity(val: number = 7) {
if (val < 1) {
this.off();
return;
}
if (val > 8) val = 8;
this._ON = 8;
this.brightness = val - 1;
this._write_data_cmd();
this._write_dsp_ctrl();
}
/**
* set data to TM1637, with given bit
*/
_dat(bit: number, dat: number) {
this._write_data_cmd();
this._start();
this._write_byte(TM1637_CMD2 | (bit % this.count))
this._write_byte(dat);
this._stop();
this._write_dsp_ctrl();
}
/**
* show a number in given position.
* @param num number will show, eg: 5
* @param bit the position of the LED, eg: 0
*/
//% blockId="TM1637_showbit" block="%tm|show digit %num |at %bit"
//% weight=90 blockGap=8
//% parts="TM1637"
showbit(num: number = 5, bit: number = 0) {
this.buf[bit % this.count] = _SEGMENTS[num % 16]
this._dat(bit, _SEGMENTS[num % 16])
}
/**
* show a number.
* @param num is a number, eg: 0
*/
//% blockId="TM1637_shownum" block="%tm|show number %num"
//% weight=91 blockGap=8
//% parts="TM1637"
showNumber(num: number) {
if (num < 0) {
this._dat(0, 0x40) // '-'
num = -num
}
else
this.showbit(Math.idiv(num, 1000) % 10)
this.showbit(num % 10, 3)
this.showbit(Math.idiv(num, 10) % 10, 2)
this.showbit(Math.idiv(num, 100) % 10, 1)
}
/**
* show a hex number.
* @param num is a hex number, eg: 0
*/
//% blockId="TM1637_showhex" block="%tm|show hex number %num"
//% weight=90 blockGap=8
//% parts="TM1637"
showHex(num: number) {
if (num < 0) {
this._dat(0, 0x40) // '-'
num = -num
}
else
this.showbit((num >> 12) % 16)
this.showbit(num % 16, 3)
this.showbit((num >> 4) % 16, 2)
this.showbit((num >> 8) % 16, 1)
}
/**
* show or hide dot point.
* @param bit is the position, eg: 1
* @param show is show/hide dp, eg: true
*/
//% blockId="TM1637_showDP" block="%tm|DotPoint at %bit|show %show"
//% weight=70 blockGap=8
//% parts="TM1637"
showDP(bit: number = 1, show: boolean = true) {
bit = bit % this.count
if (show) this._dat(bit, this.buf[bit] | 0x80)
else this._dat(bit, this.buf[bit] & 0x7F)
}
/**
* clear LED.
*/
//% blockId="TM1637_clear" block="clear %tm"
//% weight=80 blockGap=8
//% parts="TM1637"
clear() {
for (let i = 0; i < this.count; i++) {
this._dat(i, 0)
this.buf[i] = 0
}
}
/**
* turn on LED.
*/
//% blockId="TM1637_on" block="turn on %tm"
//% weight=86 blockGap=8
//% parts="TM1637"
on() {
this._ON = 8;
this._write_data_cmd();
this._write_dsp_ctrl();
}
/**
* turn off LED.
*/
//% blockId="TM1637_off" block="turn off %tm"
//% weight=85 blockGap=8
//% parts="TM1637"
off() {
this._ON = 0;
this._write_data_cmd();
this._write_dsp_ctrl();
}
}
/**
* create a TM1637 object.
* @param clk the CLK pin for TM1637, eg: DigitalPin.P1
* @param dio the DIO pin for TM1637, eg: DigitalPin.P2
* @param intensity the brightness of the LED, eg: 7
* @param count the count of the LED, eg: 4
*/
//% weight=200 blockGap=8
//% blockId="TM1637_create" block="CLK %clk|DIO %dio|intensity %intensity|LED count %count"
export function create(clk: DigitalPin, dio: DigitalPin, intensity: number, count: number): TM1637LEDs {
let tm = new TM1637LEDs();
tm.clk = clk;
tm.dio = dio;
if ((count < 1) || (count > 5)) count = 4;
tm.count = count;
tm.brightness = intensity;
tm.init();
return tm;
}
}