· 5 years ago · Jun 05, 2020, 02:26 AM
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# for now, use a global for blink speed (better implementation TBD):
15speed = 1.0
16speedb = 1.0
17speedc = 1.0
18speedd = 1.0
19speede = 1.0
20
21# Set up GPIO:
22ledPin = 1.3
23buttonPin = 1.3
24host = 'localhost'
25port = 15555
26socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
27
28
29# Define some helper functions:
30
31
32
33
34# This is called when the slider is updated:
35
36'I commented it out becouse it was annoying seeing it'
37
38
39def update_speed(obj, value):
40 pass
41 # speed = obj.value
42 # print("Le Slider 1:" + str(obj.value))
43 # speedb = obj.value
44 # print("Le Slider 2:" + str(obj.value))
45 # speedc = obj.value
46 # print("Le Slider 3:" + str(obj.value))
47 # speedd = obj.value
48 # print("Le Slider 4:" + str(obj.value))
49 # speede = obj.value
50 # print("Le Slider 5:" + str(obj.value))
51
52
53# Modify the Button Class to update according to GPIO input:
54#class InputButton(Button):
55 #def update(self, dt):
56 #btn = Button(text="Boutton démarrer!")
57
58
59class MyApp(App):
60 values = []
61
62 # this method is called when button pressed
63 def save_values(self, _):
64 if self.values:
65 self.values.clear()
66 for wid in self.root.children:
67 if isinstance(wid, Slider):
68 self.values.append(wid.value)
69 print(self.values)
70
71 conn = sqlite3.connect('oklm.db')
72 c = conn.cursor()
73 # Create table
74 c.execute("CREATE TABLE IF NOT EXISTS Sliders2 (val1 real, val2 real, val3 real, val4 real, val5 real)")
75
76 # Insert a row of data
77 c.execute("INSERT INTO Sliders2 (val1, val2, val3, val4, val5) values (?, ?, ?, ?, ?)", self.values)
78
79 # Save (commit) the changes
80 conn.commit()
81
82 # We can also close the connection if we are done with it.
83 # Just be sure any changes have been committed or they will be lost.
84 conn.close()
85
86
87 def press_callback(obj):
88 if obj.text == 'Démarrer':
89 if obj.state == "down":
90 command = 'start' + "" 'F' + str(self.values[0]) + 'D' + str(self.values[1]) + 'r' + str(self.values[2]) + 'e' + str(self.values[3]) + 't' + str(self.values[4]) + "" 'Stop'
91 test = "je teste"
92 print(command)
93
94 # socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
95 socket.connect((host, port))
96 print("Connection on {}".format(port))
97 convettobyte = bytes(test, 'utf-8')
98 socket.send(convettobyte)
99
100 else:
101 print("button off")
102
103 def build(self):
104 # Set up the layout:
105 layout = GridLayout(cols=5, spacing=30, padding=30, row_default_height=150)
106
107 # Make the background gray:
108 with layout.canvas.before:
109 Color(3.0, 0.7, 0.3, 1.0)
110 self.rect = Rectangle(size=(800, 600), pos=layout.pos)
111
112
113 save = Button(text='save', on_release=self.save_values)
114
115
116 # Create the rest of the UI objects (and bind them to callbacks, if necessary):
117 outputControl = ToggleButton(text="Démarrer")
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(outputControl)
138 layout.add_widget(speedSlider)
139 layout.add_widget(speedbSlider)
140 layout.add_widget(speedcSlider)
141 layout.add_widget(speeddSlider)
142 layout.add_widget(speedeSlider)
143 layout.add_widget(save)
144
145 return layout
146
147
148if __name__ == '__main__':
149 MyApp().run()