Skip to content

Commit

Permalink
Refactored for clarity.
Browse files Browse the repository at this point in the history
  • Loading branch information
iConor committed May 9, 2016
1 parent 8ea36d6 commit 86c4404
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 88 deletions.
6 changes: 3 additions & 3 deletions power_functions/__init__.py
@@ -1,6 +1,6 @@
"""LEGO Power Functions RC v1.20"""

import power_functions.extended as extended
import power_functions.combo_direct as direct
import power_functions.single_output as single
import power_functions.combo_pwm as pwm
import power_functions.combo_direct as combo_direct
import power_functions.single_output as single_output
import power_functions.combo_pwm as combo_pwm
22 changes: 11 additions & 11 deletions power_functions/combo_direct.py
@@ -1,35 +1,35 @@
"""LEGO Power Functions RC v1.20 - Combo Direct Mode"""

import power_functions.pf_rc_protocol as rc
import power_functions.rc_protocol as pf_rc

# Output Data
OUTPUT = ["FLT", # Float
"FWD", # Forward
"REV", # Backward
"BRK"] # Brake

def payload(_ch, _red, _blue, _esc=rc.ESC.MODE, _addr=rc.ADDR.DEF):
def payload(channel, red, blue, _esc=pf_rc.ESC.MODE, _addr=pf_rc.ADDR.DEF):
"""Returns the payload for a Combo Direct Mode command."""
nibble1 = _esc | _ch
nibble2 = _addr | rc.MODE.DIR
nibble3 = _red | (_blue << 2)
nibble4 = rc.lrc(nibble1, nibble2, nibble3)
nibble1 = _esc | channel
nibble2 = _addr | pf_rc.MODE.DIR
nibble3 = red | (blue << 2)
nibble4 = pf_rc.lrc(nibble1, nibble2, nibble3)
return nibble1, nibble2, nibble3, nibble4

def button(_ch, _red, _blue):
def button(channel, red, blue):
"""Returns the button for a Combo Direct Mode command."""
return (rc.CHANNEL[_ch], OUTPUT[_red], OUTPUT[_blue])
return (pf_rc.CHANNEL[channel], OUTPUT[red], OUTPUT[blue])

def button_string(ch_, red, blue):
def button_string(channel, red, blue):
"""Returns the string representation of Combo Direct Mode button."""
return 'CH{:s}_{:s}_{:s}'.format(ch_, red, blue)
return 'CH{:s}_{:s}_{:s}'.format(channel, red, blue)

def lirc_codes():
"""Prints LIRC codes for Combo Direct Mode."""
for i in range(0, 4):
for k in range(0, 16):
red = k & 0x3
blue = k >> 2
hex_codes = rc.payload_string(*payload(i, red, blue))
hex_codes = pf_rc.payload_string(*payload(i, red, blue))
lirc_patterns = button_string(*button(i, red, blue))
print "\t{}\t\t{}".format(lirc_patterns, hex_codes)
54 changes: 27 additions & 27 deletions power_functions/combo_pwm.py
@@ -1,46 +1,46 @@
"""LEGO Power Functions RC v1.20 - Combo PWM Mode"""

import power_functions.pf_rc_protocol as rc
import power_functions.rc_protocol as pf_rc

# Data (PWM Steps)
STEPS = ["FLT", # Float
"FWD1", # PWM forward, step 1
"FWD2", # PWM forward, step 2
"FWD3", # PWM forward, step 3
"FWD4", # PWM forward, step 4
"FWD5", # PWM forward, step 5
"FWD6", # PWM forward, step 6
"FWD7", # PWM forward, step 7
"BRK", # Brake then float
"REV7", # PWM backward, step 7
"REV6", # PWM backward, step 6
"REV5", # PWM backward, step 5
"REV4", # PWM backward, step 4
"REV3", # PWM backward, step 3
"REV2", # PWM backward, step 2
"REV1"] # PWM backward, step 1
PWM_STEP = ["FLT", # Float
"FWD1", # PWM forward, step 1
"FWD2", # PWM forward, step 2
"FWD3", # PWM forward, step 3
"FWD4", # PWM forward, step 4
"FWD5", # PWM forward, step 5
"FWD6", # PWM forward, step 6
"FWD7", # PWM forward, step 7
"BRK", # Brake then float
"REV7", # PWM backward, step 7
"REV6", # PWM backward, step 6
"REV5", # PWM backward, step 5
"REV4", # PWM backward, step 4
"REV3", # PWM backward, step 3
"REV2", # PWM backward, step 2
"REV1"] # PWM backward, step 1

def payload(_ch, _red, _blue, _esc=rc.ESC.PWM, _addr=rc.ADDR.DEF):
def payload(channel, red, blue, _esc=pf_rc.ESC.PWM, _addr=pf_rc.ADDR.DEF):
"""Returns the payload for a Combo PWM Mode command."""
nibble1 = _addr | _esc | _ch
nibble2 = _blue
nibble3 = _red
nibble4 = rc.lrc(nibble1, nibble2, nibble3)
nibble1 = _addr | _esc | channel
nibble2 = blue
nibble3 = red
nibble4 = pf_rc.lrc(nibble1, nibble2, nibble3)
return nibble1, nibble2, nibble3, nibble4

def button(_ch, _red, _blue):
def button(channel, red, blue):
"""Returns the button for a Combo PWM Mode command."""
return (rc.CHANNEL[_ch], STEPS[_red], STEPS[_blue])
return (pf_rc.CHANNEL[channel], PWM_STEP[red], PWM_STEP[blue])

def button_string(ch_, red, blue):
def button_string(channel, red, blue):
"""Returns the string representation of a Combo PWM Mode button."""
return 'CH{:s}_{:s}_{:s}'.format(ch_, red, blue)
return 'CH{:s}_{:s}_{:s}'.format(channel, red, blue)

def lirc_codes():
"""Prints LIRC codes for Combo PWM Mode."""
for i in range(0, 4):
for j in range(0, 16):
for k in range(0, 16):
hex_codes = rc.payload_string(*payload(i, j, k))
hex_codes = pf_rc.payload_string(*payload(i, j, k))
lirc_pattern = button_string(*button(i, j, k))
print "\t{}\t\t{}".format(lirc_pattern, hex_codes)
42 changes: 21 additions & 21 deletions power_functions/extended.py
@@ -1,40 +1,40 @@
"""LEGO Power Functions RC v1.20 - Extended Mode"""

import power_functions.pf_rc_protocol as rc
import power_functions.rc_protocol as pf_rc

# Function Data
FUNCTIONS = ["BRK_A", # Brake then float A
"INC_A", # Increment speed on A
"DEC_A", # Decrement speed on A
"NOT_USED", # ...
"TGL_B", # Toggle forward/float on B
"NOT_USED", # ...
"TGL_ADR", # Toggle address bit
"ALN_TGL", # Align toggle bit (get in sync)
"RESERVED"] # ...
FUNCTION = ["BRK_A", # Brake then float A
"INC_A", # Increment speed on A
"DEC_A", # Decrement speed on A
"NOT_USED", # ...
"TGL_B", # Toggle forward/float on B
"NOT_USED", # ...
"TGL_ADR", # Toggle address bit
"ALN_TGL", # Align toggle bit (get in sync)
"RESERVED"] # ...

def payload(_ch, _fn, _esc=rc.ESC.MODE, _addr=rc.ADDR.DEF):
def payload(channel, function, _esc=pf_rc.ESC.MODE, _addr=pf_rc.ADDR.DEF):
"""Returns the payload for an Extended Mode command."""
nibble1 = _esc | _ch
nibble2 = _addr | rc.MODE.EXT
nibble3 = _fn
nibble4 = rc.lrc(nibble1, nibble2, nibble3)
nibble1 = _esc | channel
nibble2 = _addr | pf_rc.MODE.EXT
nibble3 = function
nibble4 = pf_rc.lrc(nibble1, nibble2, nibble3)
return nibble1, nibble2, nibble3, nibble4

def button(_ch, _fn):
def button(channel, function):
""" Returns the button for an Extended Mode command."""
return (rc.CHANNEL[_ch], FUNCTIONS[_fn])
return (pf_rc.CHANNEL[channel], FUNCTION[function])

def button_string(ch_, fn_):
def button_string(channel, function):
"""Returns the string representation of an Extended Mode button."""
return 'CH{:s}_{:s}'.format(ch_, fn_)
return 'CH{:s}_{:s}'.format(channel, function)

def lirc_codes():
"""Prints LIRC codes for Extended Mode."""
for i in range(0, 4):
for k, func in enumerate(FUNCTIONS):
for k, func in enumerate(FUNCTION):
if func == "NOT_USED" or func == "RESERVED":
continue
hex_codes = rc.payload_string(*payload(i, k))
hex_codes = pf_rc.payload_string(*payload(i, k))
lirc_patterns = button_string(*button(i, k))
print "\t{}\t\t{}".format(lirc_patterns, hex_codes)
@@ -1,17 +1,17 @@
"""LEGO Power Functions RC v1.20 - LPF RC Protocol"""
"""LEGO Power Functions RC v1.20 - RC Protocol"""

from collections import namedtuple

# Escape Bit Masks
Esc = namedtuple('Esc', ['MODE', 'PWM'])
ESC = Esc(0x0, 0x4)
Escape = namedtuple('Escape', ['MODE', 'PWM'])
ESC = Escape(0x0, 0x4)

# Channel Bit Masks
CHANNEL = ["1", "2", "3", "4"]

# Address Space Bit Masks
Addr = namedtuple('Addr', ['DEF', 'EXT'])
ADDR = Addr(0x0, 0x8)
Address = namedtuple('Address', ['DEF', 'EXT'])
ADDR = Address(0x0, 0x8)

# Mode Selection Bit Masks
Mode = namedtuple('Mode', ['EXT', 'DIR', 'RSVD', 'SNGL'])
Expand All @@ -21,6 +21,6 @@ def lrc(nibble1, nibble2, nibble3):
"""Returns the "Longitudinal Redundancy Check" nibble."""
return 0xf ^ nibble1 ^ nibble2 ^ nibble3

def payload_string(nib1, nib2, nib3, nib4):
def payload_string(nibble1, nibble2, nibble3, nibble4):
"""Returns the string representation of a payload."""
return'0x{:X}{:X}{:X}{:X}'.format(nib1, nib2, nib3, nib4)
return'0x{:X}{:X}{:X}{:X}'.format(nibble1, nibble2, nibble3, nibble4)
28 changes: 14 additions & 14 deletions power_functions/single_output.py
@@ -1,6 +1,6 @@
"""LEGO Power Functions RC v1.20 - Single Output Mode"""

import power_functions.pf_rc_protocol as rc
import power_functions.rc_protocol as pf_rc

# Mode Bits
MODE = ["PWM", # Mode = PWM
Expand Down Expand Up @@ -45,25 +45,25 @@
"TGL_C2", # Toggle C2
"TGL_REV"] # Toggle full backward

def payload(_ch, _mode, _out, _data, _esc=rc.ESC.MODE, _addr=rc.ADDR.DEF):
def payload(channel, mode, output, data, _esc=pf_rc.ESC.MODE, _addr=pf_rc.ADDR.DEF):
"""Returns the payload for a Single Output Mode command."""
nibble1 = _esc | _ch
nibble2 = _addr | rc.MODE.SNGL | (_mode << 1) | _out
nibble3 = _data
nibble4 = rc.lrc(nibble1, nibble2, nibble3)
nibble1 = _esc | channel
nibble2 = _addr | pf_rc.MODE.SNGL | (mode << 1) | output
nibble3 = data
nibble4 = pf_rc.lrc(nibble1, nibble2, nibble3)
return nibble1, nibble2, nibble3, nibble4

def button(_ch, _mode, _output, _data):
def button(channel, mode, output, data):
"""Returns the button for a Single Output Mode command."""
if _mode == 0:
data_ = PWM[_data]
if mode == 0:
data = PWM[data]
else:
data_ = CSTID[_data]
return (rc.CHANNEL[_ch], OUTPUT[_output], data_)
data = CSTID[data]
return (pf_rc.CHANNEL[channel], OUTPUT[output], data)

def button_string(ch_, output, data):
def button_string(channel, output, data):
"""Returns the string representation of a Single Output Mode button."""
return 'CH{:s}_{:s}_{:s}'.format(ch_, output, data)
return 'CH{:s}_{:s}_{:s}'.format(channel, output, data)

def lirc_codes():
"""Prints LIRC codes for Single Output Mode."""
Expand All @@ -72,6 +72,6 @@ def lirc_codes():
for k in range(0, 16):
mode = (j & 0x2) >> 1
output = j & 0x1
hex_codes = rc.payload_string(*payload(i, mode, output, k))
hex_codes = pf_rc.payload_string(*payload(i, mode, output, k))
lirc_patterns = button_string(*button(i, mode, output, k))
print "\t{}\t\t{}".format(lirc_patterns, hex_codes)
2 changes: 1 addition & 1 deletion tests/context.py
Expand Up @@ -6,4 +6,4 @@
parent_directory = os.path.abspath(wtf)
sys.path.insert(0, parent_directory)

import power_functions as pf
import power_functions
8 changes: 4 additions & 4 deletions tests/print_all.py
@@ -1,6 +1,6 @@
from context import pf
from context import power_functions as pf

pf.extended.lirc_codes()
pf.direct.lirc_codes()
pf.single.lirc_codes()
pf.pwm.lirc_codes()
pf.combo_direct.lirc_codes()
pf.single_output.lirc_codes()
pf.combo_pwm.lirc_codes()

0 comments on commit 86c4404

Please sign in to comment.