-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwave_player_main.jal
257 lines (233 loc) · 7.95 KB
/
wave_player_main.jal
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
247
248
249
250
251
252
253
254
255
256
-- Copyright (C) 2017,2019 avrin, http://mpu.seesaa.net
-- This program is licensed under BSD license.
-- Please refer to "LICENSE.txt".
--
-- btn: meaning Button switch
--
-- /******************
-- Varialble definitions
-- *******************/
const byte LED_PERIOD_PLAYNG = 75 ; [ * 10 msec]
const byte LED_PERIOD_PAUSING = 10 ; [ * 10 msec]
var dword gdwSongFileSectors = 0 ; set to 0 for auto start music
var word gwReadCount
if CH_MODE_STEREO then
const word wREAD_COUNT = 256 ; (512/2)
const byte bHEADER_COUNT = 44/2
else
const word wREAD_COUNT = 256*2
const byte bHEADER_COUNT = 44
end if
-- /******************
-- period_intr for PWM
-- *******************/
procedure period_intr() is
pragma interrupt
var byte datL,datR
if isPeriodTimer_IF() then
if TEST_PORT_ENABLE then
test_port =1
end if
datL = send_ff()
if CH_MODE_STEREO then
datR = send_ff()
end if
if CH_MODE_STEREO & defined(pwm_r_duty_fast) then ; normal route
pwm_l_duty_fast(datL)
pwm_r_duty_fast(datR)
else
var byte bDmono
;bDmono = byte( (word(datL) + datR)>>1 )
pwm_l_duty_fast(datL)
end if
gwReadCount = gwReadCount - 1
; check 512byte sector boundery
if gwReadCount == 0 then
; end 1 sector
datL = send_ff() ; dummy read. discard CRC
datL = send_ff()
while send_ff() != 0xFE loop end loop
gwReadCount = wREAD_COUNT
gdwSongFileSectors = gdwSongFileSectors - 1
end if
; end interrupt
periodTimer_IF_clear()
if TEST_PORT_ENABLE then
test_port =0
end if
end if ; isPeriodTimer_IF()
end procedure
-- /******************
-- wave_player_main
-- *******************/
procedure wave_player_main() is
pragma inline
var word wBtnLowCount = 0
if HAVE_LED_IND_BLINK then
var byte bTimeout_led = 0
end if
var bit btn_bit_prev = true ,btn_short_on = false
var bit btn_long_on = false
if HAVE_POWER_OFF_MODE then
btn_long_on2 = false
alias btn_power_off_on is btn_long_on2
end if
var bit btn_pause_prev = false
alias fPlaying is TMR2IE_bit
alias btn_next_song_on is btn_short_on
alias btn_pause_on is btn_long_on
gwReadCount = wREAD_COUNT
if HAVE_LED_IND_PWM then ; pseudo PWM setting
const sbyte IND_PERIOD = 125
const sbyte IND_DUTY_LOW_SPEED = 1
const sbyte IND_DUTY_HI_SPEED = 3
var sbyte sbIndDuty = 0
var sbyte sbIndCurrPos = 0
var sbyte sbIndSpeed = IND_DUTY_LOW_SPEED
var sbyte sbIndDelta = sbIndSpeed
end if
forever loop
; wait 10msec Ticker flag
while !isTickTimer_IF() loop
if gdwSongFileSectors == 0 then ; found end of file
exit loop ; promptly exit and prepare next song
end if
if HAVE_LED_IND_PWM then
---------------------
-- pseudo PWM for LED
---------------------
if sbIndCurrPos < sbIndDuty then
led = 1
else
led = 0
end if
sbIndCurrPos = sbIndCurrPos + 1
if sbIndCurrPos == IND_PERIOD then
sbIndCurrPos = 0
end if
end if
end loop
-------------------
tickTimer_IF_clear()
tickTimer_reload()
-------------------
-------------------
-- Go to sleep mode (== Power off)
-------------------
;include power_off_mode
-------------------
--- Next song and start
-------------------
if ( gdwSongFileSectors == 0 ) | btn_next_song_on then
pwm_period_timer_stop()
btn_next_song_on = false
sd_stop_read()
-------------------
--- Search next song
-------------------
searchNextFile()
;# Seek to Target file sector
sd_start_read( gdwTargetFileSector )
; delete about last 30Kbyte to cut tag data in *.wav file
gdwSongFileSectors = dword( (gdwBPB_FileSize - CUT_LAST_TAG_NOISE) )>>9 ; /512 , change to sector
-------------------
--- skip WAV header size
-------------------
sd_read_pulse_byte(44) ;
-------------------
--- music start
-------------------
gwReadCount = wREAD_COUNT - bHEADER_COUNT
pwm_period_timer_start()
end if
-------------------
--- LED indicator 1 --- pseudo PWM
-------------------
if HAVE_LED_IND_PWM & !defined(PIN8_MCU) then
sbIndDuty = sbIndDuty + sbIndDelta
if sbIndDuty > IND_PERIOD then
sbIndDelta = -1 * sbIndSpeed
end if
if sbIndDuty == 0 then
sbIndDelta = sbIndSpeed
end if
if fPlaying then
sbIndSpeed = IND_DUTY_LOW_SPEED
else
sbIndSpeed = IND_DUTY_HI_SPEED
end if
end if
-------------------
--- LED indicator 2 --- simple ON/OFF
-------------------
if defined(HAVE_LED_IND_BLINK) & !defined(PIN8_MCU) then
if HAVE_LED_IND_BLINK then
if bTimeout_led == 0 then
if fPlaying then
bTimeout_led = LED_PERIOD_PLAYNG ; during Playing, on/off
led = !led
else
if HAVE_LED_PAUSE_INDICATOR then
bTimeout_led = LED_PERIOD_PAUSING ; during Pause, on/off
led = !led
else
bTimeout_led = 1
led = 0
end if
end if
end if
bTimeout_led = bTimeout_led - 1
end if
end if
-------------------
--- button sw input
-------------------
if HAVE_BUTTON_SW then
-- Using this push switch
-- http://akizukidenshi.com/catalog/g/gP-01282/
--
var bit btn = btn_bit_now
if btn_bit_prev ^ btn then
if btn then ; 0 --> 1: btn released
if (wBtnLowCount > 10) & (wBtnLowCount < 130) then ; 100msec < x < 1.3sec
btn_short_on = true
end if
wBtnLowCount = 0
end if
end if
btn_bit_prev = btn
if btn == false then
wBtnLowCount = wBtnLowCount + 1
end if
if (wBtnLowCount > 120) then ; 1.2sec >
; recognized pause on
btn_long_on = true
end if
if HAVE_POWER_OFF_MODE then
if (wBtnLowCount > 400) then ; 4sec >
; "long on2" is meaning go to sleep mode
btn_long_on2 = true;
end if
end if
-------------------
-- Pause release
-------------------
if btn_next_song_on then
if !fPlaying then ; if during pause
fPlaying = true; release pause
btn_next_song_on = false
btn_pause_on = false
end if
end if
-------------------
--- Pause enter
-------------------
if btn_pause_prev ^ btn_pause_on then
if btn_pause_on then
fPlaying = false
end if
end if
btn_pause_prev = btn_pause_on
end if
end loop ; [forever loop end]
end procedure