split out idlers into Seperate file
authorMark Tearle <[email protected]>
Sun, 4 Jul 2004 16:20:42 +0000 (16:20 +0000)
committerMark Tearle <[email protected]>
Sun, 4 Jul 2004 16:20:42 +0000 (16:20 +0000)
added initial gray code idler

sql-edition/servers/Idler.py [new file with mode: 0755]
sql-edition/servers/VendServer.py

diff --git a/sql-edition/servers/Idler.py b/sql-edition/servers/Idler.py
new file mode 100755 (executable)
index 0000000..ecf3e36
--- /dev/null
@@ -0,0 +1,133 @@
+#!/usr/bin/env python
+
+import string
+
+class Idler:
+       def __init__(self, v):
+               self.v = v
+
+       def next(self):
+               pass
+
+class TrainIdler(Idler):
+       def __init__(self, v):
+               self.idle_state = 0
+               self.v = v
+
+       def put_shark(self, s, l):
+               if self.s[l] == ' ':
+                       self.s[l] = s
+               elif self.s[l] == 'X':
+                       self.s[l] = '*'
+               else:
+                       self.s[l] = 'X'
+
+       def next(self):
+               # does the next stage of a dance
+               self.s = [' ']*10
+               shark1 = self.idle_state % 18
+               if shark1 < 9:
+                       self.put_shark('^', shark1)
+               else:
+                       self.put_shark('^', 18-shark1)
+
+               shark2 = ((self.idle_state+4) % 36)/2
+               if shark2 < 9:
+                       self.put_shark('<', shark2)
+               else:
+                       self.put_shark('<', 18-shark2)
+
+               shark3 = ((self.idle_state+7) % 54)/3
+               if shark3 < 9:
+                       self.put_shark('>', 9-shark3)
+               else:
+                       self.put_shark('>', 9-(18-shark3))
+
+               train1 = ((self.idle_state%(18*36)))
+               train1_start = 122
+               if train1 > train1_start and train1 < train1_start+(10*2):
+                       for i in range(5):
+                               ptr = i+train1-train1_start-5
+                               if ptr >= 0 and ptr < 10: self.s[ptr] = '#'
+
+               train2 = ((self.idle_state%(18*36)))
+               train2_start = 400
+               if train2 > train2_start and train2 < train2_start+(10*2):
+                       for i in range(5):
+                               ptr = i+train2-train2_start-5
+                               if ptr >= 0 and ptr < 10: self.s[9-ptr] = '#'
+
+               train3 = ((self.idle_state%(18*36)))
+               train3_start = 230
+               if train3 > train3_start and train3 < train3_start+(10*2):
+                       for i in range(10):
+                               ptr = i+train3-train3_start-10
+                               if ptr >= 0 and ptr < 10: self.s[ptr] = '-'
+
+               self.v.display(string.join(self.s, ''))
+               self.idle_state += 1
+               self.idle_state %= 18*36*54
+
+class GrayIdler(Idler):
+       def __init__(self, v, one=None, zero=None):
+               self.bits = 8
+               self.size = 1 << self.bits
+               self.i = 0
+               self.grayCode = 0
+               self.v = v
+               self.one = one
+               self.zero = zero
+
+       def next(self):
+               output = self.do_next_state()
+               # does the next stage of a dance
+               if self.zero:
+                       output = string.replace(output, "0", self.zero)
+               if self.one:
+                       output = string.replace(output, "1", self.one)
+               self.v.display(" %8.8s " % (output))
+               self.i = (self.i + 1) % self.size
+
+       def do_next_state(self):
+               self.grayCode = self.i ^ (self.i >> 1)
+               output = self.dec2bin(self.grayCode)
+
+               return "0"*(self.bits-len(output))+output
+
+
+       def dec2bin(self,num):
+           """Convert long/integer number to binary string.
+
+           E.g. dec2bin(12) ==> '1100'.
+           
+           from http://starship.python.net/~gherman/playground/decbingray/decbingray.py"""
+
+           assert num >= 0, "Decimal number must be >= 0!"
+
+           # Gracefully handle degenerate case.
+           # (Not really needed, but anyway.)    
+           if num == 0:
+               return '0'
+
+           # Find highest value bit.
+           val, j = 1L, 1L
+           while val < num:
+               val, j = val*2L, j+1L
+
+           # Convert.
+           bin = '' 
+           i = j - 1
+           while i + 1L:
+               k = pow(2L, i)
+               if num >= k:
+                   bin = bin + '1'
+                   num = num - k
+               else:
+                   if len(bin) > 0:
+                       bin = bin + '0'
+               i = i - 1L
+
+           return bin
+
+
+
index 7021622..6d79b51 100755 (executable)
@@ -12,6 +12,7 @@ from VendingMachine import VendingMachine
 from ConfigParser import ConfigParser
 from HorizScroll import HorizScroll
 from random import random, seed
+from Idler import TrainIdler,GrayIdler
 
 GREETING = 'UCC SNACKS'
 PIN_LENGTH = 4
@@ -122,65 +123,6 @@ def door_open_mode(vending_machine):
                                sleep(1)
                                return
 
-class Idler:
-       def __init__(self, v):
-               self.idle_state = 0
-               self.v = v
-
-       def put_shark(self, s, l):
-               if self.s[l] == ' ':
-                       self.s[l] = s
-               elif self.s[l] == 'X':
-                       self.s[l] = '*'
-               else:
-                       self.s[l] = 'X'
-
-       def next(self):
-               # does the next stage of a dance
-               self.s = [' ']*10
-               shark1 = self.idle_state % 18
-               if shark1 < 9:
-                       self.put_shark('^', shark1)
-               else:
-                       self.put_shark('^', 18-shark1)
-
-               shark2 = ((self.idle_state+4) % 36)/2
-               if shark2 < 9:
-                       self.put_shark('<', shark2)
-               else:
-                       self.put_shark('<', 18-shark2)
-
-               shark3 = ((self.idle_state+7) % 54)/3
-               if shark3 < 9:
-                       self.put_shark('>', 9-shark3)
-               else:
-                       self.put_shark('>', 9-(18-shark3))
-
-               train1 = ((self.idle_state%(18*36)))
-               train1_start = 122
-               if train1 > train1_start and train1 < train1_start+(10*2):
-                       for i in range(5):
-                               ptr = i+train1-train1_start-5
-                               if ptr >= 0 and ptr < 10: self.s[ptr] = '#'
-
-               train2 = ((self.idle_state%(18*36)))
-               train2_start = 400
-               if train2 > train2_start and train2 < train2_start+(10*2):
-                       for i in range(5):
-                               ptr = i+train2-train2_start-5
-                               if ptr >= 0 and ptr < 10: self.s[9-ptr] = '#'
-
-               train3 = ((self.idle_state%(18*36)))
-               train3_start = 230
-               if train3 > train3_start and train3 < train3_start+(10*2):
-                       for i in range(10):
-                               ptr = i+train3-train3_start-10
-                               if ptr >= 0 and ptr < 10: self.s[ptr] = '-'
-
-               self.v.display(string.join(self.s, ''))
-               self.idle_state += 1
-               self.idle_state %= 18*36*54
-
 def cookie(v):
        seed(time())
        messages = ['  WASSUP! ', 'PINK FISH ', ' SECRETS ', '  ESKIMO  ', ' FORTUNES ', 'MORE MONEY']
@@ -286,7 +228,9 @@ if __name__ == '__main__':
        mk = MessageKeeper(v)
        mk.set_message(GREETING)
        time_to_autologout = None
-       idler = Idler(v)
+       #idler = TrainIdler(v)
+       #idler = GrayIdler(v)
+       idler = GrayIdler(v,one="*",zero="-")
        time_to_idle = None
        last_timeout_refresh = None
 

UCC git Repository :: git.ucc.asn.au