· 5 years ago · Jun 05, 2020, 07:48 PM
1from multiprocessing.connection import Client
2from array import array
3import kivy
4import sqlite3
5
6kivy.require('1.0.6') # replace with your current kivy version !
7
8from kivy.app import App
9from kivy.uix.button import Button
10from kivy.uix.togglebutton import ToggleButton
11from kivy.uix.gridlayout import GridLayout
12from kivy.uix.image import Image
13from kivy.uix.slider import Slider
14from kivy.clock import Clock
15from kivy.graphics import Color, Rectangle
16
17# for now, use a global for blink speed (better implementation TBD):
18speed = 1.0
19speedb = 1.0
20speedc = 1.0
21speedd = 1.0
22speede = 1.0
23
24# Set up GPIO:
25ledPin = 1.3
26buttonPin = 1.3
27
28
29# Define some helper functions:
30
31# This callback will be bound to the LED toggle and Beep button:
32def press_callback(obj):
33 if obj.text == 'send':
34 if obj.state == "down":
35 print("button on")
36
37 else:
38 print("button off")
39
40
41# This is called when the slider is updated:
42
43'I commented it out becouse it was annoying seeing it'
44
45
46def update_speed(obj, value):
47 pass
48 # speed = obj.value
49 # print("Le Slider 1:" + str(obj.value))
50 # speedb = obj.value
51 # print("Le Slider 2:" + str(obj.value))
52 # speedc = obj.value
53 # print("Le Slider 3:" + str(obj.value))
54 # speedd = obj.value
55 # print("Le Slider 4:" + str(obj.value))
56 # speede = obj.value
57 # print("Le Slider 5:" + str(obj.value))
58
59
60# Modify the Button Class to update according to GPIO input:
61#class InputButton(Button):
62 #def update(self, dt):
63 # btn = Button(text="Boutton démarrer!")
64
65
66class MyApp(App):
67 values = []
68 address = ('localhost', 6000)
69
70 # this method is called when button pressed
71 def save_values(self, _):
72 if self.values:
73 self.values.clear()
74 for wid in self.root.children:
75 if isinstance(wid, Slider):
76 self.values.append(wid.value)
77 print(self.values)
78 conn = sqlite3.connect('oklm.db')
79 c = conn.cursor()
80 # Create table
81 c.execute("CREATE TABLE IF NOT EXISTS Sliders2 (val1 real, val2 real, val3 real, val4 real, val5 real)")
82
83 # Insert a row of data
84 c.execute("INSERT INTO Sliders2 (val1, val2, val3, val4, val5) values (?, ?, ?, ?, ?)", self.values)
85
86 # Save (commit) the changes
87 conn.commit()
88
89 # We can also close the connection if we are done with it.
90 # Just be sure any changes have been committed or they will be lost.
91 conn.close()
92
93 def send_values2(self, _):
94 if self.values:
95 self.values.clear()
96 for wid in self.root.children:
97 if isinstance(wid, Slider):
98 self.values.append(wid.value)
99 # print(self.values)
100 try:
101 with Client(self.address, authkey=b'SomePassword') as conn:
102 arr = array('f', self.values)
103 conn.send_bytes(arr)
104 okmessage = conn.recv_bytes()
105 print(okmessage)
106 except Exception as err:
107 print(str(err))
108
109 def build(self):
110 # Set up the layout:
111 layout = GridLayout(cols=5, spacing=30, padding=30, row_default_height=150)
112
113 # Make the background gray:
114 with layout.canvas.before:
115 Color(3.0, 0.7, 0.3, 1.0)
116 self.rect = Rectangle(size=(800, 600), pos=layout.pos)
117
118 # Instantiate the first UI object (the GPIO input indicator):
119 #inputDisplay = InputButton(text="Démarrer")
120
121 save = Button(text='save', on_release=self.save_values)
122
123 send = ToggleButton(text='send', on_release=self.send_values2)
124 send.bind(on_press=press_callback)
125
126 # Schedule the update of the state of the GPIO input button:
127 #Clock.schedule_interval(inputDisplay.update, 1.0 / 10.0)
128
129 # Create the rest of the UI objects (and bind them to callbacks, if necessary):
130 #outputControl = ToggleButton(text="Arrêt")
131 #outputControl.bind(on_press=press_callback)
132 #wimg = Image(source='logo.png')
133 speedSlider = Slider(orientation='vertical', min=1, max=30, value=speed)
134 speedSlider.bind(on_touch_down=update_speed, on_touch_move=update_speed)
135
136 speedbSlider = Slider(orientation='vertical', min=1, max=30, value=speedb)
137 speedbSlider.bind(on_touch_down=update_speed, on_touch_move=update_speed)
138
139 speedcSlider = Slider(orientation='vertical', min=1, max=30, value=speedc)
140 speedcSlider.bind(on_touch_down=update_speed, on_touch_move=update_speed)
141
142 speeddSlider = Slider(orientation='vertical', min=1, max=30, value=speedd)
143 speeddSlider.bind(on_touch_down=update_speed, on_touch_move=update_speed)
144
145 speedeSlider = Slider(orientation='vertical', min=1, max=30, value=speede)
146 speedeSlider.bind(on_touch_down=update_speed, on_touch_move=update_speed)
147
148 # Add the UI elements to the layout:
149 #layout.add_widget(wimg)
150 #layout.add_widget(inputDisplay)
151 #layout.add_widget(outputControl)
152 layout.add_widget(speedSlider)
153 layout.add_widget(speedbSlider)
154 layout.add_widget(speedcSlider)
155 layout.add_widget(speeddSlider)
156 layout.add_widget(speedeSlider)
157 layout.add_widget(save)
158 layout.add_widget(send)
159
160 return layout
161
162
163if __name__ == '__main__':
164 MyApp().run()