Skip to content

Commit

Permalink
Merge pull request #26 from m5stack/develop
Browse files Browse the repository at this point in the history
0.0.7
  • Loading branch information
lovyan03 committed Apr 21, 2022
2 parents c08fa4d + 14748a2 commit 42717f0
Show file tree
Hide file tree
Showing 12 changed files with 245 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
#include <M5UnitOLED.h>
#include <M5Unified.h>

static constexpr const int bufferSize = 64 * 1024; // buffer size in byte

/// set M5Speaker virtual channel (0-7)
static constexpr uint8_t m5spk_virtual_channel = 0;

Expand Down Expand Up @@ -79,7 +77,7 @@ class AudioOutputM5Speaker : public AudioOutput
_m5sound->stop(_virtual_ch);
for (size_t i = 0; i < 3; ++i)
{
memset(_tri_buffer[i], 0, tri_buf_size);
memset(_tri_buffer[i], 0, tri_buf_size * sizeof(int16_t));
}
++_update_count;
return true;
Expand Down Expand Up @@ -189,11 +187,15 @@ public:
}
};

static constexpr const int preallocateBufferSize = 5 * 1024;
static constexpr const int preallocateCodecSize = 29192; // MP3 codec max mem needed
static void* preallocateBuffer = nullptr;
static void* preallocateCodec = nullptr;
static constexpr size_t WAVE_SIZE = 320;
static AudioOutputM5Speaker out(&M5.Speaker, m5spk_virtual_channel);
static AudioGeneratorMP3 *mp3 = nullptr;
static AudioFileSourceICYStream *filemp3 = nullptr;
static AudioFileSourceBuffer *buffmp3 = nullptr;
static AudioGenerator *decoder = nullptr;
static AudioFileSourceICYStream *file = nullptr;
static AudioFileSourceBuffer *buff = nullptr;
static fft_t fft;
static bool fft_enabled = false;
static bool wave_enabled = false;
Expand All @@ -208,8 +210,9 @@ static char stream_title[128] = { 0 };
static const char* meta_text[2] = { nullptr, stream_title };
static const size_t meta_text_num = sizeof(meta_text) / sizeof(meta_text[0]);
static uint8_t meta_mod_bits = 0;
static volatile size_t playindex = ~0u;

void MDCallback(void *cbData, const char *type, bool isUnicode, const char *string)
static void MDCallback(void *cbData, const char *type, bool isUnicode, const char *string)
{
(void)cbData;
if ((strcmp(type, "StreamTitle") == 0) && (strcmp(stream_title, string) != 0))
Expand All @@ -219,44 +222,59 @@ void MDCallback(void *cbData, const char *type, bool isUnicode, const char *stri
}
}

void stop(void)
static void stop(void)
{
if (mp3) {
mp3->stop();
delete mp3;
mp3 = nullptr;
if (decoder) {
decoder->stop();
delete decoder;
decoder = nullptr;
}

if (buffmp3) {
buffmp3->close();
delete buffmp3;
buffmp3 = nullptr;
if (buff) {
buff->close();
delete buff;
buff = nullptr;
}
if (filemp3) {
filemp3->close();
delete filemp3;
filemp3 = nullptr;
if (file) {
file->close();
delete file;
file = nullptr;
}
out.stop();
}

void play(size_t index)
static void play(size_t index)
{
playindex = index;
}

static void decodeTask(void*)
{
stop();
filemp3 = new AudioFileSourceICYStream(station_list[index][1]);
filemp3->RegisterMetadataCB(MDCallback, (void*)"ICY");
// StreamTitle
buffmp3 = new AudioFileSourceBuffer(filemp3, bufferSize);
mp3 = new AudioGeneratorMP3();
mp3->begin(buffmp3, &out);
mp3->loop();

meta_text[0] = station_list[index][0];
stream_title[0] = 0;
meta_mod_bits = 3;
for (;;)
{
delay(1);
if (playindex != ~0u)
{
auto index = playindex;
playindex = ~0u;
stop();
meta_text[0] = station_list[index][0];
stream_title[0] = 0;
meta_mod_bits = 3;
file = new AudioFileSourceICYStream(station_list[index][1]);
file->RegisterMetadataCB(MDCallback, (void*)"ICY");
buff = new AudioFileSourceBuffer(file, preallocateBuffer, preallocateBufferSize);
decoder = new AudioGeneratorMP3(preallocateCodec, preallocateCodecSize);
decoder->begin(buff, &out);
}
if (decoder && decoder->isRunning())
{
if (!decoder->loop()) { decoder->stop(); }
}
}
}

uint32_t bgcolor(LGFX_Device* gfx, int y)
static uint32_t bgcolor(LGFX_Device* gfx, int y)
{
auto h = gfx->height();
auto dh = h - header_height;
Expand All @@ -272,7 +290,7 @@ uint32_t bgcolor(LGFX_Device* gfx, int y)
return gfx->color888(v + 2, v, v + 6);
}

void gfxSetup(LGFX_Device* gfx)
static void gfxSetup(LGFX_Device* gfx)
{
if (gfx == nullptr) { return; }
if (gfx->width() < gfx->height())
Expand Down Expand Up @@ -390,20 +408,7 @@ void gfxLoop(LGFX_Device* gfx)
}
}

if (!gfx->displayBusy())
{ // draw volume bar
static int px;
uint8_t v = M5.Speaker.getChannelVolume(m5spk_virtual_channel);
int x = v * (gfx->width()) >> 8;
if (px != x)
{
gfx->fillRect(x, 6, px - x, 2, px < x ? 0xAAFFAAu : 0u);
gfx->display();
px = x;
}
}

if (fft_enabled && !gfx->displayBusy() && M5.Speaker.isPlaying(m5spk_virtual_channel) > 1)
if (fft_enabled)
{
static int prev_x[2];
static int peak_x[2];
Expand Down Expand Up @@ -543,24 +548,22 @@ void gfxLoop(LGFX_Device* gfx)
gfx->endWrite();
}
}
}

void gfxLoopTask(void*)
{
uint32_t prev_update_count = 0;
for (;;)
{
delay(1);
uint32_t update_count = out.getUpdateCount();
if (prev_update_count != update_count)
if (!gfx->displayBusy())
{ // draw volume bar
static int px;
uint8_t v = M5.Speaker.getChannelVolume(m5spk_virtual_channel);
int x = v * (gfx->width()) >> 8;
if (px != x)
{
prev_update_count = update_count;
gfxLoop(&M5.Display);
gfx->fillRect(x, 6, px - x, 2, px < x ? 0xAAFFAAu : 0u);
gfx->display();
px = x;
}
}
}

void setup()
void setup(void)
{
auto cfg = M5.config();

Expand All @@ -570,6 +573,12 @@ void setup()

M5.begin(cfg);

preallocateBuffer = malloc(preallocateBufferSize);
preallocateCodec = malloc(preallocateCodecSize);
if (!preallocateBuffer || !preallocateCodec) {
M5.Display.printf("FATAL ERROR: Unable to preallocate %d bytes for app\n", preallocateBufferSize + preallocateCodecSize);
for (;;) { delay(1000); }
}

{ /// custom setting
auto spk_cfg = M5.Speaker.config();
Expand Down Expand Up @@ -604,18 +613,21 @@ void setup()

play(station_index);

xTaskCreatePinnedToCore(gfxLoopTask, "gfxLoopTask", 2048, nullptr, 1, nullptr, PRO_CPU_NUM);
xTaskCreatePinnedToCore(decodeTask, "decodeTask", 4096, nullptr, 1, nullptr, PRO_CPU_NUM);
}

void loop()
void loop(void)
{
if (mp3->isRunning())
{
if (!mp3->loop()) { mp3->stop(); }
}
else
gfxLoop(&M5.Display);

{
delay(16);
static int prev_frame;
int frame;
do
{
delay(1);
} while (prev_frame == (frame = millis() >> 3)); /// 8 msec cycle wait
prev_frame = frame;
}

M5.update();
Expand Down
6 changes: 3 additions & 3 deletions library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "M5Unified",
"description": "Library for M5Stack/Core2/Tough, M5StickC/C-Plus, M5CoreInk, M5Paper, M5ATOM, M5STAMP",
"description": "Library for M5Stack/Core2/Tough, M5StickC/C-Plus, M5CoreInk, M5Paper, M5ATOM, M5STAMP, M5Station",
"keywords": "M5Unified",
"authors": {
"name": "M5Stack, lovyan03",
Expand All @@ -13,10 +13,10 @@
"dependencies": [
{
"name": "M5GFX",
"version": ">=0.0.19"
"version": ">=0.0.20"
}
],
"version": "0.0.6",
"version": "0.0.7",
"frameworks": "arduino",
"platforms": "espressif32",
"headers": "M5Unified.h"
Expand Down
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name=M5Unified
version=0.0.6
version=0.0.7
author=M5Stack
maintainer=M5Stack
sentence=Library for M5Stack/Core2/Tough, M5StickC/C-Plus, M5CoreInk, M5Paper, M5ATOM, M5STAMP
sentence=Library for M5Stack/Core2/Tough, M5StickC/C-Plus, M5CoreInk, M5Paper, M5ATOM, M5STAMP, M5Station
paragraph=M5Stack, M5Stack Core2, M5Stack CoreInk, M5StickC, M5StickC-Plus, M5Paper, M5Tough, M5ATOM, M5STAMP, M5Station, See more on http://M5Stack.com
category=Display
url=https://github.com/m5stack/M5Unified.git
Expand Down
13 changes: 4 additions & 9 deletions src/M5Unified.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,8 @@ namespace m5
;
*((volatile uint32_t *)(IO_MUX_GPIO20_REG)) = tmp;
}

{
i2c_port_t in_port = I2C_NUM_0;
gpio_num_t in_sda = GPIO_NUM_1;
gpio_num_t in_scl = GPIO_NUM_0;
In_I2C.begin(in_port, in_sda, in_scl);
}
/// StampC3 does not have internal i2c.
In_I2C.setPort(-1, -1, -1);

{ /// setup External I2C
i2c_port_t ex_port = I2C_NUM_0;
Expand Down Expand Up @@ -498,7 +493,7 @@ namespace m5
M5.Ex_I2C.begin();
}

if (_cfg.internal_rtc)
if (_cfg.internal_rtc && In_I2C.isEnabled())
{
M5.Rtc.begin();
}
Expand All @@ -509,7 +504,7 @@ namespace m5

M5.Rtc.setSystemTimeFromRtc();

if (_cfg.internal_imu)
if (_cfg.internal_imu && In_I2C.isEnabled())
{
if (M5.Imu.begin())
{
Expand Down
10 changes: 5 additions & 5 deletions src/M5Unified.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ namespace m5
{
#if defined ( ARDUINO )

/// use "Serial" begin.
/// use "Serial" begin. (0=disabled)
uint32_t serial_baudrate = 115200;

#endif

/// Clear the screen.
/// Clear the screen when startup.
bool clear_display = true;

/// use external port 5V output.
/// 5V output to external port.
bool output_power = true;

/// use PMIC(AXP192) pek for M5.BtnPWR.
Expand All @@ -72,10 +72,10 @@ namespace m5
/// use internal RTC.
bool internal_rtc = true;

/// use mic.
/// use the microphone.
bool internal_mic = true;

/// use speaker.
/// use the speaker.
bool internal_spk = true;

/// use Unit Accel & Gyro.
Expand Down
2 changes: 1 addition & 1 deletion src/gitTagVersion.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define M5UNIFIED_VERSION_MAJOR 0
#define M5UNIFIED_VERSION_MINOR 0
#define M5UNIFIED_VERSION_PATCH 6
#define M5UNIFIED_VERSION_PATCH 7
#define M5UNIFIED_VERSION F( M5UNIFIED_VERSION_MAJOR "." M5UNIFIED_VERSION_MINOR "." M5UNIFIED_VERSION_PATCH )
6 changes: 3 additions & 3 deletions src/utility/AXP192_Class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace m5
LDOio0: 1.8-3.3V, 100mV/step 50mA
LDO1 : 30mA always on
LDO2 : 1.8-3.3V, 100mV/step 200mA
LDO2 : 1.8-3.3V, 100mV/step 200mA
LDO3 : 1.8-3.3V, 100mV/step 200mA
*/
bool AXP192_Class::begin(void)
Expand Down Expand Up @@ -167,7 +167,7 @@ namespace m5
{
max_mV = (max_mV / 10) - 410;
if (max_mV > 436 - 410) { max_mV = 436 - 410; }
static constexpr std::uint8_t table[] =
static constexpr std::uint8_t table[] =
{ 415 - 410 /// 4150mV
, 420 - 410 /// 4200mV
, 436 - 410 /// 4360mV
Expand Down Expand Up @@ -196,7 +196,7 @@ namespace m5
: 0;
if (current > 16) { res -= 16; }

return (res < 100) ? res : 100;
return (res < 100) ? res : 100;
}

bool AXP192_Class::isCharging(void)
Expand Down
8 changes: 8 additions & 0 deletions src/utility/AXP192_Class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,16 @@ namespace m5
inline void setDCDC2(int voltage) { _set_DCDC(1, voltage); }
inline void setDCDC3(int voltage) { _set_DCDC(2, voltage); }

/// set LDOio0 voltage
/// @param voltage milli volt. (0 - 3300).
inline void setLDO0(int voltage) { _set_LDO(0, voltage); }

/// set LDO2 voltage
/// @param voltage milli volt. (0 - 3300).
inline void setLDO2(int voltage) { _set_LDO(2, voltage); }

/// set LDO3 voltage
/// @param voltage milli volt. (0 - 3300).
inline void setLDO3(int voltage) { _set_LDO(3, voltage); }

inline void setGPIO(uint8_t gpio_num, bool state) { if (gpio_num < 3) { _set_GPIO0_2(gpio_num, state); } else { _set_GPIO3_4(gpio_num - 3, state); } }
Expand Down
Loading

0 comments on commit 42717f0

Please sign in to comment.