forked from parallaxinc/spin-standard-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.amp.max9744.spin
187 lines (136 loc) · 4.78 KB
/
audio.amp.max9744.spin
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
{
--------------------------------------------
Filename: audio.amp.max9744.spin
Author: Jesse Burt
Description: Driver for the MAX9744 20W audio amplifier IC
Copyright (c) 2023
Started Jul 7, 2018
Updated Jun 16, 2023
See end of file for terms of use.
--------------------------------------------
}
CON
{ default I/O configuration - these can be overridden by the parent object }
SCL = DEF_SCL
SDA = DEF_SDA
I2C_FREQ = DEF_HZ
I2C_ADDR = DEF_ADDR
SHDN = 24
DEF_SCL = 28
DEF_SDA = 29
DEF_HZ = 100_000
DEF_ADDR = 0
I2C_MAX_FREQ = core.I2C_MAX_FREQ
SLAVE_WR = core.SLAVE_ADDR
SLAVE_RD = core.SLAVE_ADDR|1
VAR
long _shdn
byte _vol_level, _mod_mode
OBJ
{ decide: Bytecode I2C engine, or PASM? Default is PASM if BC isn't specified }
#ifdef MAX9744_I2C_BC
i2c: "com.i2c.nocog" ' BC I2C engine
#else
i2c: "com.i2c" ' PASM I2C engine
#endif
core: "core.con.max9744" ' HW-specific constants
time: "time" ' timekeeping methods
PUB null()
' This is not a top-level object
PUB start(): status
' Start using default I/O settings
return startx(SCL, SDA, I2C_FREQ, SHDN)
PUB startx(SCL_PIN, SDA_PIN, I2C_HZ, SHDN_PIN): status
' Start using custom I/O settings
' Returns: Core/cog number+1 of I2C engine, FALSE if no cogs available
if ( lookdown(SCL_PIN: 0..31) and lookdown(SDA_PIN: 0..31) )
if ( status := i2c.init(SCL_PIN, SDA_PIN, I2C_HZ) )
time.msleep(1)
if ( i2c.present(SLAVE_WR) ) ' test device bus presence
_shdn := SHDN_PIN
powered(TRUE) ' SHDN pin high
return status
' if this point is reached, something above failed
' Double check I/O pin assignments, connections, power
' Lastly - make sure you have at least one free core/cog
return FALSE
PUB stop()
' Stop the driver
mute()
powered(FALSE)
i2c.deinit()
PUB modulation_mode(): curr_mode
' Get current output filter mode (cached)
return _mod_mode
PUB mute()
' Set 0 Volume
set_volume(0)
PUB is_powered(): curr_state
' Get current powered state
return outa[_shdn]
PUB powered(state)
' Power on or off
' Valid values:
' FALSE (0): Power off
' TRUE (non-zero): Power on
if ( state )
outa[_shdn] := 1
dira[_shdn] := 1
set_volume(_vol_level)
else
outa[_shdn] := 0
dira[_shdn] := 1
CON
{ set_modulation() filter settings }
NONE = 0
PWM = 1
PUB set_modulation(mode)
' Set modulation/output filter mode
' NONE (0): Filterless
' PWM (1): Classic PWM
if ( mode == NONE )
_mod_mode := core.MOD_FILTERLESS ' filterless modulation
elseif ( mode == PWM )
_mod_mode := core.MOD_CLASSICPWM ' classic PWM
powered(FALSE) ' cycle power to effect changes
powered(TRUE)
writereg(_mod_mode)
PUB set_volume(level)
' Set Volume to a specific level
' Valid values: 0..63
_vol_level := 0 #> level <# 63
writereg(_vol_level)
PUB vol_down()
' Decrease volume level
writereg(core.CMD_VOL_DN)
_vol_level := 0 #> (_vol_level - 1)
PUB vol_up()
' Increase volume level
writereg(core.CMD_VOL_UP)
_vol_level := (_vol_level + 1) <# 63
PUB volume(): curr_lvl
' Get current volume level (cached)
return _vol_level
PRI writereg(reg_nr) | cmd_pkt
' Write register/command to device
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := reg_nr
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 2)
i2c.stop()
DAT
{
Copyright 2023 Jesse Burt
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}