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