Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.16 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <ports.h>
  3. #include <base.h>
  4. #include <DirectIO.h>
  5.  
  6. #include <Adafruit_NeoPixel.h>
  7. #include <TimerOne.h>
  8.  
  9. #define CLKPIN TIMER1_A_PIN
  10. #define TRGPIN 4
  11. #define DEBUGPIN 6
  12. #define FIRSTFIELD 7
  13.  
  14. #ifndef cbi
  15. #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  16. #endif
  17. #ifndef sbi
  18. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  19. #endif
  20.  
  21. Output<TRGPIN> trg;
  22. Output<CLKPIN> clk;
  23. Output<6> dbg;
  24. Output<FIRSTFIELD> ff;
  25.  
  26. #define NUMPIXELS 8
  27. #define NEO_PIN 8
  28. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, NEO_PIN, NEO_RGB + NEO_KHZ800);
  29.  
  30. const uint8_t PROGMEM gamma8[] = {
  31. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
  33. 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
  34. 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
  35. 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
  36. 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
  37. 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
  38. 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
  39. 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
  40. 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
  41. 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
  42. 90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
  43. 115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
  44. 144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
  45. 177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
  46. 215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };
  47.  
  48. void setup() {
  49. analogReference(EXTERNAL);
  50. pinMode(CLKPIN, OUTPUT);
  51. //pinMode(DEBUGPIN, OUTPUT);
  52.  
  53. Timer1.initialize(1); // ~1mhz
  54. Timer1.pwm(CLKPIN, 512); // 50% duty cycle
  55. Timer1.stop();
  56. pinMode(TRGPIN, OUTPUT);
  57. pinMode(A0, INPUT);
  58. pinMode(ledToPin(0), OUTPUT);
  59. pinMode(ledToPin(1), OUTPUT);
  60. pinMode(ledToPin(2), OUTPUT);
  61. Serial.begin(115200);
  62. cbi(ADCSRA, ADPS0);
  63. cbi(ADCSRA, ADPS1);
  64. cbi(ADCSRA, ADPS2);
  65. sbi(ADCSRA, ADPS1);
  66.  
  67. // Serial.print("prescaler = ");
  68. // Serial.println(ADCSRA & 0xE0);
  69. pixels.begin();
  70. }
  71.  
  72. int avgRead(int port, int samples)
  73. {
  74. int i;
  75. long reading = 0;
  76. for(i = 0; i < samples; i++)
  77. {
  78. reading += analogRead(port);
  79. }
  80.  
  81. reading /= samples;
  82.  
  83. return reading;
  84. }
  85.  
  86. void clockCycle(int pin, int count)
  87. {
  88. while(count--)
  89. {
  90. digitalWrite(pin, HIGH);
  91. digitalWrite(pin, LOW);
  92. }
  93. }
  94.  
  95. int ledToPin(int color)
  96. {
  97. switch(color)
  98. {
  99. case 0: // red
  100. return 2;
  101. break;
  102.  
  103. case 1: // green
  104. return 3;
  105. break;
  106.  
  107. case 2: // blue
  108. return 5;
  109. break;
  110. }
  111. }
  112. void ledOn(int color)
  113. {
  114. digitalWrite(ledToPin(color), LOW);
  115. }
  116.  
  117. void ledOff(int color)
  118. {
  119. digitalWrite(ledToPin(color), HIGH);
  120. }
  121.  
  122. void ledsOn()
  123. {
  124. for(int i = 0; i <= 2; i++)
  125. ledOn(i);
  126. }
  127.  
  128. void ledsOff()
  129. {
  130. for(int i = 0; i <= 2; i++)
  131. ledOff(i);
  132. }
  133.  
  134. // returns n samples from start of readback to end of readback
  135. void readLine(byte* output, int samples)
  136. {
  137. int zeroLevel;
  138. int blackLevel;
  139. int clocks;
  140. int i;
  141.  
  142. int samplePos = 2593 / samples;
  143. int currSample = 0;
  144.  
  145. shiftOut(TRGPIN, CLKPIN, MSBFIRST, 0xF0); // trigger pulse and slow clock
  146.  
  147. // lock step through front porch with slow clock in order to capture reference levels without unpredictable jitter
  148. clockCycle(CLKPIN, 4);
  149.  
  150. // sample black level
  151. for(i=0; i < 3; i++)
  152. {
  153. clocks=5; while(clocks--)
  154. {
  155. clk=1;
  156. clk=0;
  157. }
  158. blackLevel += avgRead(A0, 3);
  159. }
  160.  
  161. blackLevel /= 3;
  162. // Serial.print("blackLevel = ");
  163. // Serial.println(blackLevel);
  164.  
  165. // 65 more clock cycles until analog goodies
  166. clocks = 65; while(clocks--)
  167. {
  168. clk=1;
  169. clk=0;
  170. }
  171.  
  172. #define ANALOG_CLOCKS (12 * 216 + 1)
  173.  
  174. int stepSpacing = ANALOG_CLOCKS / samples;
  175. int nextSample = 0;
  176.  
  177. // go through analog bins
  178. for(i = 0; i < 12 * 216 + 1; i++)
  179. {
  180. clk = 1;
  181.  
  182. if(nextSample-- == 0)
  183. {
  184. dbg=1;
  185. nextSample = stepSpacing;
  186. // output[currSample++] = map(avgRead(A0, 5), blackLevel, 1024, 0, 255);
  187. output[currSample++] = map(analogRead(A0), blackLevel, 1024, 0, 255);
  188. dbg=0;
  189. }
  190. clk = 0;
  191. }
  192.  
  193. #if 0
  194. for(i = 0; i < 2593; i++)
  195. {
  196. clk =1;
  197. clk =0;
  198. }
  199. #endif
  200.  
  201. }
  202.  
  203. void loop() {
  204. int led = 0;
  205. byte samples[64];
  206. byte redSamples[NUMPIXELS];
  207. byte greenSamples[NUMPIXELS];
  208. byte blueSamples[NUMPIXELS];
  209. int i;
  210.  
  211. ff=1;
  212. ff=0;
  213.  
  214. #if 1
  215. ledOn(0);
  216. readLine(redSamples, NUMPIXELS);
  217. ledsOff();
  218.  
  219. ledOn(1);
  220. readLine(greenSamples, NUMPIXELS);
  221. ledsOff();
  222.  
  223. ledOn(2);
  224. readLine(blueSamples, NUMPIXELS);
  225. ledsOff();
  226. #endif
  227. #if 0
  228. for(led = 0; led < 0; led++){
  229. ledOn(led);
  230. readLine(samples, 4);
  231. ledOff(led);
  232. }
  233. ledsOff();
  234.  
  235. #endif
  236.  
  237. for(i = 0; i < NUMPIXELS; i++)
  238. {
  239. #if 0
  240. Serial.print("sample[");
  241. Serial.print(i);
  242. Serial.println("] = ");
  243. Serial.print("red ");
  244. Serial.println(redSamples[i]);
  245. Serial.print("green ");
  246. Serial.println(greenSamples[i]);
  247. Serial.print("blue ");
  248. Serial.println(blueSamples[i]);
  249. #endif
  250.  
  251.  
  252. pixels.setPixelColor(i, pixels.Color(pgm_read_byte(&gamma8[greenSamples[i]]), pgm_read_byte(&gamma8[redSamples[i]]), pgm_read_byte(&gamma8[blueSamples[i]])));
  253.  
  254. // pixels.setPixelColor(i, pixels.Color(0, 0, 0));
  255. // pixels.setPixelColor(i, pixels.Color(redSamples[i], greenSamples[i], blueSamples[i]));
  256. // pixels.setPixelColor(i, pixels.Color(pgm_read_byte(&gamma8[redSamples[i]]), pgm_read_byte(&gamma8[greenSamples[i]]), pgm_read_byte(&gamma8[blueSamples[i]])));
  257. // pixels.setPixelColor(i, pixels.Color(pgm_read_byte(&gamma8[redSamples[i]]), pgm_read_byte(&gamma8[redSamples[i]]), pgm_read_byte(&gamma8[redSamples[i]])));
  258.  
  259. pixels.show(); // This sends the updated pixel color to the hardware.
  260.  
  261.  
  262. }
  263. // delay(100);
  264. //delayMicroseconds(100);
  265. }
  266.  
  267. void old_loop() {
  268. int bin;
  269. int i;
  270. int samples[12];
  271. int clocks;
  272. int zeroLevel;
  273. int blackLevel;
  274. static int led=0;
  275. //shiftOut(TRGPIN, CLKPIN, MSBFIRST, 0);
  276. dbg=1;
  277. zeroLevel = avgRead(A0, 5);
  278. // zeroLevel = analogRead(A0);
  279. dbg=0;
  280.  
  281. dbg=1;
  282. shiftOut(TRGPIN, CLKPIN, MSBFIRST, 0xF0); // trigger pulse and slow clock
  283. dbg=0;
  284.  
  285. // lock step through front porch with slow clock in order to capture reference levels without unpredictable jitter
  286. dbg=1;
  287. clockCycle(CLKPIN, 4);
  288. dbg=0;
  289.  
  290. dbg=1;
  291. for(i=0; i < 3; i++)
  292. {
  293. clocks=5; while(clocks--)
  294. {
  295. clk=1;
  296. clk=0;
  297. }
  298. blackLevel += avgRead(A0, 3);
  299. }
  300.  
  301. blackLevel /= 4;
  302. // Serial.print("blackLevel = ");
  303. // Serial.println(blackLevel);
  304. dbg=0;
  305.  
  306. if(led == 0){
  307. ff = 1;
  308. ff = 0;
  309. }
  310. ledOn(led);
  311. //ledsOn();
  312. dbg=1;
  313. clocks = 40; while(clocks--)
  314. {
  315. clk=1;
  316. clk=0;
  317. }
  318. dbg=0;
  319.  
  320. for(i = 0; i < 350*8; i++)
  321. {
  322. clk =1;
  323. clk =0;
  324. }
  325. dbg =1;
  326.  
  327. dbg=0;
  328. // Timer1.pwm(CLKPIN, 512); // fast clock
  329. /* dbg=1;
  330. for(i = 0; i < 300; i++)
  331. {
  332. samples[i] = analogRead(A0);
  333. //delayMicroseconds(2000);
  334. }
  335. dbg=0;
  336. */
  337. // sample front porch reference level 2 clocks later, for 80 hs clocks
  338. // sample bins 83 clocks after fast start
  339. // bins seem to be 215 clocks wide, 12 bins, 1 clock per pixel
  340.  
  341. //delayMicroseconds(1);
  342. #if 0
  343. dbg=1;
  344. noInterrupts();
  345. while(analogRead(A0) -5 < zeroLevel)
  346. ;
  347. interrupts();
  348. // wait for voltage to increase to follow clock jitter
  349. dbg=0;
  350. #endif
  351.  
  352.  
  353. dbg = 1;
  354. blackLevel = avgRead(A0, 5);
  355. // blackLevel = analogRead(A0);
  356. dbg = 0;
  357.  
  358. #if 0
  359. noInterrupts();
  360. while(analogRead(A0) -10< blackLevel)
  361. {
  362. dbg=1;
  363. delayMicroseconds(0);
  364. dbg=0;
  365. }
  366. ;
  367. interrupts();
  368.  
  369. #endif
  370.  
  371. dbg = 1;
  372. delayMicroseconds(1);
  373. dbg = 0;
  374. /*
  375. delayMicroseconds(100);
  376. dbg=1;
  377. bin= analogRead(A0);
  378. dbg=0;
  379. */
  380.  
  381. // delayMicroseconds(2670);
  382. Timer1.stop();
  383. digitalWrite(CLKPIN, LOW);
  384. /*
  385. Serial.print("zeroLevel = ");
  386. Serial.println(zeroLevel);
  387.  
  388. Serial.print("blackLevel = ");
  389. Serial.println(blackLevel);
  390. /* Serial.print("bin = ");
  391. Serial.println(bin);
  392. */
  393.  
  394. //delay(50);
  395. #if 1
  396. ledOff(led);
  397. // switch LED
  398. if(++led > 2)
  399. led = 0;
  400. #endif
  401. //ledsOff();
  402.  
  403. // Serial.println(led);
  404. // Serial.print("normalized = ");
  405. // Serial.println(map(bin - blackLevel, zeroLevel, 1024, 0, 255));
  406. //delay(10); // inter line delay
  407. delayMicroseconds(100);
  408. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement