Skip to content

Commit

Permalink
fix issues : low number of music or stories make crash
Browse files Browse the repository at this point in the history
  • Loading branch information
DantSu committed Feb 29, 2024
1 parent 6ed34a0 commit 3b7aca9
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
###########################################################

TARGET=TelmiOS
VERSION=1.0.2
VERSION=1.0.4

###########################################################

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The story teller is compatible with stories exported from [STUdio](https://githu

### Download the installation files

- [Download the latest version of Telmi](https://github.com/DantSu/Telmi-story-teller/releases/download/1.0.2/TelmiOS_v1.0.2.zip)
- [Download the latest version of Telmi](https://github.com/DantSu/Telmi-story-teller/releases/download/1.0.4/TelmiOS_v1.0.4.zip)

### Format your SD card as FAT32 (not exFAT!)

Expand Down Expand Up @@ -46,9 +46,9 @@ Make sure you choose: `For use with all systems and devices (FAT)`

Chrome already has a tool to format an SD card. Insert the card into your Chromebook, right-click it and click **Format Device**, make sure `FAT32` is selected under **Format** and click or tap **Erase & Format**.

### Unzip TelmiOS_v1.0.2.zip
### Unzip TelmiOS_v1.0.4.zip

Put the content of `TelmiOS_v1.0.2.zip` at the root of the SD card.
Put the content of `TelmiOS_v1.0.4.zip` at the root of the SD card.

### Power on your Miyoo

Expand Down
102 changes: 70 additions & 32 deletions src/storyTeller/music_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ void musicplayer_interfaceplayer_drawSideMusic(int index, int top) {
} else if (mIndex >= musicPlayerTracksCount) {
mIndex = mIndex - musicPlayerTracksCount;
}

if(mIndex < 0 || mIndex >= musicPlayerTracksCount) {
return;
}

char fileImageName[STR_MAX], writeTitle[STR_MAX], writeArtist[STR_MAX],
imageName[STR_MAX - 4], imageNameCopy[STR_MAX - 4], imageNameDelimiter[] = "_";
Expand Down Expand Up @@ -244,6 +248,10 @@ void musicplayer_changeAlbum(int direction) {

void musicplayer_up(void)
{
if(musicPlayerTracksCount == 0) {
return;
}

musicplayer_screenActivate();
if(musicPlayerMode == MUSICPLAYER_MODE_PLAYER) {
musicPlayerTrackPosition = 0;
Expand All @@ -256,6 +264,10 @@ void musicplayer_up(void)

void musicplayer_down(void)
{
if(musicPlayerTracksCount == 0) {
return;
}

musicplayer_screenActivate();
if(musicPlayerMode == MUSICPLAYER_MODE_PLAYER) {
musicPlayerTrackPosition = 0;
Expand Down Expand Up @@ -283,6 +295,10 @@ void musicplayer_rewind(int time)

void musicplayer_next(void)
{
if(musicPlayerTracksCount == 0) {
return;
}

musicplayer_screenActivate();
if(musicPlayerMode == MUSICPLAYER_MODE_PLAYER) {
musicplayer_rewind(10);
Expand All @@ -293,6 +309,10 @@ void musicplayer_next(void)

void musicplayer_previous(void)
{
if(musicPlayerTracksCount == 0) {
return;
}

musicplayer_screenActivate();
if(musicPlayerMode == MUSICPLAYER_MODE_PLAYER) {
musicplayer_rewind(-10);
Expand All @@ -303,6 +323,10 @@ void musicplayer_previous(void)

void musicplayer_ok(void)
{
if(musicPlayerTracksCount == 0) {
return;
}

musicplayer_screenActivate();
if(musicPlayerMode == MUSICPLAYER_MODE_ALBUM) {
musicPlayerMode = MUSICPLAYER_MODE_PLAYER;
Expand All @@ -314,6 +338,10 @@ void musicplayer_ok(void)

void musicplayer_autoplay(void)
{
if(musicPlayerTracksCount == 0) {
return;
}

musicPlayerTrackPosition = 0;
musicPlayerTrackIndex += 1;
musicplayer_load();
Expand All @@ -322,6 +350,10 @@ void musicplayer_autoplay(void)

void musicplayer_pause(void)
{
if(musicPlayerTracksCount == 0) {
return;
}

musicplayer_screenActivate();
if(Mix_PlayingMusic() == 1) {
if (Mix_PausedMusic() == 1) {
Expand All @@ -339,6 +371,10 @@ void musicplayer_pause(void)

bool musicplayer_home(void)
{
if(musicPlayerTracksCount == 0) {
return true;
}

if(musicPlayerMode == MUSICPLAYER_MODE_PLAYER) {
if (Mix_PlayingMusic() == 1 && Mix_PausedMusic() != 1) {
musicPlayerTrackPosition += get_time() - musicPlayerTrackStartTime;
Expand All @@ -354,6 +390,10 @@ bool musicplayer_home(void)

void musicplayer_menu(void)
{
if(musicPlayerTracksCount == 0) {
return;
}

musicplayer_screenActivate();
if(musicPlayerMode == MUSICPLAYER_MODE_PLAYER) {
musicplayer_setMode(MUSICPLAYER_MODE_ALBUM);
Expand Down Expand Up @@ -411,46 +451,44 @@ void musicplayer_init(void)
musicPlayerTracksCount++;
}
}
rewinddir(d);


char **filesList = (char**)malloc(musicPlayerTracksCount * sizeof(char*));
int i = 0;

while ((dir = readdir(d)) != NULL) {
if (dir->d_type == DT_REG && musicplayer_isMp3File(dir->d_name)) {
filesList[i] = malloc(STR_MAX);
strcpy(filesList[i], dir->d_name);
i++;
if (musicPlayerTracksCount > 0) {
musicPlayerTracksList = (char**)malloc(musicPlayerTracksCount * sizeof(char*));
int i = 0;

rewinddir(d);
while ((dir = readdir(d)) != NULL) {
if (dir->d_type == DT_REG && musicplayer_isMp3File(dir->d_name)) {
musicPlayerTracksList[i] = malloc(STR_MAX);
strcpy(musicPlayerTracksList[i], dir->d_name);
i++;
}
}
}
closedir(d);

if (musicPlayerTracksCount > 0) {
sort(filesList, musicPlayerTracksCount);
}
sort(musicPlayerTracksList, musicPlayerTracksCount);

char lastAlbum[STR_MAX] = {'\0'};
musicPlayerAlbumsCount = 0;
for (i = 0; i < musicPlayerTracksCount; i++)
{
if (musicplayer_isNewAlbum(filesList[i], lastAlbum)) {
musicPlayerAlbumsCount++;
char lastAlbum[STR_MAX] = {'\0'};
musicPlayerAlbumsCount = 0;
for (i = 0; i < musicPlayerTracksCount; i++)
{
if (musicplayer_isNewAlbum(musicPlayerTracksList[i], lastAlbum)) {
musicPlayerAlbumsCount++;
}
}
}

musicPlayerAlbumsIndex = malloc(sizeof(int) * musicPlayerAlbumsCount);
lastAlbum[0] = '\0';
int j = 0;
for (i = 0; i < musicPlayerTracksCount; i++)
{
if (musicplayer_isNewAlbum(filesList[i], lastAlbum)) {
musicPlayerAlbumsIndex[j] = i;
j++;
musicPlayerAlbumsIndex = (int*)malloc(sizeof(int) * musicPlayerAlbumsCount);
lastAlbum[0] = '\0';
int j = 0;
for (i = 0; i < musicPlayerTracksCount; i++)
{
if (musicplayer_isNewAlbum(musicPlayerTracksList[i], lastAlbum)) {
musicPlayerAlbumsIndex[j] = i;
j++;
}
}
}

musicPlayerTracksList = filesList;
closedir(d);
musicplayer_load();
}

Expand Down
30 changes: 29 additions & 1 deletion src/storyTeller/stories_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,21 @@ void stories_changeTitle(int direction) {

void stories_up(void)
{
if(storiesCount == 0) {
return;
}

if(storiesDiplayMode == STORIES_DISPLAY_MODE_TILES) {
stories_changeTitle(-3);
}
}

void stories_down(void)
{
if(storiesCount == 0) {
return;
}

if(storiesDiplayMode == STORIES_DISPLAY_MODE_TILES) {
stories_changeTitle(3);
}
Expand All @@ -346,6 +354,10 @@ void stories_rewind(double time)

void stories_next(void)
{
if(storiesCount == 0) {
return;
}

if(storyAutoplay) {
stories_rewind(10);
} else {
Expand All @@ -360,6 +372,10 @@ void stories_next(void)

void stories_previous(void)
{
if(storiesCount == 0) {
return;
}

if(storyAutoplay) {
stories_rewind(-10);
} else {
Expand All @@ -374,6 +390,10 @@ void stories_previous(void)

void stories_menu(void)
{
if(storiesCount == 0) {
return;
}

if(storyActionKey[0] == '\0') {
if(storiesDiplayMode == STORIES_DISPLAY_MODE_SINGLE) {
stories_setMode(STORIES_DISPLAY_MODE_TILES);
Expand All @@ -385,7 +405,7 @@ void stories_menu(void)

void stories_ok(void)
{
if(!storyOkAction) {
if(storiesCount == 0 || !storyOkAction) {
return;
}

Expand All @@ -406,6 +426,10 @@ void stories_autoplay(void)

void stories_pause(void)
{
if(storiesCount == 0) {
return;
}

if(Mix_PlayingMusic() == 1) {
if (Mix_PausedMusic() == 1) {
autosleep_lock();
Expand All @@ -423,6 +447,10 @@ void stories_pause(void)

bool stories_home(void)
{
if(storiesCount == 0) {
return true;
}

if(storyActionKey[0] == '\0') {
Mix_HookMusicFinished(NULL);
if(Mix_PlayingMusic() == 1) {
Expand Down

0 comments on commit 3b7aca9

Please sign in to comment.