Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // JPEG display and MP3 play from SD card to internal DAC
- // written by Larry Bank ([email protected])
- // Modified by Isolt
- //
- #include <Arduino.h>
- #include "AudioFileSourceSD.h"
- #include "AudioFileSourceID3.h"
- #include "AudioGeneratorMP3.h"
- #include "AudioOutputI2S.h"
- #include <bb_spi_lcd.h>
- #include <JPEGDEC.h>
- #include <SPI.h>
- #include <SD.h>
- // Static instances of the 2 classes
- // These can be allocated dynamically too (e.g. *jpg = new JPEGDEC(); )
- JPEGDEC jpg;
- BB_SPI_LCD lcd;
- SPIClass SD_SPI;
- AudioFileSourceSD *fileMP3;
- AudioGeneratorMP3 *mp3;
- AudioFileSourceSD *source;
- AudioOutputI2S *out;
- AudioFileSourceID3 *id3;
- // GPIO pin assignments specifically for the CYD 2USB model
- // check your model if different
- #define SD_MOSI 23
- #define SD_MISO 19
- #define SD_SCK 18
- #define SD_CS 5
- // Change this to the file you would like to display
- const char *jpeg_filename = "/10.jpg";
- char *mp3_filename = "/guitar.mp3";
- //
- // Draw callback from JPEG decoder
- //
- // called multiple times with groups of MCUs (minimum coded units)
- // these are 8x8, 8x16, 16x8 or 16x16 blocks of pixels depending on the
- // color subsampling option of the JPEG image
- // Each call can have a large group (e.g. 128x16 pixels)
- //
- int JPEGDraw(JPEGDRAW *pDraw) {
- lcd.setAddrWindow(pDraw->x, pDraw->y, pDraw->iWidth, pDraw->iHeight);
- lcd.pushPixels((uint16_t *)pDraw->pPixels, pDraw->iWidth * pDraw->iHeight);
- return 1;
- } /* JPEGDraw() */
- File file;
- // Callback functions to access a file on the SD card
- void *myOpen(const char *filename, int32_t *size) {
- file = SD.open(filename);
- if (file) {
- *size = file.size();
- }
- return (void *)&file;
- }
- void myClose(void *pHandle) {
- File *f = static_cast<File *>(pHandle);
- if (f != NULL)
- f->close();
- }
- int32_t myRead(JPEGFILE *pFile, uint8_t *buffer, int32_t length) {
- // File *f = (File *)pFile->fHandle;
- // return f->read(buffer, length);
- int32_t iBytesRead;
- iBytesRead = length;
- File *f = static_cast<File *>(pFile->fHandle);
- // Note: If you read a file all the way to the last byte, seek() stops working
- if ((pFile->iSize - pFile->iPos) < length)
- iBytesRead = pFile->iSize - pFile->iPos - 1; // <-- ugly work-around
- if (iBytesRead <= 0)
- return 0;
- iBytesRead = (int32_t)f->read(buffer, iBytesRead);
- pFile->iPos = f->position();
- return iBytesRead;
- }
- int32_t mySeek(JPEGFILE *pFile, int32_t iPosition) {
- // int i = micros();
- File *f = static_cast<File *>(pFile->fHandle);
- f->seek(iPosition);
- pFile->iPos = (int32_t)f->position();
- // i = micros() - i;
- // Serial.printf("Seek time = %d us\n", i);
- return pFile->iPos;
- } /* GIFSeekFile() */
- void playMP3(char *filenameMP3) {
- fileMP3 = new AudioFileSourceSD(filenameMP3);
- id3 = new AudioFileSourceID3(fileMP3);
- out = new AudioOutputI2S(0, 2, 8, -1); // Output to builtInDAC
- out->SetOutputModeMono(true);
- out->SetGain(0.2);
- mp3 = new AudioGeneratorMP3();
- mp3->begin(id3, out);
- while (mp3->isRunning()) {
- if (!mp3->loop()) mp3->stop();
- }
- }
- void setup() {
- Serial.begin(115200);
- lcd.begin(DISPLAY_CYD_2USB); // set this to the correct display type for your board
- lcd.fillScreen(TFT_BLACK);
- lcd.setTextColor(TFT_GREEN, TFT_BLACK);
- lcd.setFont(FONT_12x16);
- lcd.println("JPEG Decoder Example");
- SD_SPI.begin(SD_SCK, SD_MISO, SD_MOSI, SD_CS);
- if (!SD.begin(SD_CS, SD_SPI, 10000000)) { // Faster than 10MHz seems to fail on the CYDs
- lcd.println("Card Mount Failed");
- while (1) {};
- } else {
- lcd.println("Card Mount Succeeded");
- }
- }
- void loop() {
- if (jpg.open(jpeg_filename, myOpen, myClose, myRead, mySeek, JPEGDraw)) { // pass the data and its sizeENDIAN);
- jpg.setPixelType(RGB565_BIG_ENDIAN); // bb_spi_lcd uses big-endian RGB565 pixels
- jpg.decode(0, 0, 0); // render image top left
- }
- delay(2000);
- playMP3(mp3_filename);
- delay(2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement