-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
246 lines (235 loc) · 5.11 KB
/
main.ts
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
By Anson from KittenbotHK
Adapting TEA5767 extension for microbit
MicroPython ESP8266/ESP32 driver for TEA5767 FM radio module:
https://github.com/alankrantas/micropython-TEA5767
TEA5767 Datasheet:
https://www.sparkfun.com/datasheets/Wireless/General/TEA5767.pdf
*/
//% color="#fcba03" weight=10 icon="\uf2ce"
//% groups='["KittenRadio"]'
namespace KittenRadio {
const range_US = [87.5, 108.0]
const range_JP = [76.0, 91.0]
const addr = 0x60
let frequency = 88.1
let band = 'US'
let standby_mode = false
let mute_mode = false
let soft_mute_mode = true
let search_mode = false
let search_direction = 1
let search_adc_level = 7
let stereo_mode = true
let noise_cancel = true
let high_cut_mode = true
let is_ready = false
let is_stereo = false
let signal_adc_level = 0
/**
* Initialize Radio
*/
//% blockID=radio_init block="Initialization"
//% weight=100
//% group="TEA5767"
export function init() {
frequency = 88.1
band = 'US'
standby_mode = false
mute_mode = false
soft_mute_mode = true
search_mode = false
search_direction = 1
search_adc_level = 7
stereo_mode = true
noise_cancel = true
high_cut_mode = true
is_ready = false
is_stereo = false
signal_adc_level = 0
update()
}
/**
* Get current frequency
*/
// blockID=radio_return block="Get frequency|%freq"
// weight=60
// group="TEA5767"
/*
export function return_frequency(): number{
// update()
return frequency
}
*/
/**
* Set frequency
*/
//% blockID=radio_setFreq block="Set frequency|%freq"
//% weight=60
//% group="TEA5767"
export function set_frequency(freq: number) {
frequency = freq
update()
}
/**
* Change frequency
*/
//% blockID=radio_changeFreq block="Change frequency|%change"
//% weight=60
//% group="TEA5767"
/*
export function change_frequency(change: number) {
frequency = frequency+change
if (change >= 0) {
search_direction = 1
} else {
search_direction = 0
}
update()
}
*/
//% blockID=radio_search block="search|%mode|direction %dir|adc %adc"
//% weight=60
//% group="TEA5767"
//% advanced=true
export function search(mode: boolean, dir: number, adc: number) {
search_mode = mode
search_direction = dir
if (adc == 10 || adc == 7 || adc == 5 || adc == 0) {
search_adc_level = adc
} else {
search_adc_level = 7
}
update()
}
//% blockID=radio_mute block="mute|%mode"
//% weight=60
//% group="TEA5767"
//% advanced=true
export function mute(mode: boolean) {
mute_mode = mode
update()
}
//% blockID=radio_standby block="standby|%mode"
//% weight=60
//% group="TEA5767"
export function standby(mode: boolean) {
standby_mode = mode
update()
}
function read() {
//reg: number
//pins.i2cWriteNumber(addr, reg, NumberFormat.UInt8BE)
//let val = pins.i2cReadNumber(addr, NumberFormat.UInt8BE)
let temp=pins.i2cReadBuffer(0x60,5)
//let buf0=String(temp[0])
let buf0=temp[0]+""
//let buf1=String(temp[1])
let buf1=temp[1]+""
let buf01=buf0+buf1
let freqB=parseInt(buf01,2)
let freq= ((freqB*32768/4)-225000)/1000000
freq=Math.round(freq*10)/10
frequency=freq
if(parseInt(buf0,2)>>7==1){
is_ready=false
}else{
is_ready=true
}
if(parseInt(buf1,2)>>7==1){
is_stereo=false
}else{
is_stereo=true
}
let buf3=temp[3]+""
signal_adc_level=parseInt(buf3,2)>>4
}
function update() {
let buf = pins.createBuffer(5)
let cmd = ''
if (band == 'US') {
if (frequency < range_US[0]) {
frequency = range_US[0]
} else if (frequency > range_US[1]) {
frequency = range_US[1]
}
}
else {
if (frequency < range_JP[0]) {
frequency = range_JP[0]
} else if (frequency > range_JP[1]) {
frequency = range_JP[1]
}
}
let freqB = (frequency * 1000000 + 225000) * 4 / 32768
//basic.showNumber(freqB)
let freqH = Math.abs(freqB) >> 8
let freqL = Math.abs(freqB) & 0xFF
if (mute_mode) {
cmd = '1'
} else {
cmd = '0'
}
if (search_mode) {
cmd += '1'
} else {
cmd += '0'
}
buf[0] = Math.abs(freqH)
buf[1] = freqL
if (search_direction == 1) {
cmd = '1'
} else {
cmd = '0'
}
if (search_adc_level == 10) {
cmd += '11'
} else if (search_adc_level == 7) {
cmd += '10'
} else if (search_adc_level == 5) {
cmd += '01'
} else {
cmd += '00'
}
cmd += '1'
if (stereo_mode) {
cmd += '0'
} else {
cmd += '1'
}
cmd + '000'
buf[2]=parseInt(cmd,2)
cmd='0'
if (standby_mode){
cmd+='1'
}else{
cmd+='0'
}
if(band=='JP'){
cmd+='1'
}else{
cmd+='0'
}
cmd+='1'
if(soft_mute_mode){
cmd+='1'
}else{
cmd+='0'
}
if(high_cut_mode){
cmd+='1'
}else{
cmd+='0'
}
if(noise_cancel){
cmd+='1'
}else{
cmd+='0'
}
cmd+='0'
buf[3]=parseInt(cmd,2)
buf[4]=0
pins.i2cWriteBuffer(addr,buf)
read()
}
}