Skip to content

Commit bf35a3d

Browse files
committed
Update VS1053g.cpp
Bug in getBands() and displaySpectrum(), more dynamic display
1 parent 07e8337 commit bf35a3d

File tree

1 file changed

+52
-46
lines changed

1 file changed

+52
-46
lines changed

VS1053g.cpp

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -101,36 +101,6 @@ void VS1053g::toMp3() {
101101

102102
}
103103

104-
//----------------------------------------------------------------------------------
105-
#define BASE 0x1810
106-
void VS1053g::getBands()
107-
{
108-
const uint16_t base = 0x1810;
109-
const uint8_t wramaddr=7,wram =6;
110-
//await_data_request(); should be verified in app jb
111-
112-
write_register( wramaddr, base + 2);
113-
bands = read_register( wram);
114-
115-
write_register(wramaddr, base + 4);
116-
117-
uint8_t current_volume = getVolume();
118-
119-
for (uint8_t i = 0; i < 14; i++) {
120-
uint8_t val = read_register( wram );
121-
/* current value in bits 5..0, normally 0..31
122-
peak value in bits 11..6, normally 0..31 */
123-
uint8_t cur1 = val & 31;
124-
int8_t cur = (cur1 * current_volume ) / 30; // big bars
125-
if ( cur < 0 ) {
126-
cur = 0;
127-
} else if ( cur > ( spectrum_height - 4) ){
128-
cur = spectrum_height - 4;
129-
}
130-
spectrum[i][0] = cur;
131-
}
132-
133-
}
134104
//----------------------------------------------------------------------------------
135105

136106
uint16_t VS1053g::setSpectrumBarColor( uint16_t newbarcolor){
@@ -168,6 +138,40 @@ FILE *binfile;
168138

169139
return( buf.st_size );
170140

141+
}
142+
//----------------------------------------------------------------------------------
143+
144+
void VS1053g::getBands()
145+
{
146+
const uint16_t base = 0x1810;
147+
const uint8_t wramaddr=7,wram =6;
148+
//await_data_request(); should be verified in play() jb
149+
150+
write_register( wramaddr, base + 2);
151+
bands = read_register( wram);
152+
153+
write_register(wramaddr, base + 4);
154+
155+
uint8_t current_volume = getVolume();
156+
157+
for (uint8_t i = 0; i < 14; i++) {
158+
uint8_t val = read_register( wram );
159+
/* current value in bits 5..0, normally 0..31
160+
peak value in bits 11..6, normally 0..31
161+
*/
162+
uint32_t fivebitvalue = (val&31);
163+
uint32_t barheightperc = ( fivebitvalue * (uint32_t)current_volume ) / 31; // min = 0, max = 31*100 div by 100 = 0-100
164+
165+
// by using a value lower than 100 to divide the value,
166+
// we "amplify" the difference in volume between calls to getBands().
167+
// The lower this value, the more nerve-wrecking the display becomes.
168+
// As we are writing to a sprite, bars that are too long will be
169+
// cropped automatically.
170+
171+
int barheight = (barheightperc * spectrum_height)/60;
172+
spectrum[i][0] = barheight;
173+
}
174+
171175
}
172176

173177
//----------------------------------------------------------------------------------
@@ -176,19 +180,19 @@ void VS1053g::displaySpectrum() {
176180

177181
if (bands <= 0 || bands > 14) return;
178182

179-
uint8_t bar_width = tft.width() / bands - 2;
180-
uint16_t barx = 2; // start location of the first bar
181-
boolean visual = true; //paint to display
182-
static int nextx = 0;
183+
uint8_t bar_width = tft.width() / bands - 2;
184+
uint16_t barx = 2; // start location of the first bar
185+
static int nextx = 0;
183186

184187
if ( ! spectrum_sprite.created() ){
188+
log_d ( "create spectrum sprite %d %d", tft.width(), spectrum_height);
185189
spectrum_sprite.createSprite( tft.width(), spectrum_height );
186190
spectrum_sprite.setTextColor( TFT_GREEN, TFT_BLACK );
187191
}
188192

189193
if (bands != prevbands) {
190194
prevbands = bands;
191-
if (visual) spectrum_sprite.fillRect (0,0, tft.width(), spectrum_height, TFT_BLACK);
195+
spectrum_sprite.fillRect (0,0, tft.width(), spectrum_height, TFT_BLACK);
192196
}
193197

194198
spectrum_width = tft.width();
@@ -248,23 +252,25 @@ void VS1053g::displaySpectrum() {
248252
nextx = 0;
249253
for (uint8_t i = 0; i < bands; i++) // Handle all sections
250254
{
251-
if (visual) {
252-
if (spectrum[i][0] > spectrum[i][1]) {
253-
spectrum_sprite.fillRect (barx, spectrum_height - spectrum[i][0], bar_width, spectrum[i][0], spectrum_barcolor );
254-
spectrum_sprite.fillRect (barx, 0, bar_width, spectrum_height - spectrum[i][0], TFT_BLACK);
255-
} else {
256-
spectrum_sprite.fillRect (barx, 0, bar_width, spectrum_height - spectrum[i][0], TFT_BLACK);
257-
}
258-
}
255+
256+
int bartop = spectrum_height - spectrum[i][0];
257+
int barheight = spectrum[i][0];
258+
259+
if (spectrum[i][0] > spectrum[i][1]) {
260+
spectrum_sprite.fillRect (barx, bartop, bar_width, barheight, spectrum_barcolor );
261+
spectrum_sprite.fillRect (barx, 0, bar_width, bartop, TFT_BLACK);
262+
} else {
263+
spectrum_sprite.fillRect (barx, 0, bar_width, bartop, TFT_BLACK);
264+
}
265+
259266
if (spectrum[i][2] > 0) {
260267
spectrum[i][2]--;
261268
}
262269
if (spectrum[i][0] > spectrum[i][2]) {
263270
spectrum[i][2] = spectrum[i][0];
264271
}
265-
if (visual) {
266-
spectrum_sprite.fillRect (barx, spectrum_height - spectrum[i][2] - 3, bar_width, 2, spectrum_peakcolor );
267-
}
272+
273+
spectrum_sprite.fillRect (barx, spectrum_height - spectrum[i][2] - 3, bar_width, 2, spectrum_peakcolor );
268274

269275
spectrum[i][1] = spectrum[i][0];
270276
barx += bar_width + 2;

0 commit comments

Comments
 (0)