Skip to content

Instantly share code, notes, and snippets.

@wryun
Created December 30, 2023 02:26
Show Gist options
  • Select an option

  • Save wryun/aad699bcafdc6ea05236589d30b28cfb to your computer and use it in GitHub Desktop.

Select an option

Save wryun/aad699bcafdc6ea05236589d30b28cfb to your computer and use it in GitHub Desktop.
generate ISDATA seconds output for sensor watch (actually back to front...)
class ISDATA:
def __init__(self, com, seg, on):
self.com = com
self.segs = [(seg % 8, on)]
self.offset = com * 6 + seg // 8
self.max_seg = seg // 8 * 8 + 7
def attempt_add(self, com, seg, on):
if com != self.com or seg > self.max_seg:
return False
else:
self.segs.append((seg % 8, on))
return True
def as_binary(self):
#off[5:0]sdmask[7:0]sdata[7:0]
sdmask = 0b11111111
sdata = 0b00000000
for bit, val in self.segs:
sdata |= val << bit
sdmask ^= 1 << bit
return self.offset << 16 | sdmask << 8 | sdata
def __repr__(self):
return f'isdata{(self.com, self.offset, self.segs)}'
segmap = [
[
(2, 4),
(1, 4), (2, 5),
(1, 5),
(0, 5), (1, 6),
(0, 6),
],
[
(2, 2),
(1, 2), (2, 3),
(1, 3),
(0, 2), (0, 4),
(0, 3),
],
]
charmap = {
'A': [
1,
1, 1,
1,
1, 1,
0,
],
'0': [
1,
1, 1,
0,
1, 1,
1,
],
'1': [
0,
0, 1,
0,
0, 1,
0,
],
'2': [
1,
0, 1,
1,
1, 0,
1,
],
'3': [
1,
0, 1,
1,
0, 1,
1,
],
'4': [
0,
1, 1,
1,
0, 1,
0,
],
'5': [
1,
1, 0,
1,
0, 1,
1,
],
'6': [
1,
1, 0,
1,
1, 1,
1,
],
'7': [
1,
0, 1,
0,
0, 1,
0,
],
'8': [
1,
1, 1,
1,
1, 1,
1,
],
'9': [
1,
1, 1,
1,
0, 1,
1,
],
}
def convert_segments_to_isdata(segments):
segments = sorted(segments)
isdata = None
results = []
isdata = None
for ((com, seg), on) in segments:
if not isdata:
isdata = ISDATA(com, seg, on)
elif not isdata.attempt_add(com, seg, on):
results.append(isdata)
isdata = ISDATA(com, seg, on)
if isdata:
results.append(isdata)
return results
def convert_to_isdata(chars):
"""
>>> convert_to_isdata('AA')
[isdata(0, 0, [(2, 1), (3, 0), (4, 1), (5, 1), (6, 0)]), isdata(1, 6, [(2, 1), (3, 1), (4, 1), (5, 1), (6, 1)]), isdata(2, 12, [(2, 1), (3, 1), (4, 1), (5, 1)])]
>>> convert_to_isdata('11')
[isdata(0, 0, [(2, 0), (3, 0), (4, 1), (5, 0), (6, 0)]), isdata(1, 6, [(2, 0), (3, 0), (4, 0), (5, 0), (6, 1)]), isdata(2, 12, [(2, 0), (3, 1), (4, 0), (5, 1)])]
>>> convert_to_isdata('A1')
[isdata(0, 0, [(2, 0), (3, 0), (4, 1), (5, 1), (6, 0)]), isdata(1, 6, [(2, 0), (3, 0), (4, 1), (5, 1), (6, 1)]), isdata(2, 12, [(2, 0), (3, 1), (4, 1), (5, 1)])]
"""
segments = []
for pos_segmap, char in zip(segmap, chars):
segments.extend(zip(pos_segmap, charmap[char]))
return convert_segments_to_isdata(segments)
def convert_to_binary(chars):
"""
>>> [bin(v) for v in convert_to_binary('11')]
['0b1000001100010000', '0b1101000001101000000', '0b11001100001100101000']
"""
return [isdata.as_binary() for isdata in convert_to_isdata(chars)]
if __name__ == '__main__':
import doctest
doctest.testmod()
# Warning: hard-coded to expect 3 ISDATA writes, which is true of the numbers here.
print('static const uint32_t ISDATA_SECONDS[][3] = {')
for i in range(100):
print(f" {{{', '.join([hex(isdata.as_binary()) for isdata in convert_to_isdata(f'{i:02}')])}}}, /* {i:02} */")
print('};')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment