Skip to content

Commit d9bf0b1

Browse files
authored
Add support for Gray Encoding to BinaryCounter (#438)
* Add support for gray encoding to BinaryCounter * Add gray encode/decode functions to math_extras
1 parent e659231 commit d9bf0b1

4 files changed

Lines changed: 74 additions & 3 deletions

File tree

software/contrib/binary_counter.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
```

software/contrib/binary_counter.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
from europi import *
1515
from europi_script import EuroPiScript
1616

17+
from experimental.math_extras import gray_encode
18+
19+
import configuration
20+
1721

1822
class 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(

software/contrib/itty_bitty.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from europi import *
1919
from europi_script import EuroPiScript
2020

21+
from experimental.math_extras import gray_encode
22+
2123
import configuration
2224
import 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

software/firmware/experimental/math_extras.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)