Guest User

Untitled

a guest
Feb 19th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. /*
  2. * Copyright 2017 <Admobilize>
  3. * All rights reserved.
  4. */
  5.  
  6. #include <gflags/gflags.h>
  7. #include <wiringPi.h>
  8.  
  9. #include <math.h>
  10. #include <fstream>
  11. #include <iostream>
  12. #include <string>
  13. #include <thread>
  14. #include <valarray>
  15.  
  16. #include "../cpp/driver/everloop.h"
  17. #include "../cpp/driver/everloop_image.h"
  18. #include "../cpp/driver/microphone_array.h"
  19. #include "../cpp/driver/wishbone_bus.h"
  20.  
  21.  
  22. DEFINE_int32(sampling_frequency, 44100, "Sampling Frequency");
  23. DEFINE_int32(duration, 1, "Interrupt after N seconds");
  24.  
  25. namespace hal = matrix_hal;
  26.  
  27. int main(int argc, char *agrv[]) {
  28. google::ParseCommandLineFlags(&argc, &agrv, true);
  29.  
  30. hal::WishboneBus bus;
  31. bus.SpiInit();
  32.  
  33. int sampling_rate = FLAGS_sampling_frequency;
  34. int seconds_to_record = FLAGS_duration;
  35. std::cout << "Duration : " << seconds_to_record << "s" << std::endl;
  36.  
  37. std::thread et(
  38. [seconds_to_record, sampling_rate](hal::WishboneBus *bus) {
  39.  
  40. std::ifstream is("/home/pi/song.raw");
  41.  
  42. int delta, t;
  43. int l = 1024;
  44. uint16_t test[l];
  45.  
  46. float fs = float(sampling_rate);
  47. float ts = 1.0 / fs;
  48. float f = 1000;
  49. float w = 2 * M_PI * f;
  50. float A = (pow(2, 16)) - 1;
  51. t=0;
  52.  
  53.  
  54. while (true) {
  55. delta = (t) * l;
  56. // for (int i = 0; i < l; i++) {
  57. // test[i] = uint16_t(A/2 + A*sin(w*(delta+i)*ts)/128);
  58. // }
  59. is.read((char *)test,l*2);
  60. uint16_t write_pointer;
  61. uint16_t read_pointer;
  62. uint16_t diff;
  63. bus->SpiRead16(0x6802,(unsigned char*)&read_pointer);
  64. bus->SpiRead16(0x6803,(unsigned char*)&write_pointer);
  65.  
  66. if(write_pointer > read_pointer)
  67. diff = write_pointer - read_pointer;
  68. else
  69. diff = 4096 - read_pointer + write_pointer;
  70.  
  71. if(diff > 3072){
  72. int sleep = int(l*ts*1000000);
  73. std::this_thread::sleep_for(std::chrono::microseconds(sleep));
  74. }
  75. bus->SpiWriteBurst(0x6000,
  76. reinterpret_cast<unsigned char *>(&test[0]),
  77. sizeof(uint16_t) * l);
  78.  
  79. t++;
  80. }
  81. },
  82. &bus);
  83.  
  84. et.join();
  85.  
  86. return 0;
  87. }
Add Comment
Please, Sign In to add comment