Skip to content

Instantly share code, notes, and snippets.

@synaptiko
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save synaptiko/46ebc6640cdada7357a6 to your computer and use it in GitHub Desktop.
Save synaptiko/46ebc6640cdada7357a6 to your computer and use it in GitHub Desktop.
#picaxe 08m2
' 08M2 variable table (for convenience)
' b0:b1 b2:b3 b4:b5 b6:b7 b8:b9 b10:b11 b12:b13 b14:b15 b16:b17 b18:b19 b20:b21 b22:b23 b24:b25 b26:b27
' w0 w1 w2 w3 w4 w5 w6 w7 w8 w9 w10 w11 w12 w13
#simspeed 1
setfreq m4
symbol LED_R = C.4
symbol LED_G = C.2
symbol LED_B = C.1
symbol ON_OFF_SWITCH = C.3
symbol INDEX_R = 0
symbol INDEX_G = 1
symbol INDEX_B = 2
symbol MIN_VALUE = 5
symbol MAX_VALUE = 245
symbol COUNTER_MAX = 85
symbol isOn = bit0
symbol isOffSuspend = bit1
symbol servoRActive = bit2
symbol servoGActive = bit3
symbol servoBActive = bit4
symbol counter = b1
symbol pin = b2
symbol value = b3
symbol colorIndex = b4
main:
' initialize
input ON_OFF_SWITCH
counter = 0
gosub switchOffLed
do
if pinC.3 = 1 then ; = ON_OFF_SWITCH
if isOn = 0 then
isOn = 1
isOffSuspend = 1
gosub setLed
else
if isOffSuspend = 0 then
pause 250
if pinC.3 = 0 then ; = ON_OFF_SWITCH
isOn = 0
isOffSuspend = 0
gosub switchOffLed
else
isOffSuspend = 1
gosub toNextColor
gosub setLed
end if
else
pause 50
gosub toNextColor
gosub setLed
end if
end if
else if isOffSuspend = 1 then
isOffSuspend = 0
end if
if isOn = 0 then
disablebod 'removing brown-out-detection during nap reduces power by approx 4x
nap 4 ' sleep for a bit (4 = 288ms)
enablebod
else
pause 50
end if
loop
switchOffLed:
servoRActive = 0
servoGActive = 0
servoBActive = 0
servo LED_R, off
servo LED_G, off
servo LED_B, off
return
toNextColor:
inc counter
if counter >= COUNTER_MAX then
counter = 0
end if
return
setLed:
pin = INDEX_R
gosub setColorComponent
pin = INDEX_G
gosub setColorComponent
pin = INDEX_B
gosub setColorComponent
return
setColorComponent:
let colorIndex = counter * 3 + pin
gosub lookupColorValue
if value < MIN_VALUE then
if pin = INDEX_R then
servoRActive = 0
servo LED_R, off
else if pin = INDEX_G then
servoGActive = 0
servo LED_G, off
else
servoBActive = 0
servo LED_B, off
end if
else
if value >= MAX_VALUE then
value = 254
end if
if pin = INDEX_R then
if servoRActive = 0 then
servoRActive = 1
servo LED_R, value
else
servopos LED_R, value
end if
else if pin = INDEX_G then
if servoGActive = 0 then
servoGActive = 1
servo LED_G, value
else
servopos LED_G, value
end if
else
if servoBActive = 0 then
servoBActive = 1
servo LED_B, value
else
servopos LED_B, value
end if
end if
end if
return
lookupColorValue:
' table of colors; triples of R, G, B values
lookup colorIndex, (255, 0, 0, 255, 17, 0, 255, 35, 0, 255, 54, 0, 255, 72, 0, 255, 89, 0, 255, 108, 0, 255, 126, 0, 255, 144, 0, 255, 162, 0, 255, 179, 0, 255, 198, 0, 255, 216, 0, 255, 234, 0, 255, 252, 0, 240, 255, 0, 222, 255, 0, 203, 255, 0, 186, 255, 0, 167, 255, 0, 150, 255, 0, 131, 255, 0, 113, 255, 0, 96, 255, 0, 78, 255, 0, 60, 255, 0, 41, 255, 0, 24, 255, 0, 5, 255, 0, 0, 255, 11, 0, 255, 30, 0, 255, 47, 0, 255, 65, 0, 255, 84, 0, 255, 102, 0, 255, 120, 0, 255, 137, 0, 255, 156, 0, 255, 174, 0, 255, 192, 0, 255, 209, 0, 255, 227, 0, 255, 246, 0, 246, 255, 0, 227, 255, 0, 209, 255, 0, 192, 255, 0, 173, 255, 0, 156, 255, 0, 138, 255, 0, 120, 255, 0, 102, 255, 0, 83, 255, 0, 65, 255, 0, 48, 255, 0, 29, 255, 0, 11, 255, 5, 0, 255, 23, 0, 255, 41, 0, 255, 60, 0, 255, 77, 0, 255, 95, 0, 255, 114, 0, 255, 131, 0, 255, 149, 0, 255, 168, 0, 255, 186, 0, 255, 204, 0, 255, 222, 0, 255, 240, 0, 255, 255, 0, 252, 255, 0, 234, 255, 0, 216, 255, 0, 197, 255, 0, 180, 255, 0, 161, 255, 0, 143, 255, 0, 126, 255, 0, 108, 255, 0, 90, 255, 0, 72, 255, 0, 54, 255, 0, 36, 255, 0, 17), value
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment