PCB - Enterprise NCC-1701 refit

enterprise.ino 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. // where does our characterMap start in the ASCII code
  2. #include <avr/pgmspace.h>
  3. #include <util/delay.h>
  4. #include <avr/sleep.h>
  5. #include <EEPROM.h>
  6. #include <avr/wdt.h>
  7. #include <avr/power.h> //Needed for powering down perihperals such as the ADC/TWI and Timers
  8. // How many modes do we want to go through?
  9. #define MAX_MODE 9
  10. #ifndef cbi
  11. #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  12. #endif
  13. #ifndef sbi
  14. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  15. #endif
  16. byte __attribute__ ((section (".noinit"))) last_mode;
  17. int bx=0;
  18. int by=0;
  19. int dx=1;
  20. int dy=1;
  21. //Pin Definitions
  22. #define DISPLAY_WIDTH 4
  23. #define DISPLAY_HEIGHT 5
  24. #define PIN_NUMBER 5
  25. const byte pins[PIN_NUMBER] = {0,1,2,3,4}; //the number of the pin used for the LEDs in ordered
  26. const byte connectionMatrix[20][2] = { //the matrix that shows the LEDs pin connections. First Value is the Anode, second is the Catode
  27. {4, 3},{3, 0},{3, 1},{3, 2},{3, 4},{4, 0},{4,1},{4, 2}, //gondola 0-7
  28. {1, 2},{2, 4},{2, 3},{2, 1},{2, 0},{1, 0}, //Tanier 8,9,10,11,12,13
  29. {0, 4}, //engine 14
  30. {0, 3},{0, 2},{0, 1}, // deflektor 15,16,17
  31. {1, 3}, //mostik 18
  32. {1, 4}, //laser 19
  33. };
  34. #define g0 0
  35. #define g1 1
  36. #define g2 2
  37. #define g3 3
  38. #define g4 4
  39. #define g5 5
  40. #define g6 6
  41. #define g7 7
  42. #define p1 8
  43. #define p2 9
  44. #define p3 10
  45. #define p4 11
  46. #define p5 12
  47. #define p6 13
  48. #define eng 14
  49. #define d1 15
  50. #define d2 16
  51. #define d3 17
  52. #define br 18
  53. #define laser 19
  54. #define xx 33
  55. #include "font.h"
  56. int scdelay=0, textx=0, textindex=0;
  57. byte videoram[21]={
  58. 1,1,1,1,1,1,1,1,
  59. 1,1,1,1,1,1,
  60. 1,
  61. 1,1,1,
  62. 1,
  63. 1,
  64. 0};
  65. // runs at start
  66. void setup() {
  67. // set up render map
  68. if(bit_is_set(MCUSR, PORF)) { // Power was just established!
  69. MCUSR = 0; // clear MCUSR
  70. EEReadSettings(); // read the last mode out of eeprom
  71. }
  72. else if(bit_is_set(MCUSR, EXTRF)) { // we're coming out of a reset condition.
  73. MCUSR = 0; // clear MCUSR
  74. last_mode++; // advance mode
  75. if(last_mode > MAX_MODE) {
  76. last_mode = 0; // reset mode
  77. }
  78. }
  79. // Try and set the random seed more randomly. Alternate solutions involve
  80. // using the eeprom and writing the last seed there.
  81. uint16_t seed=0;
  82. uint8_t count=32;
  83. while (--count) {
  84. seed = (seed<<1) | (analogRead(1)&1);
  85. }
  86. randomSeed(seed);
  87. MCUCR |= (1<<PUD);
  88. //beep ();
  89. }
  90. // loops continuously
  91. void loop() {
  92. MCUCR |= (1<<PUD);
  93. // If EXTRF hasn't been asserted yet, save the mode
  94. EESaveSettings();
  95. switch(last_mode) {
  96. // red modes
  97. case 0:
  98. last_mode=0; //off mode
  99. SleepNow();
  100. break;
  101. case 1:
  102. testledon();
  103. break;
  104. case 2:
  105. start1(5);
  106. break;
  107. case 3:
  108. start1(1);
  109. break;
  110. case 4:
  111. testled();
  112. break;
  113. case 5:
  114. bliklp(1);
  115. break;
  116. case 6:
  117. bliklp2(2);
  118. break;
  119. case 7:
  120. bliklp(3);
  121. break;
  122. case 8:
  123. bliklp2(4);
  124. break;
  125. case 9:
  126. bliklp(5);
  127. break;
  128. default:
  129. last_mode=0; //off mode
  130. break;
  131. }
  132. }
  133. void bliklp (byte spd) ///low power blik
  134. {
  135. while (1)
  136. {
  137. for (byte i = 0; i < 21; i++) {
  138. pinMode(pins[connectionMatrix[i][0]], OUTPUT); //set positive pole to OUTPUT
  139. pinMode(pins[connectionMatrix[i][1]], OUTPUT); //set negative pole to OUTPUT
  140. digitalWrite(pins[connectionMatrix[i][0]], HIGH); //set positive pole to HIGH
  141. digitalWrite(pins[connectionMatrix[i][1]], LOW); //set negative pole to LOW
  142. setup_watchdog(1); // approximately 2 seconds sleep
  143. system_sleep(); // 0=16ms,
  144. pinMode(pins[connectionMatrix[i][0]], INPUT); //set both positive pole and negative pole
  145. pinMode(pins[connectionMatrix[i][1]], INPUT); // to INPUT in order to turn OFF the LED
  146. setup_watchdog(spd); // sleep
  147. // 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec // wait for a second
  148. system_sleep();
  149. }
  150. }
  151. }
  152. void bliklp2 (byte spd) ///low power blik
  153. {
  154. while (1)
  155. {
  156. for (byte i = 0; i < 21; i++) {
  157. pinMode(pins[connectionMatrix[i][0]], OUTPUT); //set positive pole to OUTPUT
  158. pinMode(pins[connectionMatrix[i][1]], OUTPUT); //set negative pole to OUTPUT
  159. digitalWrite(pins[connectionMatrix[i][0]], HIGH); //set positive pole to HIGH
  160. digitalWrite(pins[connectionMatrix[i][1]], LOW); //set negative pole to LOW
  161. setup_watchdog(spd); // approximately 2 seconds sleep
  162. system_sleep(); // 0=16ms,
  163. pinMode(pins[connectionMatrix[i][0]], INPUT); //set both positive pole and negative pole
  164. pinMode(pins[connectionMatrix[i][1]], INPUT); // to INPUT in order to turn OFF the LED
  165. // setup_watchdog(spd); // sleep
  166. // // 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec // wait for a second
  167. // system_sleep();
  168. }
  169. }
  170. }
  171. void start1 (char speed)
  172. {
  173. int ledindex=0;
  174. byte ledx =0;
  175. byte ledbri =0;
  176. int ledtime =0;
  177. clearMatrix();
  178. while (1) {
  179. ledx = (pgm_read_byte(&led_data[ledindex]));
  180. if (ledx==xx) {
  181. ledindex=0;
  182. ledx = (pgm_read_byte(&led_data[ledindex]));
  183. }
  184. ledindex++;
  185. ledbri = (pgm_read_byte(&led_data[ledindex]));
  186. ledindex++;
  187. ledtime = (pgm_read_byte(&led_data[ledindex]));
  188. ledindex++;
  189. videoram[ledx]= ledbri;
  190. for (int m = 0; m < (ledtime*speed); m++) {
  191. multiplex();
  192. }
  193. }
  194. }
  195. void testled ()
  196. {
  197. while (1)
  198. {
  199. fullMatrix();
  200. //beep ();
  201. for (byte m = 0; m < 100; m++) {
  202. multiplex();
  203. }
  204. clearMatrix();
  205. //beep ();
  206. for (byte m = 0; m < 250; m++) {
  207. multiplex();
  208. }
  209. }
  210. }
  211. void testledon ()
  212. {
  213. fullMatrix();
  214. while (1)
  215. {
  216. //beep ();
  217. for (byte m = 0; m < 250; m++) {
  218. multiplex();
  219. }
  220. }
  221. }
  222. void clearMatrix() {
  223. //clear the matrix by inserting 0 to the matrixState
  224. for (byte i = 0; i < 21; i++) {
  225. videoram[i] = 0;
  226. }
  227. }
  228. void fullMatrix() {
  229. //turn on all LEDs in the matrix by inserting 1 to the matrixState
  230. for (byte i = 0; i < 21; i++) {
  231. videoram[i] = 1;
  232. }
  233. }
  234. void multiplex() {
  235. // THIS PART IS USED TO UPDATE THE CHARLIEPLEXING LEDS MATRIX
  236. //check from matrixState which LED to turn ON or OFF
  237. for (byte i = 0; i < 21; i++) {
  238. if (videoram[i] != 0) { //turn on LED with 1 in matrixState
  239. pinMode(pins[connectionMatrix[i][0]], OUTPUT); //set positive pole to OUTPUT
  240. pinMode(pins[connectionMatrix[i][1]], OUTPUT); //set negative pole to OUTPUT
  241. digitalWrite(pins[connectionMatrix[i][0]], HIGH); //set positive pole to HIGH
  242. digitalWrite(pins[connectionMatrix[i][1]], LOW); //set negative pole to LOW
  243. delayMicroseconds(videoram[i]*40); //250
  244. pinMode(pins[connectionMatrix[i][0]], INPUT); //set both positive pole and negative pole
  245. pinMode(pins[connectionMatrix[i][1]], INPUT); // to INPUT in order to turn OFF the LED
  246. delayMicroseconds(201-(videoram[i]*40)); //250
  247. }
  248. if (videoram[i] == 0) { //turn off LED with 0 in matrixState
  249. pinMode(pins[connectionMatrix[i][0]], INPUT); //set both positive pole and negative pole
  250. pinMode(pins[connectionMatrix[i][1]], INPUT); // to INPUT in order to turn OFF the LED
  251. // delayMicroseconds(200); //250
  252. }
  253. }
  254. }
  255. void randomflash (byte spd)
  256. {
  257. while (1){
  258. bx=random(0, 4);
  259. by=random(0, 5);
  260. blik( bx, by, spd);
  261. }
  262. }
  263. void randomflashx ()
  264. {
  265. while (1){
  266. bx=random(0, 4);
  267. by=random(0, 5);
  268. blik( bx, by, random(0, 6));
  269. }
  270. }
  271. void pongflash (byte spd)
  272. {
  273. int bx,by,dx,dy;
  274. while (1)
  275. {
  276. // bx=0; //random(0, 4);
  277. // by=0; //random(0, 5);
  278. if (bx>=3) dx=random(-1, 1);
  279. if (by>=4) dy=random(-1, 1);
  280. if (bx<=0) dx=random(0, 2);
  281. if (by<=0) dy=random(0, 2);
  282. if (bx>=3) bx=3;
  283. if (by>=4) by=4;
  284. if (bx<=0) bx=0;
  285. if (by<=0) by=0;
  286. //if (dx==0) dx=-1;
  287. //if (dy==0) dy=-1;
  288. //if (bx>=3) dx=-1;
  289. //if (by>=4) dy=-1;
  290. //if (bx==0) dx=1;
  291. //if (by==0) dy=1;
  292. bx=bx+dx;
  293. by=by+dy;
  294. blik( bx, by, spd);
  295. }
  296. }
  297. void spiral (byte spd)
  298. {
  299. byte mx[] = {0,1,2,3, 3,3,3,3, 2,1,0, 0,0,0, 1,2, 2,2, 1, 1,1, 0,0,0,0,};
  300. byte my[] = {0,0,0,0, 1,2,3,4, 4,4,4, 3,2,1, 1,1, 2,3, 3, 2,1, 0,0,0,0,};
  301. while (1)
  302. {
  303. for (byte n = 0; n < 21; n++)
  304. {
  305. blik( mx[n], my[n], 3);
  306. }
  307. for (byte n = 0; n < 21; n++)
  308. {
  309. blik( mx[20-n], my[20-n], 4);
  310. }
  311. }
  312. }
  313. void spiralx ()
  314. {
  315. byte mx[] = {0,1,2,3, 3,3,3,3, 2,1,0, 0,0,0, 1,2, 2,2, 1, 1,1, 0,0,0,0,};
  316. byte my[] = {0,0,0,0, 1,2,3,4, 4,4,4, 3,2,1, 1,1, 2,3, 3, 2,1, 0,0,0,0,};
  317. while (1)
  318. {
  319. for (byte n = 0; n < 21; n++)
  320. {
  321. setPixel( mx[20-n], my[20-n], 1);
  322. setup_watchdog(2); // approximately 2 seconds sleep
  323. system_sleep(); // 0=16ms,
  324. pinMode(0, INPUT);
  325. pinMode(1, INPUT);
  326. pinMode(2, INPUT);
  327. pinMode(3, INPUT);
  328. pinMode(4, INPUT);
  329. }
  330. setup_watchdog(4); // sleep
  331. // 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec // wait for a second
  332. system_sleep();
  333. for (byte n = 0; n < 21; n++)
  334. {
  335. setPixel( mx[n], my[n], 1);
  336. setup_watchdog(2); // approximately 2 seconds sleep
  337. system_sleep(); // 0=16ms,
  338. pinMode(0, INPUT);
  339. pinMode(1, INPUT);
  340. pinMode(2, INPUT);
  341. pinMode(3, INPUT);
  342. pinMode(4, INPUT);
  343. }
  344. setup_watchdog(1); // sleep
  345. // 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec // wait for a second
  346. system_sleep();
  347. }
  348. }
  349. void maze (byte spd)
  350. {
  351. int mx,my,bx,by,dx,dy;
  352. mx=random(0, 4);
  353. my=random(0, 5);
  354. bx=0; //random(0, 4);
  355. by=0; //random(0, 5);
  356. dy=1;
  357. dx=1;
  358. while (1)
  359. {
  360. if ((bx>=3)||(bx>=mx)){
  361. dy=-1;
  362. mx=random(0, 4);
  363. }
  364. if ((by>=4)||(by>=my)){
  365. dx=-1;
  366. my=random(0, 5);
  367. }
  368. if (bx<=0) dx=random(-1, 2);
  369. if (by<=0) dy=random(-1, 2);
  370. if (bx>3) bx=3;
  371. if (by>4) by=4;
  372. if (bx<0) bx=0;
  373. if (by<0) by=0;
  374. bx=bx+dx;
  375. by=by+dy;
  376. blik( bx, by, 1);
  377. }
  378. }
  379. void rainflash (byte spd)
  380. {
  381. int xrain;
  382. while (1)
  383. {
  384. bx=random(0, 4);
  385. xrain=random(0, 7);
  386. for (byte by = 0; by < 5; by++)
  387. {
  388. blik( bx, by, xrain);
  389. }
  390. }
  391. }
  392. void scan (byte spd)
  393. {
  394. int xrain;
  395. while (1)
  396. {
  397. for (byte by = 0; by < 5; by++)
  398. {
  399. for (byte bx = 0; bx < 4; bx++)
  400. {
  401. blik( bx, by, spd);
  402. }
  403. }
  404. }
  405. }
  406. void blik (byte sx, byte sy, byte spd)
  407. {
  408. setPixel( sx, sy, 1);
  409. setup_watchdog(2); // approximately 2 seconds sleep
  410. system_sleep(); // 0=16ms,
  411. pinMode(0, INPUT);
  412. pinMode(1, INPUT);
  413. pinMode(2, INPUT);
  414. pinMode(3, INPUT);
  415. pinMode(4, INPUT);
  416. setup_watchdog(spd); // sleep
  417. // 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec // wait for a second
  418. system_sleep();
  419. }
  420. EMPTY_INTERRUPT(WDT_vect);
  421. // 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
  422. // 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
  423. void setup_watchdog(int ii) {
  424. byte bb;
  425. int ww;
  426. if (ii > 9 ) ii=9;
  427. bb=ii & 7;
  428. if (ii > 7) bb|= (1<<5);
  429. bb|= (1<<WDCE);
  430. ww=bb;
  431. MCUSR &= ~(1<<WDRF);
  432. // start timed sequence
  433. WDTCR |= (1<<WDCE) | (1<<WDE);
  434. // set new watchdog timeout value
  435. WDTCR = bb;
  436. WDTCR |= _BV(WDIE);
  437. }
  438. void system_sleep() {
  439. cbi(ADCSRA,ADEN); // switch Analog to Digitalconverter OFF
  440. set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
  441. sleep_enable();
  442. sleep_mode(); // System sleeps here
  443. sleep_disable(); // System continues execution here when watchdog timed out
  444. // sbi(ADCSRA,ADEN); // switch Analog to Digitalconverter ON
  445. }
  446. void EEReadSettings (void) { // TODO: Detect ANY bad values, not just 255.
  447. byte detectBad = 0;
  448. byte value = 255;
  449. value = EEPROM.read(0);
  450. if (value > MAX_MODE)
  451. detectBad = 1;
  452. else
  453. last_mode = value; // MainBright has maximum possible value of 8.
  454. if (detectBad)
  455. last_mode = 0; // I prefer the rainbow effect.
  456. }
  457. void EESaveSettings (void){
  458. //EEPROM.write(Addr, Value);
  459. // Careful if you use this function: EEPROM has a limited number of write
  460. // cycles in its life. Good for human-operated buttons, bad for automation.
  461. byte value = EEPROM.read(0);
  462. if(value != last_mode)
  463. EEPROM.write(0, last_mode);
  464. }
  465. void SleepNow(void) {
  466. pinMode(0, INPUT);
  467. pinMode(1, INPUT);
  468. pinMode(2, INPUT);
  469. pinMode(3, INPUT);
  470. pinMode(4, INPUT);
  471. if(last_mode > MAX_MODE) { last_mode = MAX_MODE; }
  472. EESaveSettings();
  473. // Important power management stuff follows
  474. ADCSRA &= ~(1<<ADEN); // turn off the ADC
  475. ACSR |= _BV(ACD); // disable the analog comparator
  476. MCUCR |= _BV(BODS) | _BV(BODSE); // turn off the brown-out detector
  477. set_sleep_mode(SLEEP_MODE_PWR_DOWN); // do a complete power down
  478. sleep_enable(); // enable sleep mode
  479. sei(); // allow interrupts to end sleep mode
  480. sleep_cpu(); // go to sleep
  481. delay(500);
  482. sleep_disable(); // disable sleep mode for safety
  483. }
  484. //------------------------------ Multiplex ---------------------------------
  485. void showRam ()
  486. {
  487. byte colData=0;
  488. for(int col = 0; col <5; col++){
  489. colData = videoram[col];
  490. for(int row = 0; row <5; row++){
  491. if ( colData & (1<<row) ) {
  492. setPixel( col, row, 1);
  493. }
  494. else {
  495. setPixel( col, row, 0);
  496. }
  497. }
  498. }
  499. }
  500. void setPixel(byte x, byte y, boolean ledStatus) {
  501. x=3-x;
  502. if (x >= 0 && x < DISPLAY_WIDTH) {
  503. if (y <= x) {
  504. x++;
  505. }
  506. setLed(y, x, ledStatus);
  507. }
  508. }
  509. // turn on the pins to light a LED
  510. void setLed(byte vin, byte gnd, boolean ledStatus) {
  511. set_sleep_mode(SLEEP_MODE_IDLE); // do a complete power down
  512. sleep_enable(); // enable sleep mode
  513. sleep_cpu();
  514. // delay(1); // Wait
  515. pinMode(pins[0], INPUT);
  516. pinMode(pins[1], INPUT);
  517. pinMode(pins[2], INPUT);
  518. pinMode(pins[3], INPUT);
  519. pinMode(pins[4], INPUT);
  520. // set_sleep_mode(SLEEP_MODE_IDLE); // do a complete power down
  521. // sleep_enable(); // enable sleep mode
  522. // sleep_cpu();
  523. if(!ledStatus) return;
  524. pinMode(pins[vin], OUTPUT);
  525. pinMode(pins[gnd], OUTPUT);
  526. digitalWrite(pins[vin], HIGH);
  527. digitalWrite(pins[gnd], LOW);
  528. }
  529. void beep ()
  530. {
  531. playTone( 90, 1000);
  532. playTone( 90, 3000);
  533. playTone( 90, 2000);
  534. }
  535. void playTone(long duration, int freq) {
  536. duration *= 700;
  537. int period = (1.0 / freq) * 1000000;
  538. long elapsed_time = 0;
  539. int amax=0;
  540. byte up=1;
  541. pinMode(pins[connectionMatrix[19][0]], OUTPUT);
  542. pinMode(pins[connectionMatrix[19][1]], OUTPUT); //set negative pole to OUTPUT
  543. while (elapsed_time < duration) {
  544. digitalWrite(pins[connectionMatrix[19][0]], HIGH);
  545. digitalWrite(pins[connectionMatrix[19][1]], LOW); //set negative pole to LOW
  546. delayMicroseconds(period / 2);
  547. digitalWrite(pins[connectionMatrix[19][0]], LOW);
  548. digitalWrite(pins[connectionMatrix[19][1]], HIGH); //set negative pole to LOW
  549. delayMicroseconds(period / 2);
  550. elapsed_time += (period);
  551. }
  552. pinMode(pins[connectionMatrix[19][0]], INPUT);
  553. pinMode(pins[connectionMatrix[19][1]], INPUT); //set negative pole to OUTPUT
  554. }