· 5 years ago · Jun 05, 2020, 01:46 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
24socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
25
26# Define some helper functions:
27
28# This callback will be bound to the LED toggle and Beep button:
29def press_callback(obj):
30 if obj.text == 'Arrêt':
31 if obj.state == "down":
32 print("button on")
33
34 else:
35 print("button off")
36
37
38# This is called when the slider is updated:
39
40'I commented it out becouse it was annoying seeing it'
41
42
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
62
63class MyApp(App):
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
75 conn = sqlite3.connect('oklm.db')
76 c = conn.cursor()
77 # Create table
78 c.execute("CREATE TABLE IF NOT EXISTS Sliders2 (val1 real, val2 real, val3 real, val4 real, val5 real)")
79
80 # Insert a row of data
81 c.execute("INSERT INTO Sliders2 (val1, val2, val3, val4, val5) values (?, ?, ?, ?, ?)", self.values)
82
83 # Save (commit) the changes
84 conn.commit()
85
86 # We can also close the connection if we are done with it.
87 # Just be sure any changes have been committed or they will be lost.
88 conn.close()
89
90 host = 'localhost'
91 port = 15555
92 command = 'start' + 'F (Frequency)' + str('?') + 'D' + str('?') + 'r' + str('?') + 'e' + str('?') + 't' + str(
93 '?') + 'Stop', self.values
94 test = "je teste"
95 print(command)
96
97 #socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
98 socket.connect((host, port))
99 print("Connection on {}".format(port))
100 convettobyte = bytes(test, 'utf-8')
101 socket.send(convettobyte)
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 # Instantiate the first UI object (the GPIO input indicator):
113 inputDisplay = InputButton(text="Démarrer")
114
115 save = Button(text='save', on_release=self.save_values)
116
117 # Schedule the update of the state of the GPIO input button:
118 Clock.schedule_interval(inputDisplay.update, 1.0 / 10.0)
119
120 # Create the rest of the UI objects (and bind them to callbacks, if necessary):
121 outputControl = ToggleButton(text="Arrêt")
122 outputControl.bind(on_press=press_callback)
123 wimg = Image(source='logo.png')
124 speedSlider = Slider(orientation='vertical', min=1, max=30, value=speed)
125 speedSlider.bind(on_touch_down=update_speed, on_touch_move=update_speed)
126
127 speedbSlider = Slider(orientation='vertical', min=1, max=30, value=speedb)
128 speedbSlider.bind(on_touch_down=update_speed, on_touch_move=update_speed)
129
130 speedcSlider = Slider(orientation='vertical', min=1, max=30, value=speedc)
131 speedcSlider.bind(on_touch_down=update_speed, on_touch_move=update_speed)
132
133 speeddSlider = Slider(orientation='vertical', min=1, max=30, value=speedd)
134 speeddSlider.bind(on_touch_down=update_speed, on_touch_move=update_speed)
135
136 speedeSlider = Slider(orientation='vertical', min=1, max=30, value=speede)
137 speedeSlider.bind(on_touch_down=update_speed, on_touch_move=update_speed)
138
139 # Add the UI elements to the layout:
140 layout.add_widget(wimg)
141 layout.add_widget(inputDisplay)
142 layout.add_widget(outputControl)
143 layout.add_widget(speedSlider)
144 layout.add_widget(speedbSlider)
145 layout.add_widget(speedcSlider)
146 layout.add_widget(speeddSlider)
147 layout.add_widget(speedeSlider)
148 layout.add_widget(save)
149
150 return layout
151
152
153if __name__ == '__main__':
154 MyApp().run()