-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2c.lua
More file actions
121 lines (106 loc) · 2.79 KB
/
i2c.lua
File metadata and controls
121 lines (106 loc) · 2.79 KB
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
--id=0
pinSDA=5
pinSCL=6
--device_addr=0x20
Value = 0; --0 to 1
value_min_step=0.1
Saturation = 0; --0 to 1
HUE = 0; --0 to 360
function write_reg(dev_addr,tx_bit)
i2c.start(0)
i2c.address(0, dev_addr, i2c.TRANSMITTER)
i2c.write(0,tx_bit)
i2c.stop(0)
end
function read_reg(dev_addr)
i2c.start(0)
i2c.address(0, dev_addr , i2c.RECEIVER)
c=i2c.read(0,6)
i2c.stop(0)
return c
end
function NJ_INIT()
i2c.start(0)
i2c.address(0, 0x52, i2c.TRANSMITTER)
i2c.write(0,0x40,0x00)
i2c.stop(0)
end
function NJ ()
write_reg(0x52,0x00)
reg = read_reg(0x52)
--read the buttons
--print("x" .. string.byte(reg, 1))
--print("y" .. string.byte(reg, 2))
X_Value = string.byte(reg, 1)
Y_Value = string.byte(reg, 2)
X_Value = (X_Value-128)/128
Y_Value = (Y_Value-128)/128
--print (X_Value,Y_Value)
--sat = laenge berechnen sqr(x²+y²)
Saturation= (math.sqrt((X_Value*X_Value)+(Y_Value*Y_Value)))/1.4
if Saturation>1 then
Saturation=1
end
--print("sat",Saturation)
--errechnen den hue winkel
if X_Value >= 0 then --x positive
if Y_Value >= 0 then --Q1
HUE = math.atan(Y_Value/X_Value)
else --q4
HUE = math.atan(Y_Value/X_Value)+360
end
else --x negative ->q2 or q3
HUE = math.atan(Y_Value/X_Value)+180
end
print(HUE)
HUE =HUE/360
--Saturation = string.byte(reg, 1)
--HUE = string.byte(reg, 2)
if string.byte(reg, 6) == 0x1 then --upper button "c"
if Value<(1-value_min_step) then
Value=Value+value_min_step
else
Value=1
end
print ("plus:", Value)
--ws2812.writergb(7, string.char( 0,0,0, 0 , string.byte(reg, 1), string.byte(reg, 2) ))
elseif string.byte(reg, 6) == 0x2 then --lower button "z"
if Value>value_min_step then
Value=Value-value_min_step
else
value=0
end
print ("minus:", Value)
--ws2812.writergb(7,string.char( string.byte(reg, 1), string.byte(reg, 2), 0, 0, 0, 0))
elseif string.byte(reg, 6) == 0x0 then --both buttons
print ("current value: ", Value)
--print (Value)
--ws2812.writergb(7, string.char(0, 0, 0, 0, 0, 0))
end
end
function timer_start(timer,intervall)
tmr.alarm(timer, intervall, 1, function() NJ() end )
end
function timer_stop(timer)
tmr.stop(timer)
end
--setup hardware
i2c.setup(0,pinSDA, pinSCL,i2c.SLOW)
--nunchuck init
write_reg(0x52,{[0]=0x40,[1]=0x00})
write_reg(0x52,0x00)
--init done
tmr.delay(10)
-- Conversion start command
write_reg(0x52,0x00)
tmr.delay(10)
--read values
reg = read_reg(0x52)
print(string.len(reg))
print(reg)
print(string.byte(reg, 1))
print(string.byte(reg, 2))
print(string.byte(reg, 3))
print(string.byte(reg, 4))
print(string.byte(reg, 5))
print(string.byte(reg, 6))