|
| 1 | +#include "GroveMP3V3.h" |
| 2 | + |
| 3 | +static constexpr uint8_t CMD_ROOT_INDEX_PLAY_SD = 0xa2; |
| 4 | +static constexpr uint8_t CMD_STOP = 0xab; |
| 5 | +static constexpr uint8_t CMD_VOLUME_CONTROL = 0xae; |
| 6 | +static constexpr uint8_t CMD_QUERY_CURRENT_VOLUME_SETTING = 0xc1; |
| 7 | +static constexpr uint8_t CMD_QUERY_CURRENT_OPERATION_STATE = 0xc2; |
| 8 | +static constexpr uint8_t CMD_QUERY_TOTAL_NUMBER_OF_MUSIC_SD = 0xc5; |
| 9 | + |
| 10 | +void GroveMP3V3::WriteCommand(uint8_t commandCode, const uint8_t* parameter, int parameterSize) |
| 11 | +{ |
| 12 | + uint8_t length = 1 + 1 + parameterSize + 1; |
| 13 | + uint8_t sum = 0; |
| 14 | + sum += length; |
| 15 | + sum += commandCode; |
| 16 | + for (int i = 0; i < parameterSize; ++i) sum += parameter[i]; |
| 17 | + |
| 18 | + _UART->Write(0x7e); |
| 19 | + _UART->Write(length); |
| 20 | + _UART->Write(commandCode); |
| 21 | + for (int i = 0; i < parameterSize; ++i) _UART->Write(parameter[i]); |
| 22 | + _UART->Write(sum); |
| 23 | + _UART->Write(0xef); |
| 24 | +} |
| 25 | + |
| 26 | +bool GroveMP3V3::ReadReturn(uint8_t* operationCode, uint8_t* returnValue, uint8_t returnValueSize, int timeout) |
| 27 | +{ |
| 28 | + |
| 29 | + int elapsed = 0; |
| 30 | + while (_UART->ReadAvailable() < 1) |
| 31 | + { |
| 32 | + if (elapsed * 10 >= timeout) return false; |
| 33 | + HalSystem::DelayMs(10); |
| 34 | + ++elapsed; |
| 35 | + } |
| 36 | + *operationCode = _UART->Read(); |
| 37 | + |
| 38 | + for (int i = 0; i < returnValueSize; ++i) |
| 39 | + { |
| 40 | + elapsed = 0; |
| 41 | + while (_UART->ReadAvailable() < 1) |
| 42 | + { |
| 43 | + if (elapsed * 10 >= timeout) return false; |
| 44 | + HalSystem::DelayMs(10); |
| 45 | + ++elapsed; |
| 46 | + } |
| 47 | + returnValue[i] = _UART->Read(); |
| 48 | + } |
| 49 | + |
| 50 | + return true; |
| 51 | +} |
| 52 | + |
| 53 | +bool GroveMP3V3::Init() |
| 54 | +{ |
| 55 | + int retry = 0; |
| 56 | + while (true) |
| 57 | + { |
| 58 | + GroveMP3V3::WriteCommand(CMD_QUERY_TOTAL_NUMBER_OF_MUSIC_SD, nullptr, 0); |
| 59 | + |
| 60 | + uint8_t operationCode; |
| 61 | + uint8_t totalNumberOfFiles[2]; |
| 62 | + if (GroveMP3V3::ReadReturn(&operationCode, totalNumberOfFiles, sizeof(totalNumberOfFiles), 100)) // timeout = 100[msec.] |
| 63 | + { |
| 64 | + if (operationCode == CMD_QUERY_TOTAL_NUMBER_OF_MUSIC_SD) break; |
| 65 | + } |
| 66 | + |
| 67 | + if (retry >= 100) return false; // retry * timeout = 100 * 100 = 10[sec.] |
| 68 | + ++retry; |
| 69 | + } |
| 70 | + |
| 71 | + _IsExist = true; |
| 72 | + return true; |
| 73 | +} |
| 74 | + |
| 75 | +void GroveMP3V3::Play(int index) |
| 76 | +{ |
| 77 | + if (!_IsExist) return; |
| 78 | + |
| 79 | + ++index; |
| 80 | + |
| 81 | + const uint8_t parameter[]{ static_cast<uint8_t>(index / 256), static_cast<uint8_t>(index % 256) }; |
| 82 | + GroveMP3V3::WriteCommand(CMD_ROOT_INDEX_PLAY_SD, parameter, sizeof(parameter)); |
| 83 | + |
| 84 | + uint8_t operationCode; |
| 85 | + if (!GroveMP3V3::ReadReturn(&operationCode, nullptr, 0)) HalSystem::Abort(); |
| 86 | + if (operationCode != 0x00) HalSystem::Abort(); |
| 87 | +} |
| 88 | + |
| 89 | +void GroveMP3V3::Stop() |
| 90 | +{ |
| 91 | + if (!_IsExist) return; |
| 92 | + |
| 93 | + GroveMP3V3::WriteCommand(CMD_STOP, nullptr, 0); |
| 94 | + |
| 95 | + uint8_t operationCode; |
| 96 | + if (!GroveMP3V3::ReadReturn(&operationCode, nullptr, 0)) HalSystem::Abort(); |
| 97 | + if (operationCode != 0x00) HalSystem::Abort(); |
| 98 | +} |
| 99 | + |
| 100 | +void GroveMP3V3::SetVolume(int volume) |
| 101 | +{ |
| 102 | + if (!_IsExist) return; |
| 103 | + |
| 104 | + if (volume < 0) volume = 0; |
| 105 | + if (31 < volume) volume = 31; |
| 106 | + |
| 107 | + const uint8_t parameter[]{ static_cast<uint8_t>(volume) }; |
| 108 | + GroveMP3V3::WriteCommand(CMD_VOLUME_CONTROL, parameter, sizeof(parameter)); |
| 109 | + |
| 110 | + uint8_t operationCode; |
| 111 | + if (!GroveMP3V3::ReadReturn(&operationCode, nullptr, 0)) HalSystem::Abort(); |
| 112 | + if (operationCode != 0x00) HalSystem::Abort(); |
| 113 | +} |
| 114 | + |
| 115 | +int GroveMP3V3::QueryVolume() |
| 116 | +{ |
| 117 | + if (!_IsExist) return 0; |
| 118 | + |
| 119 | + GroveMP3V3::WriteCommand(CMD_QUERY_CURRENT_VOLUME_SETTING, nullptr, 0); |
| 120 | + |
| 121 | + uint8_t operationCode; |
| 122 | + uint8_t currentVolume; |
| 123 | + if (!GroveMP3V3::ReadReturn(&operationCode, ¤tVolume, 1)) HalSystem::Abort(); |
| 124 | + if (operationCode != CMD_QUERY_CURRENT_VOLUME_SETTING) HalSystem::Abort(); |
| 125 | + |
| 126 | + return currentVolume; |
| 127 | +} |
| 128 | + |
| 129 | +GroveMP3V3::STATUS GroveMP3V3::QueryStatus() |
| 130 | +{ |
| 131 | + if (!_IsExist) return STATUS_STOP; |
| 132 | + |
| 133 | + GroveMP3V3::WriteCommand(CMD_QUERY_CURRENT_OPERATION_STATE, nullptr, 0); |
| 134 | + |
| 135 | + uint8_t operationCode; |
| 136 | + uint8_t currentOperationState; |
| 137 | + if (!GroveMP3V3::ReadReturn(&operationCode, ¤tOperationState, 1)) HalSystem::Abort(); |
| 138 | + if (operationCode != CMD_QUERY_CURRENT_OPERATION_STATE) HalSystem::Abort(); |
| 139 | + |
| 140 | + switch (currentOperationState) |
| 141 | + { |
| 142 | + case 1: |
| 143 | + return STATUS_PLAY; |
| 144 | + case 2: |
| 145 | + return STATUS_STOP; |
| 146 | + case 3: |
| 147 | + return STATUS_PAUSE; |
| 148 | + default: |
| 149 | + HalSystem::Abort(); |
| 150 | + } |
| 151 | + |
| 152 | + return STATUS_STOP; // Dummy |
| 153 | +} |
| 154 | + |
| 155 | +int GroveMP3V3::QueryNumberOfMusic() |
| 156 | +{ |
| 157 | + if (!_IsExist) return 0; |
| 158 | + |
| 159 | + GroveMP3V3::WriteCommand(CMD_QUERY_TOTAL_NUMBER_OF_MUSIC_SD, nullptr, 0); |
| 160 | + |
| 161 | + uint8_t operationCode; |
| 162 | + uint8_t totalNumberOfFiles[2]; |
| 163 | + if (!GroveMP3V3::ReadReturn(&operationCode, totalNumberOfFiles, sizeof(totalNumberOfFiles))) HalSystem::Abort(); |
| 164 | + if (operationCode != CMD_QUERY_TOTAL_NUMBER_OF_MUSIC_SD) HalSystem::Abort(); |
| 165 | + |
| 166 | + return totalNumberOfFiles[0] * 256 + totalNumberOfFiles[1]; |
| 167 | +} |
0 commit comments