def test_to_grid(self): for char in chars_to_img_code: img_code = chars_to_img_code[char] expected_grid = chars_to_grid[char] actual_grid = Mark1.to_grid(img_code, space=".", fill="#") expected_grid = utils.normalise_grid(expected_grid) actual_grid = utils.normalise_grid(actual_grid) self.assertEqual(expected_grid, actual_grid, "grids for %s did not match\nExpected:\n%s\n---Got:\n%s" % (char, expected_grid, actual_grid))
def from_grid(grid, space=".", fill="#"): "Convert a pixel art rendition of the display into an img_code to be given to enclosure.mouth_display()" g = utils.normalise_grid(grid) width, height = _confirm_grid_ok(g, space, fill) # Since this is the Mark 1, the grid can't be larger than 16x8 if width > 16 or height > 8: raise GridTooLargeException(width, height) lines = g.split("\n") # now walk through the grid xp = 0 yp = 0 accum = [] letters = [] while xp < width and yp < height: accum.append(lines[yp][xp]) yp += 1 if yp == height: yp = 0 xp += 1 if len(accum) == 4: value = 0 for i in range(len(accum)): if accum[i] == fill: value += pow(2, i) letter = chr(65 + value) letters.append(letter) accum = [] img_code = "%s%s%s" % (chr(65 + width), chr(65 + height), "".join(letters)) return img_code
def test_normalise_grid(): input = [['A1', 'B1', 'C1', 'D1'], ['A2', 'B2', 'C2'], [], ['A4', 'B4', 'C4']] expected = [['A1', 'B1', 'C1', 'D1'] ,['A2', 'B2', 'C2', ''] ,['', '', '', ''] ,['A4', 'B4', 'C4', '']] assert utils.normalise_grid(input) == expected
def from_large_grid(grid, space=".", fill="#"): """Handles grids larger than the Mark 1's 16x8, by breaking them up into blocks. Returns a list of (img_code, x, y) tuples. """ g = utils.normalise_grid(grid) width, height = _confirm_grid_ok(g, space, fill) if width <= 16 and height <= 8: # this is not actually a large grid. Defer to from_grid. return [ (from_grid(grid, space, fill), 0, 0), ] result = [] lines = g.split("\n") for x in range(0, width, 16): for y in range(0, height, 8): this_grid = [] for yy in range(y, min(height, y + 8)): this_grid.append(lines[yy][x:x + 16]) this_grid = "\n".join(this_grid) result.append((from_grid(this_grid, space, fill), x, y)) return result
def test_insert_grid(self): base = """ ................ ................ ................ ................ ................ ................ ................ ................ """ x = """ #...# .#.#. ..#.. .#.#. #...# """ grid = utils.insert_grid(base, x, 1, 1) grid = utils.insert_grid(grid, x, 6, 3) grid = utils.insert_grid(grid, x, 10, 0) expected = utils.normalise_grid(""" ..........#...#. .#...#.....#.#.. ..#.#.......#... ...#..#...##.#.. ..#.#..#.##...#. .#...#..#....... .......#.#...... ......#...#..... """) self.assertEqual(len(grid), len(expected)) self.assertEqual(grid, expected, "Expected return grid\n%s\nActual return grid\n%s\n" % (expected, grid))
":": """ .. .. .. #. .. #. .. .. """ } MYCROFT_GRID = utils.normalise_grid(""" ................................ #...##...#.##..####..##..###.### ##.##.#.#.#..#.#..#.#..#.#....#. #.#.#..#..#....###..#..#.###..#. #...#..#..#....#..#.#..#.#....#. #...#..#..#..#.#..#.#..#.#....#. #...#..#...##..#..#..##..#....#. ................................ """) MYCROFT_IMG_CODE = [ ('QIOHEAIAEAOHCAEAIHEACAMDCECEECAAOH', 0, 0), ('QIKAKAGHAAMDCECEMDAAOHKAKAAACAOHCA', 16, 0) ] class TestMark1Display(unittest.TestCase): def test_to_grid(self): for char in chars_to_img_code: