Skip to content

Commit e6ea642

Browse files
committed
Change shift direction
1 parent c49d1a6 commit e6ea642

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

deploy/src/main.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const PWM_MAX_VALUE = 1024 # PWM duty cycle range (0-1024)
3030
mutable struct HardwareContext
3131
gpio::GPIO.GPIOController
3232
chain::ShiftRegisterChain
33+
pins::Vector{GPIO.GPIOPin}
3334
end
3435

3536
"""
@@ -42,6 +43,10 @@ function shutdown!(hw::HardwareContext)
4243
println(Core.stdout, "Shutting down hardware...")
4344
hw.chain[0:23] = false
4445
close(hw.chain)
46+
for pin in hw.pins
47+
close(pin)
48+
end
49+
close(hw.gpio)
4550
println(Core.stdout, "Hardware shutdown complete.")
4651
end
4752

@@ -74,7 +79,7 @@ function (@main)(args)::Cint
7479
chain[0:23] = false
7580
println(Core.stdout, "Shift registers initialized ($NUM_REGISTERS x 8-bit, $NBITS outputs)")
7681

77-
hw = HardwareContext(gpio, chain)
82+
hw = HardwareContext(gpio, chain, [cm_present, d1, d2])
7883

7984
# Enable hardware
8085
GPIO.set_value(cm_present, 1)

deploy/src/shift_driver.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using PIOLib
22

3+
const SHIFT_RIGHT = true # LSB first
4+
const SHIFT_LEFT = false # MSB first
5+
36
function shift_register_program(pio::PIOBlock; ser_pin::Integer, clk_pin::Integer, rclk_pin::Integer,
47
nbits::Integer, clkdiv::Real=1.0f0)
58
1 <= nbits <= 31 || error("nbits must be 1-31 (SET immediate is 5 bits, and 32 encodes as 0 in autopull threshold)")
@@ -20,7 +23,7 @@ function shift_register_program(pio::PIOBlock; ser_pin::Integer, clk_pin::Intege
2023
set_pins=(rclk_pin, 1),
2124
sideset_pin_base=clk_pin,
2225
sideset=(1, false, false),
23-
out_shift=(true, true, nbits),
26+
out_shift=(SHIFT_LEFT, true, nbits),
2427
clkdiv=Float32(clkdiv),
2528
wrap=(prog.wrap_target, prog.wrap),
2629
)
@@ -122,7 +125,8 @@ end
122125
# --- Flush ---
123126

124127
function flush!(chain::ShiftRegisterChain)
125-
shift_out!(chain.sm, chain.state & 0x00FFFFFF)
128+
# Left-align: OSR shifts left, so MSB goes out first
129+
shift_out!(chain.sm, (chain.state & 0x00FFFFFF) << (32 - NBITS))
126130
end
127131

128132
# --- Bulk writes (update shadow + flush) ---

0 commit comments

Comments
 (0)