@@ -101,36 +101,6 @@ void VS1053g::toMp3() {
101
101
102
102
}
103
103
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
- }
134
104
// ----------------------------------------------------------------------------------
135
105
136
106
uint16_t VS1053g::setSpectrumBarColor ( uint16_t newbarcolor){
@@ -168,6 +138,40 @@ FILE *binfile;
168
138
169
139
return ( buf.st_size );
170
140
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
+
171
175
}
172
176
173
177
// ----------------------------------------------------------------------------------
@@ -176,19 +180,19 @@ void VS1053g::displaySpectrum() {
176
180
177
181
if (bands <= 0 || bands > 14 ) return ;
178
182
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 ;
183
186
184
187
if ( ! spectrum_sprite.created () ){
188
+ log_d ( " create spectrum sprite %d %d" , tft.width (), spectrum_height);
185
189
spectrum_sprite.createSprite ( tft.width (), spectrum_height );
186
190
spectrum_sprite.setTextColor ( TFT_GREEN, TFT_BLACK );
187
191
}
188
192
189
193
if (bands != prevbands) {
190
194
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);
192
196
}
193
197
194
198
spectrum_width = tft.width ();
@@ -248,23 +252,25 @@ void VS1053g::displaySpectrum() {
248
252
nextx = 0 ;
249
253
for (uint8_t i = 0 ; i < bands; i++) // Handle all sections
250
254
{
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
+
259
266
if (spectrum[i][2 ] > 0 ) {
260
267
spectrum[i][2 ]--;
261
268
}
262
269
if (spectrum[i][0 ] > spectrum[i][2 ]) {
263
270
spectrum[i][2 ] = spectrum[i][0 ];
264
271
}
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 );
268
274
269
275
spectrum[i][1 ] = spectrum[i][0 ];
270
276
barx += bar_width + 2 ;
0 commit comments