-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.ts
96 lines (90 loc) · 2.62 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
enum color {
//%block="RED"
red,
//%block="GREEN"
green,
//%block="BLUE"
blue
}
//%color=#A9BCF5 icon="\uf1fc" block="tcs3200"
namespace tcs3200 {
let output = 0
let s2: number
let s3: number
let out: number
let wavecount: number = 0
let freq = 0
let r_factor: number
let g_factor: number
let b_factor: number
function reset_wavecount(t: number): void {
wavecount = 0
basic.pause(t)
}
/**
* Calibrate the sensor.
* @param time waiting time; eg: 50, 10, 30, 100, 1000
*/
//%block="Calibration |S0 %S0|S1 %S1|S2 %S2|S3 %S3|OUT %OUT|time (ms)%time"
//%blockExternalInputs=true
export function calibration(S0: DigitalPin, S1: DigitalPin, S2: DigitalPin, S3: DigitalPin, OUT: DigitalPin, time: number): void {
s2 = S2
s3 = S3
out = OUT
freq = time
// set output frequency scale to 2%
pins.digitalWritePin(S0, 0)
pins.digitalWritePin(S1, 1)
// approx. wave freq count
pins.onPulsed(out, PulseValue.Low, function () {
wavecount++
})
// start calibration
serial.writeLine("Calibrating TCS3200")
basic.showString("C")
pins.digitalWritePin(s2, 0)
pins.digitalWritePin(s3, 0)
reset_wavecount(freq)
r_factor = 255 / wavecount
pins.digitalWritePin(s2, 1)
pins.digitalWritePin(s3, 1)
reset_wavecount(freq)
g_factor = 255 / wavecount
pins.digitalWritePin(s2, 0)
pins.digitalWritePin(s3, 1)
reset_wavecount(freq)
b_factor = 255 / wavecount
serial.writeLine("Calibration completed")
basic.clearScreen()
}
//%block="color: %choice"
export function color_choice(choice: color): number {
let returnValue: number
switch (choice) {
case 0:
pins.digitalWritePin(s2, 0)
pins.digitalWritePin(s3, 0)
reset_wavecount(freq)
returnValue = wavecount * r_factor
break
case 1:
pins.digitalWritePin(s2, 1)
pins.digitalWritePin(s3, 1)
reset_wavecount(freq)
returnValue = wavecount * g_factor
break
case 2:
pins.digitalWritePin(s2, 0)
pins.digitalWritePin(s3, 1)
reset_wavecount(freq)
returnValue = wavecount * b_factor
break
}
if (returnValue <= 255) {
return returnValue
}
else {
return 255
}
}
}