-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
116 lines (106 loc) · 4.69 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
/**
* Communicate with the Sparkfun Qwiic Twist RGB encoder
*/
//% color=#007A4B weight=30 icon="\uf0d1"
namespace twist {
enum TwistRegisters {
Id = 0,
Status = 1,
Version = 2,
EnableInts = 4,
Count = 5,
Difference = 7,
LastEncoderEvent = 9,
LastButtonEvent = 8,
Red = 13,
Green = 14,
Blue = 15,
ConnectRed = 16,
ConnectGreen = 18,
ConnectBlue = 20,
TurnIntTimeout = 22,
ChangeAddress = 24,
Limit = 25
}
/** Set the RGB color (light up the knob) */
//% address.defl=63 r.defl=255 g.defl=255 b.defl=255 address.min=0 address.max=255 r.min=0 r.max=255 g.min=0 g.max=255 b.min=0 b.max=255
//% blockId=twist_set_color
//% block="Set color|address%address|red%r|green%g|blue%b"
export function setColor(address: number, r: number, g: number, b: number): void {
setRed(address, r)
setGreen(address, g)
setBlue(address, b)
}
/** Set red (light up the knob) */
//% address.defl=63 r.defl=255 address.min=0 address.max=255 r.min=0 r.max=255
//% blockId=twist_set_red
//% block="Set red|address%address|red%r"
export function setRed(address: number, r: number) {
pins.i2cWriteNumber(address, TwistRegisters.Red, NumberFormat.UInt8LE, true)
pins.i2cWriteNumber(address, r, NumberFormat.UInt8LE, false)
}
/** Set green (light up the knob) */
//% address.defl=63 g.defl=255 address.min=0 address.max=255 g.min=0 g.max=255
//% blockId=twist_set_green
//% block="Set green|address%address|green%g"
export function setGreen(address: number, g: number) {
pins.i2cWriteNumber(address, TwistRegisters.Green, NumberFormat.UInt8LE, true)
pins.i2cWriteNumber(address, g, NumberFormat.UInt8LE, false)
}
/** Set blue (light up the knob) */
//% address.defl=63 b.defl=255 address.min=0 address.max=255 b.min=0 b.max=255
//% blockId=twist_set_blue
//% block="Set blue|address%address|blue%b"
export function setBlue(address: number, b: number) {
pins.i2cWriteNumber(address, TwistRegisters.Blue, NumberFormat.UInt8LE, true)
pins.i2cWriteNumber(address, b, NumberFormat.UInt8LE, false)
}
/** Get current red value */
//% address.defl=63 address.min=0 address.max=255
//% blockId=twist_get_red
//% block="Get red|address%address"
export function getRed(address: number): number {
pins.i2cWriteNumber(address, TwistRegisters.Red, NumberFormat.UInt8LE, false);
return pins.i2cReadNumber(address, NumberFormat.UInt8LE, false);
}
/** Get current green value */
//% address.defl=63 address.min=0 address.max=255
//% blockId=twist_get_green
//% block="Get green|address%address"
export function getGreen(address: number): number {
pins.i2cWriteNumber(address, TwistRegisters.Green, NumberFormat.UInt8LE, false);
return pins.i2cReadNumber(address, NumberFormat.UInt8LE, false);
}
/** Get current blue value */
//% address.defl=63 address.min=0 address.max=255
//% blockId=twist_get_blue
//% block="Get blue|address%address"
export function getBlue(address: number): number {
pins.i2cWriteNumber(address, TwistRegisters.Blue, NumberFormat.UInt8LE, false);
return pins.i2cReadNumber(address, NumberFormat.UInt8LE, false);
}
/** Get the current encoder count */
//% address.defl=63 address.min=0 address.max=255
//% blockId=twist_get_count
//% block="Get count|address%address"
export function getCount(address: number): number {
pins.i2cWriteNumber(address, TwistRegisters.Count, NumberFormat.UInt8LE, false);
return pins.i2cReadNumber(address, NumberFormat.Int16LE);
}
/** Get the current encoder count limit (0 = no limit) */
//% address.defl=63 address.min=0 address.max=255
//% blockId=twist_get_limit
//% block="Get limit|address%address"
export function getLimit(address: number): number {
pins.i2cWriteNumber(address, TwistRegisters.Limit, NumberFormat.Int8LE);
return pins.i2cReadNumber(address, NumberFormat.UInt16LE);
}
/** Set the current encoder count limit (0 = no limit) */
//% address.defl=63 address.min=0 address.max=255 limit.defl=0 limit.min=0 limit.max=65535
//% blockId=twist_set_limit
//% block="Set limit|address%address|limit%limit"
export function setLimit(address: number, limit: number): void {
pins.i2cWriteNumber(address, TwistRegisters.Limit, NumberFormat.Int8LE, true);
pins.i2cWriteNumber(address, limit, NumberFormat.UInt16LE);
}
}