Advertisement
Guest User

Untitled

a guest
Feb 21st, 2021
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.15 KB | None | 0 0
  1. #include <Audio.h>
  2. #include <Wire.h>
  3. #include <SPI.h>
  4. #include <SD.h>
  5. #include <SerialFlash.h>
  6. #include <play_sd_mp3.h> // https://github.com/FrankBoesing/Arduino-Teensy-Codec-lib
  7. //#include <play_sd_aac.h>
  8. #include <FastLED.h>
  9. #include "easing.h"
  10. #include <NXPMotionSense.h>
  11. #include <EEPROM.h>
  12.  
  13. //AudioPlaySdWav           playSdWav1;     //xy=154,422
  14. AudioPlaySdMp3           playMp31;
  15. AudioMixer4              mixer1;
  16. AudioOutputAnalog        dac1;
  17. AudioConnection          patchCord1(playMp31, 0, mixer1, 0);
  18. AudioConnection          patchCord2(playMp31, 1, mixer1, 1);
  19. AudioConnection          patchCord3(mixer1, dac1);
  20.  
  21. NXPMotionSense imu;
  22. NXPSensorFusion filter;
  23.  
  24. #define LEFT_RING_NEO_PIN 2
  25. #define RIGHT_RING_NEO_PIN 3
  26. #define N_LEDS 16
  27.  
  28. CRGB ringL[N_LEDS];
  29. CRGB ringR[N_LEDS];
  30.  
  31. #define PROP_AMP_ENABLE    5
  32. #define FLASH_CHIP_SELECT  6
  33. //#define FLASH_CHIP_SELECT 21  // Arduino 101 built-in SPI Flash
  34.  
  35. uint8_t max_bright = 128;                                      // Overall brightness definition.
  36.  
  37. void setup() {
  38.   Serial.begin(115200);
  39.   AudioMemory(8); //4
  40.   delay(2000);
  41.   imu.begin();
  42.   filter.begin(100);
  43.  
  44.   FastLED.addLeds<NEOPIXEL, LEFT_RING_NEO_PIN>(ringL, N_LEDS);
  45.   FastLED.addLeds<NEOPIXEL, RIGHT_RING_NEO_PIN>(ringR, N_LEDS);
  46.   FastLED.setBrightness(max_bright);
  47.  
  48.   // Start SerialFlash
  49.   if (!SerialFlash.begin(FLASH_CHIP_SELECT)) {
  50.     while (1) {
  51.       Serial.println ("Cannot access SPI Flash chip");
  52.       delay (1000);
  53.     }
  54.   }
  55.  
  56.   listFiles();
  57.  
  58.   //Set Volume
  59.   mixer1.gain(0, 2);
  60.   mixer1.gain(1, 2);
  61.  
  62.   //Start Amplifier
  63.   pinMode(PROP_AMP_ENABLE, OUTPUT);
  64.   digitalWrite(PROP_AMP_ENABLE, HIGH);    // Enable Amplifier
  65.  
  66.   fill_solid(ringL, N_LEDS, CRGB::Green);
  67.   fill_solid(ringR, N_LEDS, CRGB::Green);
  68.   FastLED.show();
  69.  
  70.   //Startup sound
  71.   playFile("WELC.MP3");
  72. }
  73.  
  74. void listFiles(){
  75.   Serial.println("All Files on SPI Flash chip:");
  76.  
  77.   if (!SerialFlash.begin(FLASH_CHIP_SELECT)) {
  78.     error("Unable to access SPI Flash chip");
  79.   }
  80.  
  81.   SerialFlash.opendir();
  82.   while (1) {
  83.     char filename[64];
  84.     uint32_t filesize;
  85.  
  86.     if (SerialFlash.readdir(filename, sizeof(filename), filesize)) {
  87.       Serial.print("  ");
  88.       Serial.print(filename);
  89.       spaces(20 - strlen(filename));
  90.       Serial.print("  ");
  91.       Serial.print(filesize);
  92.       Serial.print(" bytes");
  93.       Serial.println();
  94.     } else {
  95.       break; // no more files
  96.     }
  97.   }
  98. }
  99.  
  100. void spaces(int num) {
  101.   for (int i=0; i < num; i++) {
  102.     Serial.print(" ");
  103.   }
  104. }
  105.  
  106. void error(const char *message) {
  107.   while (1) {
  108.     Serial.println(message);
  109.     delay(2500);
  110.   }
  111. }
  112.  
  113. void playFile(const char *filename)
  114. {
  115.   SerialFlashFile ff = SerialFlash.open(filename);
  116.   Serial.print("Playing file: ");
  117.   Serial.println(filename);
  118.  
  119.   uint32_t sz = ff.size();
  120.   uint32_t pos = ff.getFlashAddress();
  121.  
  122.   // Start playing the file.  This sketch continues to
  123.   // run while the file plays.
  124.   playMp31.play(pos,sz);
  125.  
  126.   // Simply wait for the file to finish playing.
  127.   //while (playMp31.isPlaying()) {
  128.    //yield();
  129.   //}
  130. }
  131.  
  132.  
  133. void loop() {
  134.   if(!playMp31.isPlaying()){
  135.     playFile("R301.MP3");
  136.   }
  137.   readMotionSensor();
  138. }
  139.  
  140. void readMotionSensor(){
  141.   float ax, ay, az;
  142.   float gx, gy, gz;
  143.   float mx, my, mz;
  144.   float roll, pitch, heading;
  145.  
  146.   if (imu.available()) {
  147.     // Read the motion sensors
  148.     imu.readMotionSensor(ax, ay, az, gx, gy, gz, mx, my, mz);
  149.  
  150.     // Update the SensorFusion filter
  151.     filter.update(gx, gy, gz, ax, ay, az, mx, my, mz);
  152.  
  153.     //print the data
  154.     roll = filter.getRoll();
  155.     pitch = filter.getPitch();
  156.     heading = filter.getYaw();
  157.     Serial.print("Orientation: ");
  158.     Serial.print(heading);
  159.     Serial.print(" ");
  160.     Serial.print(pitch);
  161.     Serial.print(" ");
  162.     Serial.println(roll);
  163.     Serial.print("Accel X: ");
  164.     Serial.println(ax);
  165.     Serial.print("Gyro X: ");
  166.     Serial.println(gx);
  167.     Serial.print("Accel Y: ");
  168.     Serial.println(ay);
  169.     Serial.print("Gyro Y: ");
  170.     Serial.println(gy);
  171.     Serial.print("Accel Z: ");
  172.     Serial.println(az);
  173.     Serial.print("Gyro Z: ");
  174.     Serial.println(gz);
  175.     Serial.println("=============");
  176.   }
  177. }
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement