smartknob/proto/generate_protobuf.py
Scott Bezek ae28d523e0
Protobuf serial protocol (#101)
- Firmware
  - Refactor all code to use a log interface rather than `Serial` directly
  - Moved platformio.ini to root, so you can load the entire repo in VS Code and still use platformio
  - Reduced graphic buffer bit depth to 8 bits (short on RAM :( )
  - Implemented threadsafe log interface in interface_task (using a queue for posting log message)
  - Created serial protocol interface and 2 implementations - plaintext (default) and protobuf (selected by sending a NULL byte)
  - Protobuf protocol is roughly the same architecture as Splitflap's:
    - PacketSerial (cobs) framing with NULL delimiters
    - CRC32 checksums for packets
    - nanopb generated code for encoding/decoding (generated firmware code is checked in, since it should change less frequently and this reduces burden to build the project from scratch)
- Software
  - Typescript example host-side code:
    - smartknobjs-proto
      - Autogenerated types/encoding/decoding protobuf code (using protobufjs)
    - smartknobjs
      - Helper library for interfacing the with smartknob via serial/protobuf. Implements basic outgoing queue (with retries and ACK checking) and message callback for responding to messages from the SmartKnob
    - example
      - Basic demo CLI app that uses smartknobjs to connect to the smartknob, send a haptic config, and print state changes and log messages to the console
2022-10-22 17:27:35 -07:00

35 lines
1018 B
Python
Executable File

#!/usr/bin/env python3
from pathlib import Path
import os
import shutil
import subprocess
import sys
def run():
SCRIPT_PATH = Path(__file__).absolute().parent
REPO_ROOT = SCRIPT_PATH.parent
proto_path = REPO_ROOT / 'proto'
nanopb_path = REPO_ROOT / 'thirdparty' / 'nanopb'
# Make sure nanopb submodule is available
if not os.path.isdir(nanopb_path):
print(f'Nanopb checkout not found! Make sure you have inited/updated the submodule located at {nanopb_path}', file=sys.stderr)
exit(1)
nanopb_generator_path = nanopb_path / 'generator' / 'nanopb_generator.py'
c_generated_output_path = REPO_ROOT / 'firmware' / 'src' / 'proto_gen'
proto_files = [f for f in os.listdir(proto_path) if f.endswith('.proto')]
assert len(proto_files) > 0, 'No proto files found!'
# Generate C files via nanopb
subprocess.check_call(['python3', nanopb_generator_path, '-D', c_generated_output_path] + proto_files, cwd=proto_path)
if __name__ == '__main__':
run()