{"id":196,"date":"2025-12-31T01:52:57","date_gmt":"2025-12-31T01:52:57","guid":{"rendered":"https:\/\/myjetsonnano.hopto.org\/?p=196"},"modified":"2025-12-31T01:53:32","modified_gmt":"2025-12-31T01:53:32","slug":"atoms3u-with-io-commands","status":"publish","type":"post","link":"https:\/\/myjetsonnano.perseus314.com\/index.php\/2025\/12\/31\/atoms3u-with-io-commands\/","title":{"rendered":"AtomS3U with IO commands"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>import sys\nimport time\n\n# ----------------------\n# Line polling function\n# ----------------------\ndef poll_line(buffer, handler, ignore_next_terminator_flag):\n    \"\"\"\n    Poll a single byte from USB and handle line input.\n    \n    Args:\n        buffer (bytearray): accumulated bytes so far\n        handler (function): function to call when a line is complete\n        ignore_next_terminator_flag (list of bool): mutable flag for paired terminator\n    \"\"\"\n    try:\n        b = sys.stdin.buffer.read(1)  # read 1 byte at a time\n    except OSError:\n        # USB not connected\n        return\n\n    if b is None or len(b) == 0:\n        return  # no data available\n\n    c = b&#91;0]\n\n    # Ignore second terminator if flag is set\n    if ignore_next_terminator_flag&#91;0]:\n        if c == 10 or c == 13:  # LF or CR\n            ignore_next_terminator_flag&#91;0] = False\n            return\n        else:\n            ignore_next_terminator_flag&#91;0] = False  # resume normal data\n\n    # Check for line terminator\n    if c == 10 or c == 13:  # LF or CR\n        line_str = buffer.decode('utf-8')\n        handler(line_str)        # handle the line\n        buffer&#91;:] = b''          # clear buffer safely\n        ignore_next_terminator_flag&#91;0] = True\n    else:\n        buffer.append(c)         # accumulate normal byte\n\n# ----------------------\n# Example line handler\n# ----------------------\ndef handle_input(line: str):\n    \"\"\"\n    Called when a full line is received.\n    Displays the line and returns cursor to left.\n    \"\"\"\n    # Display line and return cursor to left (CR)\n    sys.stdout.buffer.write(line.encode('utf-8') + b'\\n\\r')\n    # sys.stdout.buffer.flush()  &lt;-- removed, not needed\n\n# ----------------------\n# Main loop\n# ----------------------\nbuffer = bytearray()\nignore_next_terminator_flag = &#91;False]  # mutable flag\n\nwhile True:\n    poll_line(buffer, handle_input, ignore_next_terminator_flag)\n    time.sleep_ms(10)  # avoid busy-loop\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,12,10],"tags":[],"class_list":["post-196","post","type-post","status-publish","format-standard","hentry","category-atoms3u","category-micropython","category-uiflow2"],"_links":{"self":[{"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/posts\/196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/comments?post=196"}],"version-history":[{"count":1,"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/posts\/196\/revisions"}],"predecessor-version":[{"id":197,"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/posts\/196\/revisions\/197"}],"wp:attachment":[{"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/media?parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/categories?post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/tags?post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}