-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ftduino_direct.py and udev rules for ftduino access
Signed-off-by: PeterDHabermehl <[email protected]>
- Loading branch information
1 parent
b221d83
commit da89d2e
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
board/fischertechnik/TXT/rootfs/etc/udev/rules.d/99-ftduino.rules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ATTRS{idVendor}=="1c40" ATTRS{idProduct}=="0537", ENV{ID_MM_DEVICE_IGNORE}="1" | ||
ATTRS{idVendor}=="1c40" ATTRS{idProduct}=="0538", ENV{ID_MM_DEVICE_IGNORE}="1" |
109 changes: 109 additions & 0 deletions
109
board/fischertechnik/TXT/rootfs/opt/ftc/ftduino_direct.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# v1.0.4 (c) 2017 Peter Habermehl | ||
# | ||
# Kommandos fuer ftduino.comm : | ||
# | ||
# void led_set <0|1> | ||
# | ||
# string ftduino_direct_get_version | ||
# char[16] ftduino_id_set <string maxlen=16> | ||
# char[16] ftduino_id_get | ||
# | ||
# | ||
# void input_set_mode <I1..I8> <switch|resistance|voltage> | ||
# uint16_t input_get <I1..I8> | ||
# | ||
# void output_set <O1..O8> <mode="0..2"> <pwm=[0..512]> mode: 0=open 1=switch to plus (normal operation) 2=switch to gnd | ||
# void motor_set <M1..M4> <right|left|brake> <pwm=[0..512]> | ||
# | ||
# void ultrasonic_enable <true|false> | ||
# int16_t ultrasonic_get() | ||
# | ||
# void counter_set_mode <none|rising|falling|any> | ||
# uint16_t counter_get <C1..C4> | ||
# void counter_clear <C1..C4> | ||
# uint8_t counter_get_state <C1..C4> | ||
# | ||
# Beispiel: | ||
# myftd=ftduino_direct.ftduino() | ||
# myftd.comm("motor M1 right 512") | ||
# | ||
|
||
|
||
import serial | ||
import serial.tools.list_ports | ||
import time | ||
|
||
FTDUINO_VIDPID="1c40:0538" | ||
|
||
__all__ = ["ftduino_scan", "ftduino_find_by_name", "ftduino"] | ||
|
||
def ftduino_scan(): | ||
# scannt nach ftduinos und gibt eine Liste zurueck, die den device-pfad und die vom ftduino zurueckgemeldete ID beinhaltet | ||
# [x][0] enthaelt den device-pfad, [x][1] die ID | ||
# | ||
devices = [] | ||
try: | ||
for dev in serial.tools.list_ports.grep("vid:pid="+FTDUINO_VIDPID): | ||
o = serial.Serial(dev[0], 115200, timeout=.1) | ||
time.sleep(0.25) | ||
o.flushInput() | ||
o.flushOutput() | ||
o.write("ftduino_id_get\n".encode("utf-8")) | ||
n=o.readline().decode("utf-8")[:-2] | ||
o.close() | ||
devices.append([dev[0], n]) | ||
except: | ||
pass | ||
|
||
return devices | ||
|
||
def ftduino_find_by_name(duino): | ||
# sucht nach einem ftduino mit angegebenem Namen und gibt im Erfolgsfall den device-pfad zurueck | ||
# der device-Pfad kann beim Erzeugen eines ftduino-Objekts angegeben werden, um gezielt einen | ||
# bestimmten ftduino anzusprechen. | ||
d=ftduino_scan() | ||
try: | ||
return next(c for c in ftduino_scan() if c[1] == duino)[0] | ||
except: | ||
return None | ||
|
||
class ftduino(object): | ||
|
||
def __init__(self, device=None): | ||
self.ftduino=None | ||
|
||
# bei Angabe eines device-Pfades wird versucht, diesen ftduino zu oeffnen | ||
# ansonsten wird der erste gefundene ftduino angesprochen | ||
|
||
try: | ||
if device==None: | ||
liste=ftduino_scan() | ||
port=liste[0][0] | ||
if liste!=None: | ||
self.ftduino = serial.Serial(port, 115200, timeout=.1) | ||
time.sleep(0.5) #give the connection a second to settle | ||
else: | ||
self.ftduino = serial.Serial(device, 115200, timeout=.1) | ||
except: | ||
pass | ||
|
||
def getDevice(self): | ||
return self.ftduino | ||
|
||
def comm(self, command): | ||
try: | ||
command=command+"\n" | ||
self.ftduino.flushInput() | ||
self.ftduino.flushOutput() | ||
self.ftduino.write(command.encode("utf-8")) | ||
data = self.ftduino.readline() | ||
if data: | ||
return data.decode("utf-8")[:-2] | ||
else: | ||
return False | ||
except: | ||
return False | ||
|
||
def close(self): | ||
self.ftduino.close() | ||
|
da89d2e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The udev rule shouldn't be necessary. We don't have a modem manager on the TXT so we don't need to prevent it from grabbing the ttyACM device.
da89d2e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically I agree, but my thinking was that there might be other projects based on the cfw setup (like my cfw_dev-setup_4rpi) or future changes to the cfw (even if unlikely) who might need it, so it's in place and might make live easier. Plus it's part of the ftduino project. What's the cost of leaving it there?