Skip to content

Instantly share code, notes, and snippets.

@kevinwchang
Last active December 16, 2015 12:09
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 kevinwchang/5432926 to your computer and use it in GitHub Desktop.
Save kevinwchang/5432926 to your computer and use it in GitHub Desktop.
RGB shoji lamp Maestro script
<!--Pololu Maestro servo controller settings file, http://www.pololu.com/catalog/product/1350-->
<UscSettings version="1">
<NeverSuspend>false</NeverSuspend>
<SerialMode>UART_DETECT_BAUD_RATE</SerialMode>
<FixedBaudRate>9600</FixedBaudRate>
<SerialTimeout>0</SerialTimeout>
<EnableCrc>false</EnableCrc>
<SerialDeviceNumber>12</SerialDeviceNumber>
<SerialMiniSscOffset>0</SerialMiniSscOffset>
<Channels MiniMaestroServoPeriod="80000" ServoMultiplier="1">
<!--Period = 20 ms-->
<!--Channel 0-->
<Channel name="Clock" mode="Output" min="3968" max="8000" homemode="Off" home="3968" speed="0" acceleration="0" neutral="6000" range="1905" />
<!--Channel 1-->
<Channel name="Latch" mode="Output" min="3968" max="8000" homemode="Off" home="3968" speed="0" acceleration="0" neutral="6000" range="1905" />
<!--Channel 2-->
<Channel name="Data" mode="Output" min="3968" max="8000" homemode="Off" home="3968" speed="0" acceleration="0" neutral="6000" range="1905" />
<!--Channel 3-->
<Channel name="R pot" mode="Input" min="0" max="1024" homemode="Ignore" home="0" speed="0" acceleration="0" neutral="1024" range="1905" />
<!--Channel 4-->
<Channel name="G pot" mode="Input" min="0" max="1024" homemode="Ignore" home="0" speed="0" acceleration="0" neutral="1024" range="1905" />
<!--Channel 5-->
<Channel name="B pot" mode="Input" min="0" max="1024" homemode="Ignore" home="0" speed="0" acceleration="0" neutral="1024" range="1905" />
<!--Channel 6-->
<Channel name="Off button" mode="Input" min="0" max="1024" homemode="Ignore" home="0" speed="0" acceleration="0" neutral="1024" range="1905" />
<!--Channel 7-->
<Channel name="Flicker button" mode="Input" min="0" max="1024" homemode="Ignore" home="0" speed="0" acceleration="0" neutral="1024" range="1905" />
<!--Channel 8-->
<Channel name="Solid button" mode="Input" min="0" max="1024" homemode="Ignore" home="0" speed="0" acceleration="0" neutral="1024" range="1905" />
<!--Channel 9-->
<Channel name="Cycle button" mode="Input" min="0" max="1024" homemode="Ignore" home="0" speed="0" acceleration="0" neutral="1024" range="1905" />
<!--Channel 10-->
<Channel name="" mode="Servo" min="3968" max="8000" homemode="Off" home="3968" speed="0" acceleration="0" neutral="3968" range="1905" />
<!--Channel 11-->
<Channel name="" mode="Servo" min="3968" max="8000" homemode="Off" home="3968" speed="0" acceleration="0" neutral="6000" range="1905" />
</Channels>
<Sequences />
<Script ScriptDone="false">1023 # 0 R
0 # 1 G
0 # 2 B
0 # 3 cycle state: 0 = green up, 1 = red down, 2 = blue up, 3 = green down, 4 = red up, 5 = blue down
0 # 4 lamp state: 0 = off, 1 = flicker, 2 = solid, 3 = cycle
0 # 5 flicker counter
begin
read_buttons
4 peek 0 equals if 0 0 0 rgb endif # off
4 peek 1 equals if flicker endif #flicker
4 peek 2 equals if user_color endif #solid
4 peek 3 equals if cycle endif #cycle
repeat
sub read_buttons
6 get_position 512 less_than if 0 4 poke endif
7 get_position 512 less_than if 1 4 poke endif
8 get_position 512 less_than if 2 4 poke endif
9 get_position 512 less_than if 3 4 poke endif
return
sub flicker
5 peek 1 plus
dup 20 equals if
10 get_position 1 bitwise_and if
user_color
else
3 get_position 200 minus 0 max
4 get_position 200 minus 0 max
5 get_position 200 minus 0 max
rgb
endif
drop 0 # reset flicker counter
endif
5 poke
return
sub cycle
3 peek # read cycle state
dup 0 equals if # green up
1 peek 8 plus # read and increment green
dup 1023 greater_than if # if green &gt; 1023
drop 1023 # cap to 1023
1 3 poke endif # advance cycle state
1 poke # write green
update_cycle_color
drop return
endif
dup 1 equals if # red down
0 peek 8 minus # read and decrement red
dup 0 less_than if # if red &lt; 0
drop 0 # cap to 0
2 3 poke endif # advance cycle state
0 poke # write red
update_cycle_color
drop return
endif
dup 2 equals if # blue up
2 peek 8 plus # read and increment blue
dup 1023 greater_than if # if blue &gt; 1023
drop 1023 # cap to 1023
3 3 poke endif # advance cycle state
2 poke # write blue
update_cycle_color
drop return
endif
dup 3 equals if # green down
1 peek 8 minus # read and decrement green
dup 0 less_than if # if green &lt; 0
drop 0 # cap to 0
4 3 poke endif # advance cycle state
1 poke # write green
update_cycle_color
drop return
endif
dup 4 equals if # red up
0 peek 8 plus # read and increment red
dup 1023 greater_than if # if red &gt; 1023
drop 1023 # cap to 1023
5 3 poke endif # advance cycle state
0 poke # write red
update_cycle_color
drop return
endif
dup 5 equals if # blue down
2 peek 8 minus # read and decrement blue
dup 0 less_than if # if blue &lt; 0
drop 0 # cap to 0
0 3 poke endif # advance cycle state
2 poke # write blue
update_cycle_color
drop return
endif
sub update_cycle_color
0 peek 1 peek 2 peek rgb
return
sub user_color
3 get_position
4 get_position
5 get_position
rgb
return
# Subroutine for setting the RGB value of a ShiftBrite/ShiftBar.
# example usage: 1023 511 255 rgb
sub rgb
0 send_bit # this bit does not matter
0 send_bit # the "address" bit - 0 means a color command
swap rot rot
send_10_bit_value
send_10_bit_value
send_10_bit_value
0 1 8000 1 servo servo # toggle the latch pin
return
# sends a numerical value as a sequence of 10 bits
sub send_10_bit_value
512
begin
dup
while
over over bitwise_and send_bit
1 shift_right
repeat
drop drop
return
# sends a single bit
sub send_bit
if 8000 else 0 endif
2 servo # set DATA to 0 or 1
0 0 8000 0 servo servo # toggle CLOCK
return</Script>
</UscSettings>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment