Skip to content

Commit c56d65d

Browse files
committed
implement garland example
1 parent 136447e commit c56d65d

File tree

6 files changed

+82
-18
lines changed

6 files changed

+82
-18
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ Requirement:
88

99
to run call: python3 main.py
1010

11-
The idea is pretty similiar to [this](http://bildr.org/2011/01/arduino-serial/), but with
12-
LEDs placed in a square like [here](http://skpang.co.uk/catalog/images/arduino/seeed/IMG_2850.jpg).
11+
The idea is pretty similiar to [this](http://bildr.org/2011/01/arduino-serial/), but
12+
LEDs can be placed in any figure and you can control state by Tkinter GUI (or implement your
13+
awesome example).
1314

14-
As you see you can update code to use not only LEDs, but anything you want.
15+
For sure you can update code to use not only LEDs, but anything you want.
16+
17+
[![Awesome garland example on arduino](https://github.com/l1va/garland/blob/master/video_link.jpg?raw=true)](https://youtu.be/GTirX8CdfMU)

arduino.ino

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
int incomingByte = 0;
1+
2+
int start_pin = 2;
3+
int end_pin = 13;
4+
5+
String input;
26

37
void setup() {
4-
Serial.begin(4800); // opens serial port, sets data rate to 9600 bps
8+
Serial.begin(9600);
9+
for (int i = start_pin; i <= end_pin; i++){
10+
pinMode(i, OUTPUT);
11+
digitalWrite(i, LOW);
12+
}
513
}
614

715
void loop() {
816
if (Serial.available() > 0) {
9-
incomingByte = Serial.read();
10-
Serial.print("I received: ");
11-
Serial.println(incomingByte, DEC);
17+
input = Serial.readString();
18+
pin = input.substring(1).toInt()
19+
if (input[0]=='+') {
20+
digitalWrite(pin + start_pin, HIGH);
21+
}else{
22+
digitalWrite(pin + start_pin, LOW);
23+
}
1224
}
13-
}
25+
}

awesome_example.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
class Awesome:
3+
def __init__(self, main):
4+
self.main = main
5+
6+
def do(self):
7+
self.awesome_on(0)
8+
9+
def awesome_on(self, i):
10+
self.main.select(i)
11+
if i == self.main.last_check_ind:
12+
self.main.top.after(2000, self.awesome_off, self.main.last_check_ind)
13+
else:
14+
self.main.top.after(2000, self.awesome_on, i + 1)
15+
16+
def awesome_off(self, i):
17+
self.main.deselect(i)
18+
if i > 0:
19+
self.main.top.after(2000, self.awesome_off, i - 1)

connection_frame.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import subprocess
33
import serial
44

5-
MATRIX_TYPE = "Matrix"
6-
75

86
class Connection:
97
def __init__(self, parent):
@@ -63,7 +61,7 @@ def f():
6361
var = tk.IntVar(value=1)
6462
chb = tk.Checkbutton(self.model_frame, text=str(port), variable=var,
6563
command=self.checkbox_changed(i, j, var))
66-
chb.grid(row=start_row + i, column=j+1)
64+
chb.grid(row=start_row + i, column=j + 1)
6765

6866
return f
6967

@@ -90,7 +88,7 @@ def f():
9088
def get_available_serials():
9189
ports_string = subprocess.check_output(['python', '-m', 'serial.tools.list_ports']).decode("utf-8").strip()
9290
print("FOUND: " + ports_string)
93-
if len(ports_string) == 0: # TODO: REMOVE ME: stub to debug without any ports
91+
if len(ports_string) == 0: # TODO: REMOVE ME: stub to debug without any ports
9492
print("stubs used as no ports found")
9593
return ["/dev/ttyUSB0", "/dev/ttyUSB1"]
9694
return ports_string.split('\n')

main.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import tkinter as tk
2-
import connection_frame
32

3+
import connection_frame
4+
import awesome_example
45

56
class Main:
67
def __init__(self):
78
self.serial = None
89
self.top = tk.Tk()
10+
self.checks = []
11+
self.last_check_ind = 0
912

1013
def start(self):
1114
connection_frame.Connection(self).draw()
@@ -32,18 +35,47 @@ def on_connect(self, ser, model):
3235
command=checkbox_changed(ind, var, self.serial))
3336
chb.grid(row=start_row + i, column=j)
3437
ind += 1
38+
self.checks.append((chb, var))
39+
self.last_check_ind = ind - 1
40+
awesome_button = tk.Button(self.top, text="make awesome", width=10, command=self.algo_run)
41+
awesome_button.grid(row=start_row + len(model), column=len(model[0]))
42+
43+
def select(self, i):
44+
self.serial.write(("+" + str(i)).encode("utf-8"))
45+
self.serial.flush()
46+
self.checks[i][0].select()
47+
48+
def deselect(self, i):
49+
self.serial.write(("-" + str(i)).encode("utf-8"))
50+
self.serial.flush()
51+
self.checks[i][0].deselect()
52+
53+
def state(self, i):
54+
return self.checks[i][1].get()
55+
56+
def toggle(self, i):
57+
if self.state(i) == 1:
58+
self.deselect(i)
59+
else:
60+
self.select(i)
61+
62+
def algo_run(self):
63+
awesome_example.Awesome(self).do()
64+
65+
66+
3567

3668

3769
def checkbox_changed(port, var, ser):
3870
def f():
39-
to_send = str(port)
71+
to_send = "-"
4072
if var.get() == 1:
41-
to_send += "+"
73+
to_send = "+"
4274
print("checked " + str(port))
4375
else:
44-
to_send += "-"
76+
to_send = "-"
4577
print("unchecked " + str(port))
46-
to_send += "\n"
78+
to_send += str(port) + "\n"
4779
ser.write(to_send.encode("utf-8"))
4880
ser.flush()
4981

video_link.jpg

12.8 KB
Loading

0 commit comments

Comments
 (0)