Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
switchboard_driver
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pascal Engeler
switchboard_driver
Commits
0cf791b3
Commit
0cf791b3
authored
4 years ago
by
Pascal Engeler
Browse files
Options
Downloads
Patches
Plain Diff
First commit
parents
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
switchboard.py
+79
-0
79 additions, 0 deletions
switchboard.py
switchboard_sandbox.ipynb
+102
-0
102 additions, 0 deletions
switchboard_sandbox.ipynb
with
181 additions
and
0 deletions
switchboard.py
0 → 100644
+
79
−
0
View file @
0cf791b3
import
serial
class
Switchboard
:
def
__init__
(
self
,
port
,
debug
=
False
):
"""
Constructor
port is a serial port connected to the switchboard
the serial port will be opened
"""
self
.
ser
=
serial
.
Serial
(
port
,
9600
,
timeout
=
5
,
bytesize
=
8
,
parity
=
'
N
'
,
stopbits
=
1
)
self
.
port
=
port
self
.
allowed_channels
=
[
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
11
,
12
,
13
,
14
,
15
,
18
,
19
,
20
,
21
,
22
,
23
,
24
,
25
,
26
,
27
,
28
,
29
,
30
,
31
,
32
,
33
,
34
,
35
,
36
,
37
,
38
,
39
,
40
,
41
,
42
,
43
,
44
,
45
,
46
,
47
,
48
,
49
,
50
,
51
,
52
,
53
]
self
.
debug
=
debug
self
.
current_channel
=
-
1
def
select_channel
(
self
,
channel
):
"""
Switch to specific channel; all other channels will be turned off
channel is an integer
returns True if switch was completed successfully
returns False if the channel is invalid
No side effects if channel is invalid
"""
channel_int
=
int
(
channel
)
#weed out channels that will always be illegal
if
(
not
channel_int
in
self
.
allowed_channels
):
return
False
else
:
d1
=
chr
(
int
(
channel_int
/
10.
)
+
ord
(
'
0
'
))
#extract 10-digit
d0
=
chr
((
channel_int
%
10
)
+
ord
(
'
0
'
))
#extract 1-digit
self
.
ser
.
write
(
b
'
c
'
)
self
.
ser
.
write
(
bytes
(
d1
,
encoding
=
'
utf8
'
))
self
.
ser
.
write
(
bytes
(
d0
,
encoding
=
'
utf8
'
))
response
=
self
.
ser
.
read
(
1
)
if
response
==
b
'
A
'
:
if
self
.
debug
==
True
:
print
(
f
"
Sent:
{
c
}
{
d1
}
{
d0
}
"
)
print
(
f
"
Response:
{
response
}
"
)
self
.
current_channel
=
channel
return
True
else
:
if
self
.
debug
==
True
:
print
(
f
"
Sent:
{
c
}
{
d1
}
{
d0
}
"
)
print
(
f
"
Response:
{
response
}
"
)
return
False
def
turn_off
(
self
):
"""
Turn off all channels
returns True if operation was completed successfully
returns False else
No side effects if operation fails
"""
self
.
ser
.
write
(
b
'
o
'
)
self
.
ser
.
write
(
b
'
f
'
)
self
.
ser
.
write
(
b
'
f
'
)
response
=
self
.
ser
.
read
(
1
)
if
response
==
b
'
A
'
:
if
self
.
debug
==
True
:
print
(
f
"
Sent:
{
o
}
{
f
}
{
f
}
"
)
print
(
f
"
Response:
{
response
}
"
)
self
.
current_channel
=
-
1
return
True
else
:
if
self
.
debug
==
True
:
print
(
f
"
Sent:
{
o
}
{
f
}
{
f
}
"
)
print
(
f
"
Response:
{
response
}
"
)
return
False
def
close
(
self
):
"""
Close the serial port
"""
self
.
ser
.
close
()
This diff is collapsed.
Click to expand it.
switchboard_sandbox.ipynb
0 → 100644
+
102
−
0
View file @
0cf791b3
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#currently enabled channels: 4, 9, 10, 12, 13, 14, 15, 20, 23, 46\n",
"enabled_channels = [4, 9, 10, 12, 13, 14, 15, 20, 23, 46]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from switchboard import *\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"mySwitches = Switchboard('/dev/tty.usbmodem143101')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"for j in range(10):\n",
" for i in enabled_channels:\n",
" mySwitches.select_channel(i)\n",
" time.sleep(1)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mySwitches.turn_off()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"mySwitches.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
%% Cell type:code id: tags:
```
python
#currently enabled channels: 4, 9, 10, 12, 13, 14, 15, 20, 23, 46
enabled_channels
=
[
4
,
9
,
10
,
12
,
13
,
14
,
15
,
20
,
23
,
46
]
```
%% Cell type:code id: tags:
```
python
from
switchboard
import
*
import
time
```
%% Cell type:code id: tags:
```
python
mySwitches
=
Switchboard
(
'
/dev/tty.usbmodem143101
'
)
```
%% Cell type:code id: tags:
```
python
for
j
in
range
(
10
):
for
i
in
enabled_channels
:
mySwitches
.
select_channel
(
i
)
time
.
sleep
(
1
)
```
%% Cell type:code id: tags:
```
python
mySwitches
.
turn_off
()
```
%% Output
True
%% Cell type:code id: tags:
```
python
mySwitches
.
close
()
```
%% Cell type:code id: tags:
```
python
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment