1 #**************************************************************************
5 # Object Name : $RCSfile$
6 # Revision : $Revision$
9 # Created By : Robert Heller
10 # Created : Sun Mar 1 10:42:54 2015
11 # Last Modified : <150728.1017>
19 #**************************************************************************
21 # Copyright (C) 2015 Robert Heller D/B/A Deepwoods Software
23 # Wendell, MA 01379-9728
25 # This program is free software; you can redistribute it and/or modify
26 # it under the terms of the GNU General Public License as published by
27 # the Free Software Foundation; either version 2 of the License, or
28 # (at your option) any later version.
30 # This program is distributed in the hope that it will be useful,
31 # but WITHOUT ANY WARRANTY; without even the implied warranty of
32 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 # GNU General Public License for more details.
35 # You should have received a copy of the GNU General Public License
36 # along with this program; if not, write to the Free Software
37 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
41 #**************************************************************************
45 snit::type SignalDriverMax72xx {
46 ## SignalDriverMax72xx is a Snit type (OO class) that implements the host
47 # interface to the SignalDriverMax72xx program running on an Arduino.
48 # It provides an abstraction of the serial port interface that controls
49 # signals multiplexed by the MAX72xx chip.
50 # This version assumes a that there is only one SignalDriverMax72xx driver
51 # boards (1 to 8 signals, numbered 0 through 7) connected to the Arduino.
52 # This version assumes that only these aspects are valid (case folded):
54 # g_r (Green over Red -- Clear)
55 # y_r (Yellow over Red -- Approach)
56 # r_r (Red over Red -- [Absolute] Stop)
57 # r_g (Red over Green -- Slow Clear)
58 # r_y (Red over Yellow -- Approach Limited)
59 # dark (all lights off)
62 typecomponent validateaspects
63 ## @private Validation type for aspects.
64 typecomponent validatesignalnums
65 ## @private Validation type for signal numbers.
67 ## Initialize the validation typecomponents.
68 set validateaspects [snit::enum %%AUTO%% -values {g_r y_r r_r r_g r_y
70 set validatesignalnums [snit::integer %%AUTO%% -min 0 -max 7]
72 typemethod validate {object} {
73 ## Type validation typemethod
74 # @param object An object to be validated.
76 if {[catch {$object info type} thetype]} {
77 error "Not a valid $type object: $object"
78 } elseif {$thetype ne $type} {
79 error "Not a valid $type object: $object"
86 ## @private Variable to hold the port fd
88 #* option for the portname
89 option -portname -readonly yes -default /dev/ttyACM0
91 ## Constructor: open the port, configure it, set a readable file event,
93 # @param name The name of the object to be created.
95 # @arg -portname The name of the USB Serial port connecting to the Uno.
97 set options(-portname) [from args -portname]
98 set portfd [open $options(-portname) w+]
99 fconfigure $portfd -mode 115200,n,8,2 -blocking no -buffering none \
100 -handshake none -translation {crlf cr}
101 fileevent $portfd readable [mymethod _ReadPort]
106 ## @private Variable to hold ready (for a command) state
108 method _ReadPort {} {
109 ## @private Method to gobble from the Arduino
110 foreach {in out} [fconfigure $portfd -queue] {break}
112 set buffer [read $portfd $in]
113 if {[string range $buffer end-1 end] eq ">>"} {
122 ## Method to turn off all LEDs
133 method set {signo aspect} {
134 ## Method to set the aspect for one signal
135 # @param signo Signal number
136 # @param aspect The desired aspect
138 # Validate the aspect
139 $validateaspects validate [string tolower $aspect]
140 # Validate the signal number
141 $validatesignalnums validate $signo
147 set cmd [format {S %d %s} $signo $aspect]
154 ## Destructor: close the port.
155 catch {close $portfd}
159 package provide SignalDriverMax72xx_Host 1.0