Skip to content

Add BodyEmulator sketch; refactor shared code into lib/#4

Draft
jenogc wants to merge 1 commit intoLexOptical:masterfrom
jenogc:feature/body-emulator
Draft

Add BodyEmulator sketch; refactor shared code into lib/#4
jenogc wants to merge 1 commit intoLexOptical:masterfrom
jenogc:feature/body-emulator

Conversation

@jenogc
Copy link
Copy Markdown

@jenogc jenogc commented Mar 12, 2026

Summary

  • Extracts shared protocol code into lib/ (Constants, DebugTools, Message, Message05) so both sketches stay in sync automatically
  • Adds LensEmulator/ — the original sketch updated to use lib/ and named pin constants
  • Adds BodyEmulator/ — new sketch that emulates a camera body

BodyEmulator details

Wiring (Teensy 3.5):

Pin Role
PIN0 (Serial1 RX) LENS7 — read lens->body responses
PIN10 (Serial2 TX) BODY7 — send body->lens commands
PIN2 LENS_CS_BODY INPUT — monitor lens chip-select
PIN3 BODY_CS_LENS OUTPUT — drive body chip-select
PIN4 BODY_VD_LENS OUTPUT — generate VD clock

Behaviour:

  1. Generates a 60 Hz VD square wave via Teensy IntervalTimer
  2. CS handshake (two-beat init sequence matching lens emulator timing)
  3. Sends init messages 0x01, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0D, 0x10 in sequence; state machine advances on each lens response
  4. Normal mode: periodic 0x04 lens-data queries each VD window
  5. Public API: requestAF(position) and requestAperture(value) queue a 0x03 command for the next window
  6. Parses 0x05 responses and logs aperture value and AF position bytes to Serial

Also fixes a latent bug in DebugTools.h where flushDebugOutputBuffer checked debugInputBufferPosition (always 0) instead of debugOutputBufferPosition, causing it to always print "no message".

Test plan

  • Build LensEmulator with Arduino IDE 2.x / Teensyduino — no errors
  • Build BodyEmulator — no errors
  • Connect two Teensies: LensEmulator TX->RX into BodyEmulator and vice versa; verify init completes and 0x04/0x05 exchange loops on Serial monitor
  • Verify requestAF() produces a 0x03 message on the wire
  • Confirm aperture byte offsets in requestAperture() via packet capture and update TODO comment

🤖 Generated with Claude Code

Restructures the project into three components that maximise code reuse:

lib/
  Shared protocol library used by both sketches:
  - Constants.h   pin assignments, protocol constants, lens response payloads
  - DebugTools.h  generic writeSerialDebugable(port, ...) replacing the
                  Serial1-specific writeSerial1Debuggable; fixes latent bug
                  where flushDebugOutputBuffer checked debugInputBufferPosition
                  instead of debugOutputBufferPosition
  - Message.h/.cpp, Message05.h/.cpp  unchanged from original

LensEmulator/
  Former E-Mount.ino updated to use lib/ includes, the new debug helper
  signatures, and the named pin constants from Constants.h.  LISTEN_ONLY
  and emulation modes work as before.

BodyEmulator/
  New sketch that emulates a Sony E-Mount camera body:
  - Generates a 60 Hz VD clock on PIN4 via Teensy IntervalTimer
  - Drives BODY_CS_LENS (PIN3) and monitors LENS_CS_BODY (PIN2)
  - Sends body->lens messages on Serial2 TX (PIN10)
  - Receives lens->body responses on Serial1 RX (PIN0)
  - State machine: CS handshake -> init sequence (0x01..0x10) ->
    normal mode with periodic 0x04 queries
  - Public API: requestAF(position), requestAperture(value)
  - Parses 0x05 responses for aperture and AF position

README updated with repo layout, mode comparison table, and per-mode
pin assignment table.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@jenogc jenogc marked this pull request as draft March 12, 2026 02:17
@jenogc
Copy link
Copy Markdown
Author

jenogc commented Mar 12, 2026

I haven't tested any of this on hardware yet. The idea is to emulate both camera and lens and move all common code shared between the emulators to a shared location. PR will remain in draft form until I have time to build the hardware and test this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants