Smart Pin RGB LED Demo
JonnyMac
Posts: 9,104
More simple, yet useful fun with smart pins -- a no-cog RGB LED controller. This is an alpha version; I'm sure more features will be added later. It works with common-cathode or common-anode LEDs, and you get to set the PWM frequency.
Setup code:
Setup code:
pub start(r, g, b, drive, freq) | x '' Start RGB LED driver '' -- r, g, and b are output pins '' -- drive is the output drive mode '' * 0 = common anode '' * 1 = common cathode '' -- freq is pwm frequency longmove(@rpin, @r, 3) ' copy pins x := 255 << 16 ' set pwm period to 255 units x |= 1 #> ((clkfreq / 255) / freq) <# $FFFF ' set timing of 1 unit drive := (drive) ? 0 : OP_INV ' setup CA inversion if needed pinstart(rpin, SP_PWM | drive, x, 0) ' red = true mode pwm, off pinstart(gpin, SP_PWM | drive, x, 0) ' green = true mode pwm, off pinstart(bpin, SP_PWM | drive, x, 0) ' blue = true mode pwm, offExample use:
repeat level from 0 to 255 ' ramp up rgb1.set_red(level, rgb1.USE_GAMMA) ' linear brightness waitus(1_000_000 >> 8) ' ramp in 1 second