Skip to content

Commit

Permalink
Merge pull request #4 from Overboard/new-features
Browse files Browse the repository at this point in the history
New features
  • Loading branch information
Overboard authored Apr 22, 2017
2 parents a6c76f1 + 3155a8c commit 2f80822
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
16 changes: 15 additions & 1 deletion doc/protocol.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,21 @@ A2 00 00 00 00 00 00 00 00 -> all off ("reset")

assumption:

A2 00 <rr> <gg> <bb> 00 00 00 -> set color value
A2 00 <rr> <gg> <bb> 00 00 00 00 -> set color immediately

transition time derived empirically:

A2 00 <rr> <gg> <bb> <t0> <t1> <t2> <t3>

t0 is the LSB of a 4-byte transition time from the
current color to the target color where each count
is 1ms. The 'distance' that the transition will cover
has no impact on the timing. Some examples:

A2 00 FF FF FF 64 00 00 00 -> 100.0ms to reach all on
A2 00 FF 00 00 00 04 00 00 -> 1.024s to reach full 'red'
A2 00 00 7F 00 60 EA 00 00 -> 1.0 min to reach half 'green'
A2 00 00 00 3F 00 00 01 00 -> 65.536s to reach qtr 'blue'

------------------------------------
A4 00 00 01 -> position center
Expand Down
31 changes: 29 additions & 2 deletions pycyborg/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import logging
logger = logging.getLogger(__name__)

import sys
USBLIB_AVAILABLE=False
try:
Expand All @@ -12,6 +15,7 @@
import ctypes
import os


# --- device info ---
VENDOR=0x06a3
PRODUCT=0x0dc5
Expand Down Expand Up @@ -96,6 +100,7 @@ def _usb_get_report(self):
self.vertical_position=v2k(v_pos,V_POS)
self.intensity=data[7]
self.position=v2k(position,POSITION)
return(data)

@staticmethod
def assert_int_255(i):
Expand All @@ -108,7 +113,7 @@ def assert_int_255(i):
return i

def set_rgb_color(self,r=0,g=0,b=0,force=False):
"""Set the light to a specific color"""
"""Set the light to a specific color immediately"""
r=self.assert_int_255(r)
g=self.assert_int_255(g)
b=self.assert_int_255(b)
Expand All @@ -121,7 +126,29 @@ def set_rgb_color(self,r=0,g=0,b=0,force=False):
self.g=g
self.b=b
return ret


def rgb_transition(self,r=0,g=0,b=0,duration=1,force=False):
"""Transition from current color to another. Duration in secs.
Timing handled by Cyborg hardware. Function returns immediately."""
r=self.assert_int_255(r)
g=self.assert_int_255(g)
b=self.assert_int_255(b)
#make this a no-op if we already are showing this color
if not force and (r==self.r and g==self.g and b==self.b):
return None

tms = int(duration*1000)
assert tms>=0 and tms<=0xffffffff
t=tms.to_bytes(4,'little')

logger.info(
'Transitioning device at %03d:%03d to RGB:%02x%02x%02x over %d ms',
self.usbdev.bus,self.usbdev.address,r,g,b,tms)
ret=self.usbdev.ctrl_transfer(bmRequestType=0x21, bRequest=0x09, wValue=0x03a2, wIndex=0, data_or_wLength=[0xa2,0x00,r,g,b,t[0],t[1],t[2],t[3]])
self.r=r
self.g=g
self.b=b
return ret

def transition_to(self,r,g,b,duration=1,updatetime=0.001):
"""Transition from current color to another. duration and updatetime in secs"""
Expand Down

0 comments on commit 2f80822

Please sign in to comment.