@@ -6,6 +6,7 @@ include(joinpath(@__DIR__, "mpu6000.jl"))
66include (joinpath (@__DIR__ , " encoder.jl" ))
77include (joinpath (@__DIR__ , " shift_driver.jl" ))
88include (joinpath (@__DIR__ , " seven_seg.jl" ))
9+ include (joinpath (@__DIR__ , " ws2812_driver.jl" ))
910
1011using . GPIO
1112using . PWM
@@ -29,7 +30,9 @@ const PWM_MAX_VALUE = 1024 # PWM duty cycle range (0-1024)
2930# Global handles for GPIO pins and PWM channels
3031mutable struct HardwareContext
3132 gpio:: GPIO.GPIOController
33+ pio:: PIOBlock
3234 chain:: ShiftRegisterChain
35+ nose:: WS2812
3336 pins:: Vector{GPIO.GPIOPin}
3437end
3538
@@ -42,7 +45,10 @@ Called on Ctrl-C or program exit.
4245function shutdown! (hw:: HardwareContext )
4346 println (Core. stdout , " Shutting down hardware..." )
4447 hw. chain[0 : 23 ] = false
48+ set_color! (hw. nose, 0 , 0 , 0 )
4549 close (hw. chain)
50+ close (hw. nose)
51+ close (hw. pio)
4652 for pin in hw. pins
4753 close (pin)
4854 end
6167const CM_PRESENT= 23
6268const D1= 5
6369const D2= 24
70+ const NOSE_RGB= 18
6471
6572function (@main )(args):: Cint
6673 println (Core. stdout , " Balance car starting..." )
@@ -74,12 +81,20 @@ function (@main)(args)::Cint
7481 d2 = GPIO. request_output (gpio, D2, " d2" , 0 )
7582 println (Core. stdout , " GPIO configured" )
7683
84+ # Open shared PIO block
85+ pio = open_pio (0 )
86+
7787 # Initialize 3 chained shift registers via PIO
78- chain = open_shift_registers ()
88+ chain = open_shift_registers (pio )
7989 chain[0 : 23 ] = false
8090 println (Core. stdout , " Shift registers initialized ($NUM_REGISTERS x 8-bit, $NBITS outputs)" )
8191
82- hw = HardwareContext (gpio, chain, [cm_present, d1, d2])
92+ # Initialize nose RGB LED (WS2812) on same PIO block
93+ nose = open_ws2812 (pio, NOSE_RGB)
94+ set_color! (nose, 0 , 0 , 0 )
95+ println (Core. stdout , " Nose RGB LED initialized on pin $NOSE_RGB " )
96+
97+ hw = HardwareContext (gpio, pio, chain, nose, [cm_present, d1, d2])
8398
8499 # Enable hardware
85100 GPIO. set_value (cm_present, 1 )
@@ -93,6 +108,7 @@ function (@main)(args)::Cint
93108 disp2 = SevenSeg (chain, 16 ) # 3rd register: bits 16–23
94109
95110 digit = 0
111+ hue = 0
96112
97113 # Control loop timing (500ms = 2Hz)
98114 loop_period_ns = 500_000_000
@@ -119,6 +135,26 @@ function (@main)(args)::Cint
119135 show_digit! (disp1, (digit + 1 ) % 10 )
120136 end
121137 digit = (digit + 1 ) % 10
138+
139+ # Cycle nose LED through hue wheel
140+ # Simple HSV→RGB with S=V=255, varying H
141+ h = hue ÷ 43
142+ f = (hue - h * 43 ) * 6
143+ q = 255 - f
144+ if h == 0
145+ set_color! (nose, 255 , f, 0 )
146+ elseif h == 1
147+ set_color! (nose, q, 255 , 0 )
148+ elseif h == 2
149+ set_color! (nose, 0 , 255 , f)
150+ elseif h == 3
151+ set_color! (nose, 0 , q, 255 )
152+ elseif h == 4
153+ set_color! (nose, f, 0 , 255 )
154+ else
155+ set_color! (nose, 255 , 0 , q)
156+ end
157+ hue = (hue + 25 ) % 256
122158 end
123159 catch e
124160 if e isa InterruptException
0 commit comments