|
| 1 | +from aioconsole import ainput |
| 2 | +from frameutils import Bluetooth |
| 3 | +import asyncio |
| 4 | +import time |
| 5 | + |
| 6 | + |
| 7 | +async def main(): |
| 8 | + |
| 9 | + main_script = """ |
| 10 | + require("graphics") |
| 11 | + |
| 12 | + local graphics = Graphics.new() |
| 13 | + local last_print_time = 0 |
| 14 | +
|
| 15 | + graphics:append_text("This is a test. The quick brown fox jumps over the lazy dog.", "\\u{F0000}") |
| 16 | + |
| 17 | + while true do |
| 18 | + if frame.time.utc() - last_print_time > 0.07 then |
| 19 | + graphics:print() |
| 20 | + last_print_time = frame.time.utc() |
| 21 | + end |
| 22 | + |
| 23 | + collectgarbage("collect") |
| 24 | + end |
| 25 | + """ |
| 26 | + |
| 27 | + graphics_script = r""" |
| 28 | + Graphics = {} |
| 29 | + Graphics.__index = Graphics |
| 30 | +
|
| 31 | + function Graphics.new() |
| 32 | + local self = setmetatable({}, Graphics) |
| 33 | + self:clear() |
| 34 | + return self |
| 35 | + end |
| 36 | +
|
| 37 | + function Graphics:clear() |
| 38 | + -- Set by append_text function |
| 39 | + self.__text = "" |
| 40 | + self.__emoji = "" |
| 41 | + -- Used internally by print function |
| 42 | + self.__this_line = "" |
| 43 | + self.__last_line = "" |
| 44 | + self.__last_last_line = "" |
| 45 | + self.__starting_index = 1 |
| 46 | + self.__current_index = 1 |
| 47 | + self.__ending_index = 1 |
| 48 | + self.__done_function = (function() end)() |
| 49 | + end |
| 50 | +
|
| 51 | + function Graphics:append_text(data, emoji) |
| 52 | + self.__text = self.__text .. string.gsub(data, '\\n+', ' ') |
| 53 | + self.__emoji = emoji |
| 54 | + end |
| 55 | +
|
| 56 | + function Graphics:on_complete(func) |
| 57 | + self.__done_function = func |
| 58 | + end |
| 59 | +
|
| 60 | + function Graphics.__print_layout(last_last_line, last_line, this_line, emoji) |
| 61 | + local TOP_MARGIN = 118 |
| 62 | + local LINE_SPACING = 58 |
| 63 | + local EMOJI_MAX_WIDTH = 91 |
| 64 | +
|
| 65 | + frame.display.text(emoji, 640 - EMOJI_MAX_WIDTH, TOP_MARGIN, { color = 'YELLOW' }) |
| 66 | +
|
| 67 | + if last_last_line == '' and last_line == '' then |
| 68 | + frame.display.text(this_line, 1, TOP_MARGIN) |
| 69 | + elseif last_last_line == '' then |
| 70 | + frame.display.text(last_line, 1, TOP_MARGIN) |
| 71 | + frame.display.text(this_line, 1, TOP_MARGIN + LINE_SPACING) |
| 72 | + else |
| 73 | + frame.display.text(last_last_line, 1, TOP_MARGIN) |
| 74 | + frame.display.text(last_line, 1, TOP_MARGIN + LINE_SPACING) |
| 75 | + frame.display.text(this_line, 1, TOP_MARGIN + LINE_SPACING * 2) |
| 76 | + end |
| 77 | +
|
| 78 | + frame.display.show() |
| 79 | + end |
| 80 | +
|
| 81 | + function Graphics:print() |
| 82 | + if self.__text:sub(self.__starting_index, self.__starting_index) == ' ' then |
| 83 | + self.__starting_index = self.__starting_index + 1 |
| 84 | + end |
| 85 | +
|
| 86 | + if self.__current_index >= self.__ending_index then |
| 87 | + self.__starting_index = self.__ending_index |
| 88 | + self.__last_last_line = self.__last_line |
| 89 | + self.__last_line = self.__this_line |
| 90 | + self.__starting_index = self.__ending_index |
| 91 | + end |
| 92 | +
|
| 93 | + for i = self.__starting_index + 22, self.__starting_index, -1 do |
| 94 | + if self.__text:sub(i, i) == ' ' or self.__text:sub(i, i) == '' then |
| 95 | + self.__ending_index = i |
| 96 | + break |
| 97 | + end |
| 98 | + end |
| 99 | +
|
| 100 | + self.__this_line = self.__text:sub(self.__starting_index, self.__current_index) |
| 101 | +
|
| 102 | + self.__print_layout(self.__last_last_line, self.__last_line, self.__this_line, self.__emoji) |
| 103 | +
|
| 104 | + if self.__current_index >= #self.__text then |
| 105 | + pcall(self.__done_function) |
| 106 | + self.__done_function = (function() end)() |
| 107 | + return |
| 108 | + end |
| 109 | +
|
| 110 | + self.__current_index = self.__current_index + 1 |
| 111 | + end |
| 112 | + """ |
| 113 | + |
| 114 | + # Connect to bluetooth and upload file |
| 115 | + b = Bluetooth() |
| 116 | + await b.connect( |
| 117 | + print_response_handler=lambda s: print(s), |
| 118 | + ) |
| 119 | + |
| 120 | + print("Uploading script") |
| 121 | + |
| 122 | + await b.upload_file(graphics_script, "graphics.lua") |
| 123 | + await b.upload_file(main_script, "main.lua") |
| 124 | + await b.send_reset_signal() |
| 125 | + |
| 126 | + # Wait until a keypress |
| 127 | + await ainput("") |
| 128 | + |
| 129 | + await b.send_break_signal() |
| 130 | + await b.disconnect() |
| 131 | + |
| 132 | + |
| 133 | +loop = asyncio.new_event_loop() |
| 134 | +loop.run_until_complete(main()) |
0 commit comments