Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MSGEQ7 chip
- var msg7RESET = B9,
- msg7Strobe = B8,
- msg7DCout = A0;
- // Band values
- var levels = [];
- var poll = null;
- var band = 0,
- bass = 0,
- tmp = 0;
- SPI2.setup({baud:3200000, mosi:B15});
- var matrix = Graphics.createArrayBuffer(16,8,24,{zigzag:true});
- matrix.flip = function(){ SPI2.send4bit(matrix.buffer, 0b0001, 0b0011); };
- function onInit() {
- setInterval(loop, 1000/120);
- }
- function loop() {
- // Reset the MSGEQ7's counter
- digitalWrite(msg7RESET, 1);
- digitalWrite(msg7RESET, 0);
- band = 0;
- poll = setInterval(function(){
- if ((band&1)===0) {
- digitalWrite(msg7Strobe, 0);
- } else {
- // Get outputted value and store it
- tmp = analogRead(msg7DCout);
- if(tmp < 0.06) {
- levels[band>>1] = 0;
- } else {
- levels[band>>1] = tmp;
- }
- digitalWrite(msg7Strobe, 1);
- }
- // Stop after 7 bands
- band++;
- if(band >= 14) { clearInterval(poll); }
- }, 0.035); // 35 microseconds
- matrix.clear();
- fullLevels = expandLevels(levels);
- // Draw bars
- for(var i=0; i < fullLevels.length; i++) {
- drawBar(i, fullLevels[i]);
- }
- // BASS
- bass = fullLevels[0] / 3;
- matrix.setColor(fullLevels[0]/2,fullLevels[2]/2,fullLevels[10]/2);
- matrix.fillRect(14, 0, 15, 1);
- // MID
- bass = fullLevels[2] / 2;
- matrix.setColor(bass,0,bass);
- matrix.fillRect(14, 3, 15, 4);
- // MIX
- bass = fullLevels[10] / 2;
- matrix.setColor(bass,bass,0);
- matrix.fillRect(14, 6, 15, 7);
- matrix.flip();
- }
- function drawBar(bar, level) {
- level = Math.round(level * 8);
- matrix.setColor(0.05, 0, 0.05);
- matrix.fillRect(bar, 8-level, bar, 8);
- }
- function expandLevels(levels) {
- var newLevels = [];
- for(var i=0; i < levels.length; i++) {
- newLevels[i*2] = levels[i];
- if((i+1) < levels.length) {
- newLevels[i*2+1] = (levels[i] + levels[(i+1)]) / 2;
- }
- }
- return newLevels;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement