-
Notifications
You must be signed in to change notification settings - Fork 9
/
libamp.py
154 lines (129 loc) · 3.6 KB
/
libamp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
Send RC5-encoded commands to suit Cambridge Audio amplifiers.
Confirmed working with:
Cambridge Audio azur 540A v2
"""
import pigpio
pi = pigpio.pi()
#############
# CONSTANTS #
#############
RC5_PER = 889 # half-bit period (microseconds)
CA_RC5_SYS = 16
# dictionary of supported models, each containing:
# a dictionary of possible commands, mapped to the code we need to send
command_table = {
"540A": {
"aux": 4,
"cd": 5,
"tuner": 3,
"dvd": 1,
"av": 2,
"tapemon": 0,
"vol-": 17,
"voldown": 17,
"vol+": 16,
"volup": 16,
"mute": 13,
"standby": 12,
"bright": 18,
"source+": 19,
"source-": 20,
"sourcenext": 19,
"sourceprev": 20,
"clipoff": 21,
"clipon": 22,
"muteon": 50,
"muteoff": 51,
"ampon": 14,
"ampoff": 15,
},
"840A": {
"source1": 4,
"source2": 5,
"source3": 3,
"source4": 1,
"source5": 2,
"source6": 0,
"source7": 6,
"source8": 7,
"tapemon": 7,
"vol-": 17,
"voldown": 17,
"vol+": 16,
"volup": 16,
"mute": 13,
"bright": 18,
"source+": 8,
"source-": 9,
"sourcenext": 8,
"sourceprev": 9,
"muteon": 50,
"muteoff": 51,
"ampon": 14,
"ampoff": 15,
"spkselect": 20,
"clipoff": 21, # enters Balance mode
},
"CXA60": {
"muteon": 50,
"muteoff": 51,
"vol+": 16,
"vol-": 17,
"poweron": 110,
"poweroff": 111,
},
}
all_models = {model for model in command_table}
all_commands = {command for model in all_models for command in command_table[model]}
#############
# Functions #
#############
def build_rc5(cmd: int) -> int:
"""Build an RC5 message"""
RC5_START = 0b100 + (0b010 * (cmd < 64))
RC5_SYS = int(CA_RC5_SYS)
RC5_CMD = int(cmd)
# RC-5 message has a 3-bit start sequence, a 5-bit system ID, and a 6-bit command.
RC5_MSG = (
((RC5_START & 0b111) << 11) | ((RC5_SYS & 0b11111) << 6) | (RC5_CMD & 0b111111)
)
return RC5_MSG
def wave_mnch(DATA: int, PIN: int):
"""
manchester encode waveform. Period is the half-bit period in microseconds.
"""
pi.set_mode(PIN, pigpio.OUTPUT) # set GPIO pin to output.
# create msg
# syntax: pigpio.pulse(gpio_on, gpio_off, delay us)
msg = []
# this is a terrible way to iterate over bits... but it works.
for i in bin(DATA)[2:]:
if i == "1":
msg.append(pigpio.pulse(0, 1 << PIN, RC5_PER)) # L
msg.append(pigpio.pulse(1 << PIN, 0, RC5_PER)) # H
else:
msg.append(pigpio.pulse(1 << PIN, 0, RC5_PER)) # H
msg.append(pigpio.pulse(0, 1 << PIN, RC5_PER)) # L
msg.append(pigpio.pulse(0, 1 << PIN, RC5_PER)) # return line to idle condition.
pi.wave_add_generic(msg)
wid = pi.wave_create()
return wid
def posint(n):
"""check for positive integer"""
try:
if int(n) > 0:
return int(n)
else:
raise ValueError
except ValueError:
msg = str(n) + " is not a positive integer"
raise argparse.ArgumentTypeError(msg)
def execute(pin: int, command: str, repeat: int, model: str = "504A"):
"""execute a given command"""
# generate RC5 message (int)
rc5_msg = build_rc5(command_table[model][command])
# generate digital manchester-encoded waveform
wid = wave_mnch(rc5_msg, pin)
for i in range(repeat):
cbs = pi.wave_send_once(wid)