· 5 years ago · Jun 04, 2020, 09:28 PM
1import sqlite3
2import kivy
3import socket
4from array import array
5from kivy.app import App
6from kivy.uix.button import Button
7from kivy.uix.togglebutton import ToggleButton
8from kivy.uix.gridlayout import GridLayout
9from kivy.uix.image import Image
10from kivy.uix.slider import Slider
11from kivy.clock import Clock
12from kivy.graphics import Color, Rectangle
13
14
15# for now, use a global for blink speed (better implementation TBD):
16speed = 1.0
17speedb = 1.0
18speedc = 1.0
19speedd = 1.0
20speede = 1.0
21
22# Set up GPIO:
23ledPin = 1.3
24buttonPin = 1.3
25
26
27
28# Define some helper functions:
29
30# This callback will be bound to the LED toggle and Beep button:
31def press_callback(obj):
32 if obj.text == 'Arrêt':
33 if obj.state == "down":
34 print("button on")
35
36 else:
37 print("button off")
38
39# This is called when the slider is updated:
40
41'I commented it out becouse it was annoying seeing it'
42def update_speed(obj, value):
43 pass
44 # speed = obj.value
45 # print("Le Slider 1:" + str(obj.value))
46 # speedb = obj.value
47 # print("Le Slider 2:" + str(obj.value))
48 # speedc = obj.value
49 # print("Le Slider 3:" + str(obj.value))
50 # speedd = obj.value
51 # print("Le Slider 4:" + str(obj.value))
52 # speede = obj.value
53 # print("Le Slider 5:" + str(obj.value))
54
55
56# Modify the Button Class to update according to GPIO input:
57class InputButton(Button):
58 def update(self, dt):
59 btn = Button(text="Boutton démarrer!")
60
61class MyApp(App):
62
63 values = []
64
65 # this method is called when button pressed
66 def save_values(self, _):
67 if self.values:
68 self.values.clear()
69 for wid in self.root.children:
70 if isinstance(wid, Slider):
71 self.values.append(wid.value)
72 print(self.values)
73
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 host = 'localhost'
90 port = 15555
91 command = 'start' + 'F (Frequency)' + str('?') + 'D' + str('?') + 'r' + str('?') + 'e' + str('?') + 't' + str('?') + 'Stop',self.values
92
93 socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
94 socket.connect((host, port))
95 print("Connection on {}".format(port))
96
97 socket.send(command)
98
99 def build(self):
100 # Set up the layout:
101 layout = GridLayout(cols=5, spacing=30, padding=30, row_default_height=150)
102
103 # Make the background gray:
104 with layout.canvas.before:
105 Color(3.0, 0.7, 0.3, 1.0)
106 self.rect = Rectangle(size=(800, 600), pos=layout.pos)
107
108 # Instantiate the first UI object (the GPIO input indicator):
109 inputDisplay = InputButton(text="Démarrer")
110
111 save = Button(text='save', on_release=self.save_values)
112
113 # Schedule the update of the state of the GPIO input button:
114 Clock.schedule_interval(inputDisplay.update, 1.0 / 10.0)
115
116 # Create the rest of the UI objects (and bind them to callbacks, if necessary):
117 outputControl = ToggleButton(text="Arrêt")
118 outputControl.bind(on_press=press_callback)
119 wimg = Image(source='logo.png')
120 speedSlider = Slider(orientation='vertical', min=1, max=30, value=speed)
121 speedSlider.bind(on_touch_down=update_speed, on_touch_move=update_speed)
122
123 speedbSlider = Slider(orientation='vertical', min=1, max=30, value=speedb)
124 speedbSlider.bind(on_touch_down=update_speed, on_touch_move=update_speed)
125
126 speedcSlider = Slider(orientation='vertical', min=1, max=30, value=speedc)
127 speedcSlider.bind(on_touch_down=update_speed, on_touch_move=update_speed)
128
129 speeddSlider = Slider(orientation='vertical', min=1, max=30, value=speedd)
130 speeddSlider.bind(on_touch_down=update_speed, on_touch_move=update_speed)
131
132 speedeSlider = Slider(orientation='vertical', min=1, max=30, value=speede)
133 speedeSlider.bind(on_touch_down=update_speed, on_touch_move=update_speed)
134
135 # Add the UI elements to the layout:
136 layout.add_widget(wimg)
137 layout.add_widget(inputDisplay)
138 layout.add_widget(outputControl)
139 layout.add_widget(speedSlider)
140 layout.add_widget(speedbSlider)
141 layout.add_widget(speedcSlider)
142 layout.add_widget(speeddSlider)
143 layout.add_widget(speedeSlider)
144 layout.add_widget(save)
145
146 return layout
147
148
149if __name__ == '__main__':
150 MyApp().run()