Skip to content

Commit

Permalink
changes: * use most recent M5Core2 library
Browse files Browse the repository at this point in the history
           see https://github.com/m5stack/M5Core2/releases/tag/0.1.6
           see schreibfaul1/ESP32-audioI2S#410 needs SpeakerEnable=false in M5.begin(...)
         * fixed bat status
         * enhanced GUI; add prev, add play/pause, re-aranged other widgets to free space
         * store index of song played last to resume on power-on
           ! hack and works only upt to 255 songs (just 1 byte of 4 byte int is stored)
           ! missing is recognition whether it is the same SD
         * default volume 8 (instead of 0)

libraries:
   M5Core2 (0.1.6/cb65d23) can be installed by Library Manager (deps do not need to be installed)
   ESP32-audioI2S (c612a07, newer than 3.0.0/c395d0f) needs to be manually installed
  • Loading branch information
drtrigon committed Dec 7, 2023
1 parent 5305f5d commit bd02f4a
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 25 deletions.
Binary file added data/icon-track-prev.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/ESP32-audioI2S-master-c612a07.zip
Binary file not shown.
Binary file added lib/M5Core2-0.1.6-cb65d23.zip
Binary file not shown.
Empty file removed lib/see v1
Empty file.
Binary file added media/icon-track-prev.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 14 additions & 9 deletions src/battery_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace singsang
class CBatteryWidget : public CBaseWidget
{
public:
CBatteryWidget() : CBaseWidget(270, 40, 40, 40) {}
CBatteryWidget() : CBaseWidget(270, 0, 40, 40) {}

void update()
{
Expand All @@ -21,23 +21,28 @@ class CBatteryWidget : public CBaseWidget
}
else
{
const auto batteryPower = M5.Axp.GetBatPower();
const float batVoltage = M5.Axp.GetBatVoltage();
const float batPercentage = (batVoltage < 3.2) ? 0 : (batVoltage - 3.2) * 100;

if (batteryPower > 0.20)
if (batPercentage > 80)
{
newIconPath = "/icon-battery-1.png";
newIconPath = "/icon-battery-4.png";
}
else if (batPercentage > 60)
{
newIconPath = "/icon-battery-3.png";
}
else if (batteryPower > 0.40)
else if (batPercentage > 40)
{
newIconPath = "/icon-battery-2.png";
}
else if (batteryPower > 0.60)
else if (batPercentage > 20)
{
newIconPath = "/icon-battery-3.png";
newIconPath = "/icon-battery-1.png";
}
else if (batteryPower > 0.80)
else
{
newIconPath = "/icon-battery-4.png";
newIconPath = "/icon-battery.png";
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/next_song_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace singsang
class CNextSongWidget : public CBaseWidget
{
public:
CNextSongWidget() : CBaseWidget(270, 160, 40, 40) {}
CNextSongWidget() : CBaseWidget(270, 200, 40, 40) {}

void update() {}

Expand Down
24 changes: 24 additions & 0 deletions src/pause_song_widget.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef SINGSANG_PAUSE_SONG_WIDGET_HPP
#define SINGSANG_PAUSE_SONG_WIDGET_HPP

#include "base_widget.hpp"

namespace singsang
{
class CPauseSongWidget : public CBaseWidget
{
public:
CPauseSongWidget() : CBaseWidget(270, 120, 40, 40) {}

void update() {}

void draw(const bool f_updateOnly)
{
M5.Lcd.drawPngFile(SPIFFS, "/icon-caret-right.png", m_positionX,
m_positionY, m_sizeX, m_sizeY);
}
};

} // namespace singsang

#endif // SINGSANG_PAUSE_SONG_WIDGET_HPP
74 changes: 63 additions & 11 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ void CPlayer::begin()
{
initializeHardware();
initializeGui();

// initialize Player (should be put into own method)
// hacky as int has size of 4 - should be using struct or json
// also store additional info to identify the SD and check whether its the same as last time
File status = SPIFFS.open("/status", FILE_READ);
if (status) {
m_activeSongIdx = int(status.read()); // hacky as int has size 4, works up to 255 only!

m_audio.connecttoSD(m_songFiles[m_activeSongIdx].c_str());
}
status.close();
}

void CPlayer::loop()
Expand All @@ -20,17 +31,16 @@ void CPlayer::loop()

handleInactivityTimeout();

// * start next song if nothing is playing (auto-skips non-supported formats)
// ! incompatible with inactivity detection or future pause/stop feature
// ! needs status variable playing/stopped that holds actual requested state, currently it is hard-coded to "playing"
if (!m_audio.isRunning()) { // not playing e.g. due to not supported file format
startNextSong();
}
if (m_isRunning) {
if (!m_audio.isRunning()) { // not playing e.g. due to not supported file format (hacky work-a-round)
startNextSong();
}
}
}

void CPlayer::initializeHardware()
{
M5.begin();
M5.begin(true, true, true, false, kMBusModeOutput, false); // SpeakerEnable = false

M5.Axp.SetLed(false);
M5.Axp.SetLcdVoltage(1800); // dimmed, nominal value is 2800
Expand All @@ -46,7 +56,7 @@ void CPlayer::initializeHardware()
delay(100);

m_audio.setPinout(12, 0, 2);
m_audio.setVolume(0);
m_audio.setVolume(m_currentVolume);

populateMusicFileList();

Expand All @@ -63,6 +73,8 @@ void CPlayer::initializeGui()
m_batteryWidget.draw(false);
m_fileSelectionWidget.draw(false);
m_nextSongWidget.draw(false);
m_prevSongWidget.draw(false);
m_pauseSongWidget.draw(false);
m_progressWidget.draw(false);
m_volumeDisplayWidget.draw(false);
m_volumeDownWidget.draw(false);
Expand Down Expand Up @@ -147,6 +159,18 @@ void CPlayer::handleTouchEvents()
startNextSong();
}

if (m_prevSongWidget.isTouched(touchPoint))
{
vibrate();
startPrevSong();
}

if (m_pauseSongWidget.isTouched(touchPoint))
{
vibrate();
pauseSong();
}

if (m_volumeDownWidget.isTouched(touchPoint))
{
vibrate();
Expand All @@ -167,18 +191,46 @@ void CPlayer::startNextSong()
return;
}

m_activeSongIdx++;
if (m_activeSongIdx >= m_songFiles.size() || m_activeSongIdx < 0)
m_activeSongIdx = (m_activeSongIdx + 1) % m_songFiles.size();

if (m_audio.isRunning())
{
m_audio.stopSong();
}

m_audio.connecttoSD(m_songFiles[m_activeSongIdx].c_str());

File status = SPIFFS.open("/status", FILE_WRITE);
status.write(char(m_activeSongIdx)); // hacky as int has size 4, works up to 255 only!
status.close();
}

void CPlayer::startPrevSong()
{
if (m_songFiles.size() == 0)
{
m_activeSongIdx = 0;
return;
}

m_activeSongIdx = (m_activeSongIdx - 1) % m_songFiles.size();

if (m_audio.isRunning())
{
m_audio.stopSong();
}

m_audio.connecttoSD(m_songFiles[m_activeSongIdx].c_str());

File status = SPIFFS.open("/status", FILE_WRITE);
status.write(char(m_activeSongIdx)); // hacky as int has size 4, works up to 255 only!
status.close();
}

void CPlayer::pauseSong()
{
m_audio.pauseResume();

m_isRunning = m_audio.isRunning();
}

void CPlayer::updateGui()
Expand Down
11 changes: 10 additions & 1 deletion src/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "battery_widget.hpp"
#include "file_selection_widget.hpp"
#include "next_song_widget.hpp"
#include "prev_song_widget.hpp"
#include "pause_song_widget.hpp"
#include "progress_widget.hpp"
#include "volume_display_widget.hpp"
#include "volume_down_widget.hpp"
Expand All @@ -33,6 +35,10 @@ class CPlayer

void startNextSong();

void startPrevSong();

void pauseSong();

void updateVolume(int f_deltaVolume);

void increaseVolume();
Expand All @@ -51,15 +57,18 @@ class CPlayer

Audio m_audio{};

int m_currentVolume{0};
int m_currentVolume{8};
int m_activeSongIdx{-1};
unsigned int m_turnOffAfterInactiveForMilliSec{5 * 60 * 1000};
unsigned int m_lastActivityTimestamp{0};
std::vector<String> m_songFiles{};
bool m_isRunning{true};

CBatteryWidget m_batteryWidget;
CFileSelectionWidget m_fileSelectionWidget;
CNextSongWidget m_nextSongWidget;
CPrevSongWidget m_prevSongWidget;
CPauseSongWidget m_pauseSongWidget;
CProgressWidget m_progressWidget;
CVolumeDisplayWidget m_volumeDisplayWidget;
CVolumeDownWidget m_volumeDownWidget;
Expand Down
24 changes: 24 additions & 0 deletions src/prev_song_widget.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef SINGSANG_PREV_SONG_WIDGET_HPP
#define SINGSANG_PREV_SONG_WIDGET_HPP

#include "base_widget.hpp"

namespace singsang
{
class CPrevSongWidget : public CBaseWidget
{
public:
CPrevSongWidget() : CBaseWidget(10, 200, 40, 40) {}

void update() {}

void draw(const bool f_updateOnly)
{
M5.Lcd.drawPngFile(SPIFFS, "/icon-track-prev.png", m_positionX,
m_positionY, m_sizeX, m_sizeY);
}
};

} // namespace singsang

#endif // SINGSANG_PREV_SONG_WIDGET_HPP
2 changes: 1 addition & 1 deletion src/volume_display_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace singsang
class CVolumeDisplayWidget : public CBaseWidget
{
public:
CVolumeDisplayWidget() : CBaseWidget(10, 80, 40, 80) {}
CVolumeDisplayWidget() : CBaseWidget(10, 40, 40, 80) {}

void update(const int f_newAudioVolume)
{
Expand Down
2 changes: 1 addition & 1 deletion src/volume_down_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace singsang
class CVolumeDownWidget : public CBaseWidget
{
public:
CVolumeDownWidget() : CBaseWidget(10, 160, 40, 40) {}
CVolumeDownWidget() : CBaseWidget(10, 120, 40, 40) {}

void draw(const bool f_updateOnly)
{
Expand Down
2 changes: 1 addition & 1 deletion src/volume_up_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace singsang
class CVolumeUpWidget : public CBaseWidget
{
public:
CVolumeUpWidget() : CBaseWidget(10, 40, 40, 40) {}
CVolumeUpWidget() : CBaseWidget(10, 0, 40, 40) {}

void draw(const bool f_updateOnly)
{
Expand Down

0 comments on commit bd02f4a

Please sign in to comment.