File: simfeedback.tcl.in

package info (click to toggle)
simulavr 1.0.0%2Bgit20160221.e53413b-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 5,748 kB
  • sloc: cpp: 35,491; python: 6,991; ansic: 3,567; makefile: 1,072; sh: 653; asm: 414; tcl: 320
file content (236 lines) | stat: -rw-r--r-- 4,776 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!  @TCL_SHELL@
#
#  $Id$
#

package require Itcl
namespace import itcl::*

proc locateInDirs {name dirs} {

  foreach dir ${dirs}  {
     set toTest ${dir}/${name}
     if { [file exists ${toTest}] } {
       return ${toTest}
     }
  }
  error "unable to locate ${name}"
}

proc toChar { value } {
    return [format %c $value]
}

proc toASCII { char } {
    scan $char %c value
    return $value
}


# Dummy Class to satisfy GUI port
class UpdateControl {
  protected {
    variable objname
    variable win
    variable parent
  }

  public {
    constructor {  _parent _objname _win } {
      set parent $_parent
      set win $_win
      set objname $_objname
    }
  }
}

class AnalogNet {
  protected {
    variable inState
    variable objname
    variable win
    variable parent
  }

  public {
    constructor {  _parent _objname _win } {
      set parent $_parent
      set win $_win
      set objname $_objname
    }
  }

  method ChangeValue { newValue } {
    set inState $newValue
  }
}

class Net {
  protected {
    variable inState
    variable objname
    variable win
    variable parent
  }

  public {
    constructor {  _parent _objname _win } {
      set parent $_parent
      set win $_win
      set objname $_objname
    }
  }

  method ChangeValue { newValue } {
    set inState $newValue
  }

}

class SerialRx {
  protected {
    variable inState
    variable objname
    variable win
    variable parent
  }

  public {
    constructor {  _parent _objname _win } {
      set parent $_parent
      set win $_win
      set objname $_objname

      # set all ADC's to 0
      for {set adc 0} {$adc <= 7} {incr adc} {
        $parent SendToSimulator [format "adc%d" $adc] 0
      }
    }

    method WriteADCs { base } {
      for {set adc 0} {$adc <= 7} {incr adc} {
        $parent SendToSimulator [format "adc%d" $adc] \
           [format "%f" [expr 0.5 * (($adc + $base) % 8)]]
      }
    }

    method ChangeValue { newValue } {
      set inState $newValue
      #puts "SerialRx: ${newValue}"
      puts -nonewline [toChar ${newValue}]
      flush stdout
      # If we see 0-9, change the ADC0 value.
      if { ${newValue} >= 0x30  && ${newValue} <= 0x39 } {
        WriteADCs [expr ${newValue} - 0x30 - 1]
      }
      if { ${newValue} == 0x45 } {
        $parent SendToSimulator "exit" "0"
        exit 0
      }
    }
  }
}

class SerialTx {
  protected {
    variable objname
    variable win
    variable parent
    variable inState
  }

  public {
    constructor {  _parent _objname _win } {
      set parent $_parent
      set win $_win
      set objname $_objname
    }

    method ChangeValue { newValue } {
      set inState $newValue
      puts "SerialTx: ${newValue}"
    }
  }
}

class Master {
  protected {
    variable sock
  }

  public {
    constructor { _sock } {
      #puts "Constructor from Master called!"
      set sock $_sock
      #set sock [socket localhost $socketNo]
      fileevent $sock readable "$this ReadFromSimulavr"
      fconfigure $sock -blocking 0

      # set initial ADC0 value
      SendToSimulator adc0 0
      SendToSimulator adc1 0
    }

    method ReadFromSimulavr { } {
      if { ![eof $sock] } {
        gets $sock x
        #puts "FDBK RECV: --->$x<---"
        set readList [split $x]
        set offset [string first " " $x ]
        set sub [string range $x $offset end ]

        # puts "offset: $offset    rest: $sub"

        # create class objName parent win objName

        set front [lrange $x 1 2]
        set back [lrange $x 3 end]
        set objName [lindex $x 2]


        switch [lindex $readList 0] {
          create {
            #puts "FDBK CREATE: $front $this $objName $back"
            if { [catch { eval "$front $this $objName $back" }]  } {
              puts "exception .. are we missing class $front?"
            }
          }

          set {
            #puts "FDBK SET: [lindex $readList 1] ChangeValue [lindex $readList 2]"
            catch {
              eval "[lindex $readList 1] ChangeValue [lindex $readList 2]"
            }
          }
          default {
            #puts "FDBK DEFAULT: >$x<"
            eval $x
          }
        }

        puts -nonewline $sock "__ack X "
        flush $sock
      } else {
        error "Error condition in io input handler"
      }
    }

    method SendToSimulator { id val } {
      #puts "GUI SEND: Update the var $id to $val"
      puts -nonewline $sock "$id $val "
      flush $sock
      update
    }
  }
}

# create a server socket
socket -server NewConnection 7777

proc NewConnection { _sock _address _port } {
  puts "New Connection on Simulation Feedback Server established!"
  puts "Socket: $_sock  from Address $_address  and port $_port"
  Master guiMaster_$_sock $_sock
}

vwait forever