Model Railroad System  2.2.1
ArdunioMAX72XX_Signals.tcl
1 #*****************************************************************************
2 #
3 # System :
4 # Module :
5 # Object Name : $RCSfile$
6 # Revision : $Revision$
7 # Date : $Date$
8 # Author : $Author$
9 # Created By : Robert Heller
10 # Created : Tue Jul 28 19:15:41 2015
11 # Last Modified : <150817.1413>
12 #
13 # Description
14 #
15 # Notes
16 #
17 # History
18 #
19 #*****************************************************************************
20 #
21 # Copyright (C) 2015 Robert Heller D/B/A Deepwoods Software
22 # 51 Locke Hill Road
23 # Wendell, MA 01379-9728
24 #
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.
29 #
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.
34 #
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.
38 #
39 #
40 #
41 #*****************************************************************************
42 
43 
44 package require SignalDriverMax72xx_Host;# Load low-level code
45 package require snit;# require the SNIT OO framework
46 
47 namespace eval arduniomax72xx_signals {
48  ## @defgroup arduniomax72xx_signals Using an Ardunio Uno and a MAX72XX to Operate Signals
49  # @brief Classes to operate signals using an Ardunio Uno and a MAX72XX.
50  #
51  # The module contains code to operate various sorts of signals using
52  # the hardware and code in the ArdunioMAX72XX folder. See the
53  # documentation there for information on how things are wired, etc.
54  #
55  # @{
56 
57 snit::enum signalcolors -values {
58  ## @enum signalcolors
59  # @brief Basic signal colors.
60  # The four values are dark, red, yellow, and green.
61 
62  dark
63  ## Dark, all lamps off, implies red.
64 
65  red
66  ## Red, generally stop or stop and proceed.
67 
68  yellow
69  ## Yellow, generally approach.
70 
71  green
72  ## Green, generally clear.
73 }
74 
75 snit::listtype onetwoaspectlist -minlen 1 -maxlen 2 -type signalcolors
76 ## @typedef twoaspectlist
77 # @brief Aspects for one or two headed signals.
78 # This is a list of one or two aspect colors, the first element for the upper
79 # head (or only) and the second element for the lower head.
80 
81 snit::type OneTwoHead3Color {
82  ## @brief One or two heads signals, 3 colors per head.
83  #
84  # Typical usage:
85  #
86  # @code
87  # # Load the low-level code
88  # package require SignalDriverMax72xx_Host
89  # # Connect to the Ardunio
90  # SignalDriverMax72xx controlpoint1 -portname /dev/ttyACM0
91  # # Allocate a signal
92  # arduniomax72xx_signals::OneTwoHead3Color CP1w2 -driver controlpoint1 -signal 0
93  # # Set aspect to Green over Red (clear)
94  # CP1w2 setaspect {green red}
95  # @endcode
96  #
97  # @author Robert Heller \<heller\@deepsoft.com\>
98 
99  # Ardunio options
100  option -signal -readonly yes -default 0 -type {snit::integer -min 0 -max 7}
101  option -driver -readonly yes -default {} -type ::SignalDriverMax72xx
102  # Signal name
103  option -signalname -readonly yes -default {}
104 
105  component driver
106  ## @private The SignalDriverMax72xx object.
107 
108  typemethod validate {object} {
109  ## Type validating code
110  # Raises an error if object is not either the empty string or a
111  # OneTwoHead3Color type object.
112  # @param object Some object.
113 
114  if {$object eq ""} {
115  return $object;# Empty or null objects are OK
116  } elseif {[catch {$object info type} itstype]} {
117  error "$object is not a $type";# object is not a SNIT type
118  } elseif {$itstype eq $type} {
119  return $object;# Object is of our type.
120  } else {
121  error "$object is not a $type";# object is something else
122  }
123  }
124 
125  constructor {args} {
126  ## @brief Constructor: initialize the signal object.
127  #
128  # Create a low level actuator object and install it as a component.
129  #
130  # @param name Name of the signal object.
131  # @param ... Options:
132  # @arg -driver SignalDriverMax72xx object.
133  # @arg -signal Signal number on the MAX7200 board.
134  # @arg -signalname Name of the signal on the track work schematic.
135  # @par
136 
137  # Prefetch the -driver option.
138  set options(-driver) [from args -driver]
139  if {$options(-driver) eq {}} {
140  error "The -driver option is required!"
141  }
142  set driver $options(-driver)
143  }
144 
145  typevariable aspectmap -array {
146  green g_r
147  yellow y_r
148  red r_r
149  green-red g_r
150  yellow-red y_r
151  red-red r_r
152  red-green r_g
153  red-yellow r_y
154  dark dark
155  }
156  ## @private Aspect map.
157 
158  method setaspect {aspect} {
159  ## Set signal aspect.
160  #
161  # @param aspect New aspect color.
162 
163  onetwoaspectlist validate $aspect
164  set ap [join $aspect -]
165  if {![info exists aspectmap($ap)]} {
166  error "Undefined aspect: $aspect"
167  }
168  $driver set [$self cget -signal] $aspectmap(ap)
169  set sig [$self cget -signalname]
170  if {$sig ne {}} {MainWindow ctcpanel setv $sig $aspect}
171  }
172 }
173 
174 ## @}
175 
176 }
177 
178 package provide ArdunioMAX72XX_Signals 1.0
179 
180 
181 
arduniomax72xx_signals::green
@ green
Green, generally clear.
Definition: ArdunioMAX72XX_Signals.tcl:37
arduniomax72xx_signals::signalcolors
signalcolors
Basic signal colors. The four values are dark, red, yellow, and green.
Definition: ArdunioMAX72XX_Signals.tcl:20
arduniomax72xx_signals
Definition: ArdunioMAX72XX_Signals.tcl:13
arduniomax72xx_signals::red
@ red
Red, generally stop or stop and proceed.
Definition: ArdunioMAX72XX_Signals.tcl:29
arduniomax72xx_signals::OneTwoHead3Color
One or two heads signals, 3 colors per head.
Definition: ArdunioMAX72XX_Signals.tcl:64
arduniomax72xx_signals::dark
@ dark
Dark, all lamps off, implies red.
Definition: ArdunioMAX72XX_Signals.tcl:25
arduniomax72xx_signals::yellow
@ yellow
Yellow, generally approach.
Definition: ArdunioMAX72XX_Signals.tcl:33
arduniomax72xx_signals::onetwoaspectlist
listtype onetwoaspectlist
Definition: ArdunioMAX72XX_Signals.tcl:45