Skip to content

Commit 42fab23

Browse files
committed
add cube status button
1 parent 8994df2 commit 42fab23

File tree

1 file changed

+43
-38
lines changed

1 file changed

+43
-38
lines changed

main.py

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -108,25 +108,8 @@ def show_frame(self):
108108
else:
109109
self.after(10, self.show_frame)
110110

111-
def show_scan_cube_image(self):
111+
def show_scan_cube_status(self):
112112

113-
for idx, cube_name in enumerate(self.cubeScanList):
114-
scanimg = {}
115-
try:
116-
scanimg = camera.cv_images[idx]
117-
if self.scanImageFrame[cube_name]['wraplength'] != 10:
118-
scanimg = Image.fromarray(scanimg).resize((108,97))
119-
scanout = ImageTk.PhotoImage(scanimg)
120-
self.scanImageFrame[cube_name].configure(image=scanout)
121-
self.scanImageFrame[cube_name].image = scanout
122-
self.scanImageFrame[cube_name]['wraplength'] = 10
123-
self.scanImageFrame[cube_name]['text'] = cube_name
124-
except IndexError:
125-
scanimg = {}
126-
127-
self.after(100, self.show_scan_cube_image)
128-
129-
def show_scan_cube_label(self):
130113
for idx, sname in enumerate(self.cubeScanList):
131114
for crow in range(3):
132115
for ccol in range(3):
@@ -140,8 +123,27 @@ def show_scan_cube_label(self):
140123
if crow == 1 and ccol == 1:
141124
self.cubematrix[idx][crow][ccol].config(text=sname)
142125
self.cubematrix[idx][crow][ccol].config(bg=self._from_rgb(colorname))
126+
if not self.is_use_scan_cube_label:
127+
self.cubematrix[idx][crow][ccol].grid_remove()
128+
else:
129+
self.cubematrix[idx][crow][ccol].grid(row=crow, column=ccol, ipadx=1, ipady=3, padx=2, pady=2)
130+
131+
scanimg = {}
132+
try:
133+
scanimg = camera.cv_images[idx]
134+
if self.scanImageFrame[sname]['wraplength'] != 10:
135+
scanimg = Image.fromarray(scanimg).resize((108,97))
136+
scanout = ImageTk.PhotoImage(scanimg)
137+
self.scanImageFrame[sname].configure(image=scanout)
138+
self.scanImageFrame[sname].image = scanout
139+
self.scanImageFrame[sname]['wraplength'] = 10
140+
self.scanImageFrame[sname]['text'] = sname
143141

144-
self.after(100, self.show_scan_cube_label)
142+
except IndexError:
143+
scanimg = {}
144+
145+
146+
self.after(100, self.show_scan_cube_status)
145147

146148
def __init__(self, *args, **kwargs):
147149
super(Solver, self).__init__(*args, **kwargs)
@@ -159,7 +161,7 @@ def __init__(self, *args, **kwargs):
159161
self.grip_labelframe.pack(side='left', fill=tk.Y, ipadx=2, ipady=2, padx=20, pady=20)
160162

161163
# Side Grip/Stop Buttons
162-
self.button_names = ['Fix', 'Release', 'Infinite', 'Stop']
164+
self.button_names = ['Fix', 'Release', 'Infinite', 'Stop', 'Cube Status']
163165
max_button_width = max(map(lambda x: len(x), self.button_names))
164166
self.buttons = {}
165167
for button_name in self.button_names:
@@ -216,19 +218,16 @@ def __init__(self, *args, **kwargs):
216218
row_col = self.get_cube_row_col(sname)
217219
row = row_col[0]
218220
col = row_col[1]
219-
self.scanImageFrame[sname] = tk.Label(self.cube_labelframe, text="N", compound=tk.CENTER, bg="black")
221+
self.scanImageFrame[sname] = tk.Label(self.cube_labelframe, text=sname, compound=tk.CENTER, bg="lightgray")
220222
self.scanImageFrame[sname].grid(row=row, column=col, padx=3, pady=1)
221223

222224
if self.is_use_scan_cube_label:
223225
for crow in range(3):
224226
for ccol in range(3):
225227
self.cubematrix[idx][crow][ccol] = tk.Label(self.scanImageFrame[sname], text=sname, width=3, compound=tk.CENTER, bd=2, relief="flat", bg="lightgray")
226-
self.cubematrix[idx][crow][ccol].grid(row=crow, column=ccol, ipadx=1, ipady=3, padx=2, pady=2)
228+
#self.cubematrix[idx][crow][ccol].grid(row=crow, column=ccol, ipadx=1, ipady=3, padx=2, pady=2)
227229

228-
if self.is_use_scan_cube_label:
229-
self.show_scan_cube_label()
230-
else:
231-
self.show_scan_cube_image()
230+
self.show_scan_cube_status()
232231

233232
self.show_frame()
234233
self.scanCubeReset()
@@ -238,14 +237,12 @@ def scanCubeReset(self):
238237

239238
camera.cv_images = []
240239
scanout = {}
241-
if not self.is_use_scan_cube_label:
242-
cube_image_file = './images/cube.jpg'
243-
if os.path.isfile(cube_image_file):
244-
scanimg = cv2.imread(cube_image_file, cv2.IMREAD_COLOR)
245-
scanimg = cv2.cvtColor(scanimg, cv2.COLOR_BGR2RGB)
246-
scanimg = Image.fromarray(scanimg).resize((108,97))
247-
scanout = ImageTk.PhotoImage(scanimg)
248-
240+
cube_image_file = './images/cube.jpg'
241+
if os.path.isfile(cube_image_file):
242+
scanimg = cv2.imread(cube_image_file, cv2.IMREAD_COLOR)
243+
scanimg = cv2.cvtColor(scanimg, cv2.COLOR_BGR2RGB)
244+
scanimg = Image.fromarray(scanimg).resize((108,97))
245+
scanout = ImageTk.PhotoImage(scanimg)
249246

250247
for idx, cubename in enumerate(self.cubeScanList):
251248
if scanout:
@@ -259,9 +256,19 @@ def scanCubeReset(self):
259256
camera.cubeColors[idx][crow][ccol] = webcolors.name_to_rgb("lightgray")
260257

261258
def button_action(self, label):
262-
if label == 'Stop' or label == 'fix' or label == 'release' or label == 'scramble' or label == 'infinite':
259+
if label == 'Stop' or label == 'fix' or label == 'release' or label == 'scramble':
263260
self.scanCubeReset()
264-
261+
elif label == 'Infinite':
262+
self.scanCubeReset()
263+
#
264+
elif label == 'Cube Status':
265+
if self.is_use_scan_cube_label:
266+
self.is_use_scan_cube_label = False
267+
self.buttons[label].config(bg="green")
268+
else:
269+
self.is_use_scan_cube_label = True
270+
self.buttons[label].config(bg="lightgray")
271+
265272
self.pub.publish(self.channel, label)
266273

267274
def refresh_page(self):
@@ -1351,8 +1358,6 @@ def fsm_runner():
13511358
rubiks.stop(hard=False) # change state here
13521359
elif 'scramble cube' == msg:
13531360
rubiks.scramble(config=config) # change state here
1354-
elif 'infinite' == msg:
1355-
rubiks.scramble(config=config) # change state here
13561361
elif 'fix' == msg:
13571362
rubiks.command(config=config, type='system', action='fix') # reflexive state here
13581363
elif 'release' == msg:

0 commit comments

Comments
 (0)