• That's a great idea! Like you mentioned, this sort of volume syncing is already built into android for bluetooth speakers and such so we should be able to just make it compatible with that.

    I made a few changes to my program, but I haven't figured it out yet. I did write a little helper file to simply log the music messages from gadgetbridge onto the display with timestamps. It seems to send twice as many messages as normal so I'm thinking maybe this is the cause of my problem?

    let lines = [];
    
    function breakIntoLines(str, maxWidth) {
      let chunks = str.split(','); // Split by commas
      let lines = [];
      let currentLine = "";
    
      chunks.forEach(chunk => {
        let words = chunk.trim().split(' '); // Split by spaces
        words.forEach((word, idx) => {
          let separator = idx === 0 ? "" : " ";
          let newContent = (currentLine.length > 0 ? separator : '') + word;
    
          if (g.stringWidth(currentLine + newContent) <= maxWidth) {
            currentLine += newContent;
          } else {
            if (currentLine.length > 0) {
              lines.push(currentLine);
            }
            currentLine = word;
          }
        });
    
        currentLine += ',';
      });
    
      if (currentLine.length > 0) {
        lines.push(currentLine);
      }
    
      return lines;
    }
    
    function breakWord(word, maxWidth) {
      let brokenLines = [];
      let currentLine = "";
    
      for (let char of word) {
        if (g.stringWidth(currentLine + char) <= maxWidth) {
          currentLine += char;
        } else {
          brokenLines.push(currentLine);
          currentLine = char;
        }
      }
    
      if (currentLine.length > 0) {
        brokenLines.push(currentLine);
      }
    
      return brokenLines;
    }
    
    function renderLines() {
      g.clear();
      let y = 0;
      lines.forEach((line, index) => {
        let sublines = breakIntoLines(line, g.getWidth() - 5);
        sublines.forEach(subline => {
          // calculate next Y position before drawing
          let nextY = y + g.getFontHeight();
          // if next Y position is outside of screen, remove oldest line
          if (nextY > g.getHeight()) {
            lines.pop();
            return; // Skip the current iteration
          }
          g.setFont('6x8');
          g.drawString(subline, 0, y);
          y = nextY;
        });
      });
    }
    
    function formatTimestamp() {
      const date = new Date();
      return `[${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}.${date.getMilliseconds().toString().padStart(3, '0')}]`;
    }
    
    function handleGadgetbridgeMessage(msg) {
      if (msg.t === 'musicstate' || msg.t === 'musicinfo') {
        let info = formatTimestamp() +
          (msg.state !== undefined ? ' ' + msg.state + ',' : '') +
          (msg.position !== undefined ? ' ' + msg.position : '') +
          (msg.track !== undefined ? ' ' + msg.track + ',' : '') +
          (msg.artist !== undefined ? ' ' + msg.artist + ',' : '') +
          (msg.album !== undefined ? ' ' + msg.album + ',' : '') +
          (msg.dur !== undefined ? ' ' + msg.dur : '');
    
        lines.unshift(info); // Add the info at the beginning of the array
        renderLines();  // Draw lines after every new message
      }
    }
    
About

Avatar for stweedo @stweedo started