File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -22,3 +22,31 @@ counter.
2222| ` cv4 ` | 8s-bit output |
2323| ` cv5 ` | 16s-bit output |
2424| ` cv6 ` | Most significant bit output |
25+
26+ ## Configuration
27+
28+ This program has the following configuration options:
29+
30+ - ` USE_GRAY_ENCODING ` : if ` true ` , instead of traditional binary encoding, the pattern is encoded
31+ using [ gray encoding] ( https://en.wikipedia.org/wiki/Gray_encoding ) . This means that consecutive
32+ gate patterns will always differ by exactly 1 bit.
33+
34+ | Decimal value | Traditional binary | Gray encoding |
35+ | ---------------| --------------------| ---------------|
36+ | 0 | ` 000000 ` | ` 000000 ` |
37+ | 1 | ` 000001 ` | ` 000001 ` |
38+ | 2 | ` 000010 ` | ` 000011 ` |
39+ | 3 | ` 000011 ` | ` 000010 ` |
40+ | 4 | ` 000100 ` | ` 000110 ` |
41+ | 5 | ` 000101 ` | ` 000111 ` |
42+ | 6 | ` 000110 ` | ` 000101 ` |
43+ | 7 | ` 000111 ` | ` 000100 ` |
44+ | ... | ... | ... |
45+
46+ To enable Gray encoding, create/edit ` /config/BinaryCounter.json ` to contain the following:
47+ ``` json
48+ {
49+ "USE_GRAY_ENCODING" : true
50+ }
51+
52+ ```
Original file line number Diff line number Diff line change 1414from europi import *
1515from europi_script import EuroPiScript
1616
17+ from experimental .math_extras import gray_encode
18+
19+ import configuration
20+
1721
1822class BinaryCounter (EuroPiScript ):
1923 MAX_N = (1 << NUM_CVS ) - 1
@@ -37,6 +41,16 @@ def __init__(self):
3741
3842 b2 .handler (self .reset )
3943
44+ @classmethod
45+ def config_points (cls ):
46+ return [
47+ # If true, use gray encoding instead of standard binary
48+ configuration .boolean (
49+ "USE_GRAY_ENCODING" ,
50+ False
51+ ),
52+ ]
53+
4054 def on_gate_rise (self ):
4155 self .gate_recvd = True
4256
@@ -47,12 +61,18 @@ def reset(self):
4761 self .n = 0
4862
4963 def set_outputs (self ):
64+ if self .config .USE_GRAY_ENCODING :
65+ n = gray_encode (self .n )
66+ else :
67+ n = self .n
68+
5069 for i in range (NUM_CVS ):
51- if (self . n >> i ) & 0x01 :
70+ if (n >> i ) & 0x01 :
5271 cvs [i ].on ()
5372 else :
5473 cvs [i ].off ()
5574
75+
5676 def main (self ):
5777 while True :
5878 self .k = int (
Original file line number Diff line number Diff line change 1818from europi import *
1919from europi_script import EuroPiScript
2020
21+ from experimental .math_extras import gray_encode
22+
2123import configuration
2224import time
2325
@@ -88,8 +90,7 @@ def change_sequence(self, n):
8890 self .sequence_n = n
8991
9092 if self .use_gray_encoding :
91- # convert the number from traditional binary to its gray encoding equivalent
92- n = (n & 0xff ) ^ ((n & 0xff ) >> 1 )
93+ n = gray_encode (n )
9394 else :
9495 n = n & 0xff
9596
Original file line number Diff line number Diff line change @@ -203,3 +203,25 @@ def solve_linear_system(m):
203203 results [i ] = results [i ] / m [i ][i ]
204204
205205 return results
206+
207+
208+ def gray_encode (n : int ) -> int :
209+ """
210+ Convert a binary integer to its Gray Encoding equivalent.
211+
212+ :param n: The value to convert
213+ """
214+ return (n & 0xFF ) ^ ((n & 0xFF ) >> 1 )
215+
216+
217+ def gray_decode (n : int ) -> int :
218+ """
219+ Convert a binary integer from Gray Encoding to its traditional equivalent.
220+
221+ :param n: The value to convert
222+ """
223+ mask = n
224+ while mask != 0 :
225+ mask = mask >> 1
226+ n = n ^ mask
227+ return n
You can’t perform that action at this time.
0 commit comments