Skip to content

Commit

Permalink
Solving "-0" problem (jump to 0) by shifting negative numbers by 1
Browse files Browse the repository at this point in the history
  • Loading branch information
wallneradam committed Jan 3, 2021
1 parent 32a0829 commit 16df4a2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ You can found more information on its fun (not official) site: https://www.tc420

I reverse engineered the USB protocol the device uses and wrote this library and CLI tool for making endless possibilities from this device. I successfully implemented all the functions PLED.exe have.

The library is cross platform, works everywhere where [libusb](https://github.com/libusb/libusb) works. But because it is a HID device, and MacOS does not allow to detach kernel drivers of HID devices, on MacOS it is extremely hard to make it work in current form. But I tested on **Windows** and **Linux**, and works fine there. It works on (all kinds of) Raspberry Pi, so it
The library is cross platform, works everywhere where [libusb](https://github.com/libusb/libusb) works. But because it is a HID device, and MacOS does not allow to detach kernel drivers of HID devices, on MacOS it is extremely hard to make it work in current form. But I tested on **Windows** and **Linux**, and works fine there. It works on (all kinds of) Raspberry Pi.
(Actually there is a python library for HID devices, so theoretically it is possible to rewrite the lib using hidapi, and then it could work on MacOS as well, pull requests are welcome...)

It has a full featured **CLI**, which can act as a reference for the libray. Also you can use it for automation, or just for fun.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# This call to setup() does all the work
setup(
name="tc420",
version="0.2.0",
version="0.2.1",
description="TC420 LED Controller library and command line interface",
long_description=README,
long_description_content_type="text/markdown",
Expand Down
29 changes: 16 additions & 13 deletions tc420/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import os
from random import random, randint
from datetime import datetime
from time import sleep
from typing import Callable, Tuple

import click
Expand Down Expand Up @@ -107,15 +106,17 @@ def time_sync(ctx: Context):
@click.option("--step", "-s", "steps", multiple=True, required=True,
type=(
click.DateTime(formats=('%H:%M', '%H.%M', '%H%M')),
click.IntRange(-100, 100),
click.IntRange(-100, 100),
click.IntRange(-100, 100),
click.IntRange(-100, 100),
click.IntRange(-100, 100),
click.IntRange(-101, 100),
click.IntRange(-101, 100),
click.IntRange(-101, 100),
click.IntRange(-101, 100),
click.IntRange(-101, 100),
),
metavar="<TIME> <CH1 %> <CH2 %> <CH3 %> <CH4 %> <CH5 %>",
help="Step data: e.g.: 13:00 100 99 50 0 -70"
"\n\nNegative channel values are \"jump\" (immediate) values.")
"\n\nNegative channel values are \"jump\" (immediate) values, but because -0 is not "
"interpretable, jump values are shifted by one. So -1 will be 0, -2 will be 1 ... and "
"-101 will be 100.")
def mode(ctx: Context, name: str, bank: int, steps: Tuple[Tuple[datetime, int, int, int, int, int]]):
"""
Create (or modify) mode(s) (program).
Expand Down Expand Up @@ -158,15 +159,17 @@ def clear_all_modes(ctx: Context):
@click.option("--steps", "-s", "steps", multiple=True, required=True,
type=(
float,
click.IntRange(-100, 100),
click.IntRange(-100, 100),
click.IntRange(-100, 100),
click.IntRange(-100, 100),
click.IntRange(-100, 100),
click.IntRange(-101, 100),
click.IntRange(-101, 100),
click.IntRange(-101, 100),
click.IntRange(-101, 100),
click.IntRange(-101, 100),
),
metavar="<DURATION sec> <CH1 %> <CH2 %> <CH3 %> <CH4 %> <CH5 %>",
help="Step data: e.g.: 1.5 100 99 50 0 -70"
"\n\nNegative channel values are \"jump\" (immediate) values.")
"\n\nNegative channel values are \"jump\" (immediate) values, but because -0 is not "
"interpretable, jump values are shifted by one. So -1 will be 0, -2 will be 1 ... and "
"-101 will be 100.")
@click.pass_context
def play(ctx: Context, name: str, steps: Tuple[Tuple[float, int, int, int, int, int]]):
"""
Expand Down
4 changes: 2 additions & 2 deletions tc420/tc420.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def __init__(self, step_data: Tuple[Union[datetime, time], int, int, int, int, i
# Negative step value means jump
if step < 0:
jump_flags |= 1 << b
step_data[b + 1] = -step
step_data[b + 1] = -step - 1

self.add_uchar(step_data[0].hour)
self.add_uchar(step_data[0].minute)
Expand Down Expand Up @@ -451,7 +451,7 @@ def playing_thread():
# Calculate new channel values
for c in range(5):
if self._play_step_data[c + 1] < 0: # Immediate value
new_channel_values[c] = abs(self._play_step_data[c + 1])
new_channel_values[c] = abs(self._play_step_data[c + 1]) - 1
else: # Linear calculation
dcv = self._play_step_data[c + 1] - last_channel_values[c]
new_channel_values[c] = round(last_channel_values[c] + dcv *
Expand Down

0 comments on commit 16df4a2

Please sign in to comment.