Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LED_FastGPIO.ino 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. #include <FastGPIO.h>
  2. #include <TimerOne.h>
  3. int iByte;
  4. byte col = 0;
  5. byte leds[12][4];
  6. byte pass = 1;
  7. int fadecount = 1;
  8. const int fadelimit = 3000;
  9. const int fadelimitshort = 1000;
  10. byte mode = 4;
  11. byte brightness = 2;
  12. boolean changemode = 0;
  13. int rain = 0;
  14. const int rainlimit = 5000;
  15. const int rainfade = 5000;
  16. byte rx = 0;
  17. byte ry = 0;
  18. // pin[xx] on led matrix connected to nn on Arduino (-1 is dummy to make array start at pos 1)
  19. int pins[17] = {
  20. -1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 16, 14, 15, 18, 19, 20, 21
  21. };
  22. // col[xx] of leds = pin yy on led matrix
  23. int cols[12] = {
  24. pins[8], pins[7], pins[6], pins[5], pins[9], pins[10], pins[11], pins[12], pins[13], pins[14], pins[15], pins[16]
  25. };
  26. // row[xx] of leds = pin yy on led matrix
  27. int rows[4] = {
  28. pins[1], pins[2], pins[3], pins[4]
  29. };
  30. #define DELAY 0
  31. extern byte leds[12][4];
  32. void setup() {
  33. Serial1.begin(9600);
  34. setupLeds();
  35. for (int s = 0; s < 5; s++) {
  36. for ( int r = 1; r < 9; r++) {
  37. delayMicroseconds(65000);
  38. delayMicroseconds(65000);
  39. for (int j = 0; j < 4; j++) {
  40. for (int i = 0; i < 12; i++) {
  41. leds[i][j] = 1;
  42. for (int p = 0; p < 25; p++) {
  43. }
  44. leds[i][j] = r;
  45. }
  46. }
  47. }
  48. for ( int r = 9; r > 0; r--) {
  49. delayMicroseconds(65000);
  50. delayMicroseconds(65000);
  51. delayMicroseconds(65000);
  52. for (int j = 0; j < 4; j++) {
  53. for (int i = 0; i < 12; i++) {
  54. leds[i][j] = 1;
  55. for (int p = 0; p < 25; p++) {
  56. }
  57. leds[i][j] = r;
  58. }
  59. }
  60. }
  61. }
  62. }
  63. void loop() {
  64. switch (mode) {
  65. case 0:
  66. //Blacklight
  67. for (int i = 0; i < 12; i++) {
  68. for (int j = 0; j < 4; j++) {
  69. leds[i][j] = brightness;
  70. }
  71. }
  72. checkserial();
  73. break;
  74. case 1:
  75. //Breathing
  76. for ( int r = 1; r < 9; r++) {
  77. checkserial();
  78. if (changemode == 0) {
  79. delayMicroseconds(65000);
  80. delayMicroseconds(65000);
  81. delayMicroseconds(65000);
  82. for (int j = 0; j < 4; j++) {
  83. for (int i = 0; i < 12; i++) {
  84. leds[i][j] = 1;
  85. for (int p = 0; p < 25; p++) {
  86. }
  87. leds[i][j] = r;
  88. }
  89. }
  90. }
  91. else {
  92. break;
  93. }
  94. }
  95. for ( int r = 9; r > 0; r--) {
  96. checkserial();
  97. if (changemode == 0) {
  98. delayMicroseconds(65000);
  99. delayMicroseconds(65000);
  100. delayMicroseconds(65000);
  101. delayMicroseconds(65000);
  102. for (int j = 0; j < 4; j++) {
  103. for (int i = 0; i < 12; i++) {
  104. leds[i][j] = 1;
  105. for (int p = 0; p < 25; p++) {
  106. }
  107. leds[i][j] = r;
  108. }
  109. }
  110. }
  111. else {
  112. break;
  113. }
  114. }
  115. for ( int r = 1; r < 30; r++) {
  116. checkserial();
  117. if (changemode == 0) {
  118. delayMicroseconds(65000);
  119. delayMicroseconds(65000);
  120. }
  121. else {
  122. break;
  123. }
  124. }
  125. break;
  126. case 2:
  127. //Random
  128. leds[random(12)][random(4)] = random(8);
  129. delayMicroseconds(10000);
  130. checkserial();
  131. break;
  132. case 3:
  133. //Rain
  134. rain++;
  135. if (rain > rainlimit) {
  136. rain = 0;
  137. rx = random(12);
  138. ry = random(4);
  139. if (leds[rx][ry] == 0) {
  140. leds[rx][ry] = 18;
  141. }
  142. }
  143. fadecount++;
  144. if (fadecount > rainfade) {
  145. fadecount = 1;
  146. for (int i = 0; i < 12; i++) {
  147. for (int j = 0; j < 4; j++) {
  148. if (leds[i][j] > 0) {
  149. leds[i][j] = leds[i][j] - 1;
  150. }
  151. }
  152. }
  153. }
  154. checkserial();
  155. break;
  156. case 4:
  157. //Reactive
  158. fadecount++;
  159. if (fadecount > fadelimit) {
  160. fadecount = 1;
  161. for (int i = 0; i < 12; i++) {
  162. for (int j = 0; j < 4; j++) {
  163. if (leds[i][j] > 0) {
  164. leds[i][j] = leds[i][j] - 1;
  165. }
  166. }
  167. }
  168. }
  169. checkserial();
  170. break;
  171. case 5:
  172. //Reactive Target
  173. fadecount++;
  174. if (fadecount > fadelimitshort) {
  175. fadecount = 1;
  176. for (int i = 0; i < 12; i++) {
  177. for (int j = 0; j < 4; j++) {
  178. if (leds[i][j] > 0) {
  179. leds[i][j] = leds[i][j] - 1;
  180. }
  181. }
  182. }
  183. }
  184. checkserial();
  185. break;
  186. default:
  187. mode = 0;
  188. break;
  189. }
  190. changemode = 0;
  191. }
  192. void checkserial() {
  193. if (Serial1.available() > 0) {
  194. iByte = Serial1.read();
  195. if (iByte == 100) {
  196. brightness++;
  197. if (brightness > 9) {
  198. brightness = 1;
  199. }
  200. }
  201. if (iByte == 101) {
  202. mode++;
  203. }
  204. if (iByte < 100) {
  205. if (mode == 4) {
  206. byte row = iByte / 16;
  207. byte col = iByte % 16;
  208. leds[col][row] = 18;
  209. }
  210. if (mode == 5) {
  211. byte row = iByte / 16;
  212. byte col = iByte % 16;
  213. for (byte i = 0; i < 12; i++) {
  214. leds[i][row] = 18;
  215. }
  216. for (byte p = 0; p < 4; p++) {
  217. leds[col][p] = 18;
  218. }
  219. }
  220. }
  221. }
  222. }
  223. void setupLeds() {
  224. // sets the pins as output
  225. FastGPIO::Pin<2>::setOutputLow();
  226. FastGPIO::Pin<3>::setOutputLow();
  227. FastGPIO::Pin<4>::setOutputLow();
  228. FastGPIO::Pin<5>::setOutputLow();
  229. FastGPIO::Pin<6>::setOutputLow();
  230. FastGPIO::Pin<7>::setOutputLow();
  231. FastGPIO::Pin<8>::setOutputLow();
  232. FastGPIO::Pin<9>::setOutputLow();
  233. FastGPIO::Pin<10>::setOutputLow();
  234. FastGPIO::Pin<16>::setOutputLow();
  235. FastGPIO::Pin<14>::setOutputLow();
  236. FastGPIO::Pin<15>::setOutputLow();
  237. FastGPIO::Pin<18>::setOutputLow();
  238. FastGPIO::Pin<19>::setOutputLow();
  239. FastGPIO::Pin<20>::setOutputLow();
  240. FastGPIO::Pin<21>::setOutputLow();
  241. // set up Cols
  242. FastGPIO::Pin<6>::setOutputValueLow();
  243. FastGPIO::Pin<7>::setOutputValueLow();
  244. FastGPIO::Pin<8>::setOutputValueLow();
  245. FastGPIO::Pin<9>::setOutputValueLow();
  246. FastGPIO::Pin<10>::setOutputValueLow();
  247. FastGPIO::Pin<16>::setOutputValueLow();
  248. FastGPIO::Pin<14>::setOutputValueLow();
  249. FastGPIO::Pin<15>::setOutputValueLow();
  250. FastGPIO::Pin<18>::setOutputValueLow();
  251. FastGPIO::Pin<19>::setOutputValueLow();
  252. FastGPIO::Pin<20>::setOutputValueLow();
  253. FastGPIO::Pin<21>::setOutputValueLow();
  254. // set up Rows
  255. FastGPIO::Pin<2>::setOutputValueLow();
  256. FastGPIO::Pin<3>::setOutputValueLow();
  257. FastGPIO::Pin<4>::setOutputValueLow();
  258. FastGPIO::Pin<5>::setOutputValueLow();
  259. clearLeds();
  260. Timer1.initialize(25);
  261. Timer1.attachInterrupt(display);
  262. }
  263. void clearLeds() {
  264. // Clear display array
  265. for (int i = 0; i < 12; i++) {
  266. for (int j = 0; j < 4; j++) {
  267. leds[i][j] = 0;
  268. }
  269. }
  270. }
  271. void onLeds() {
  272. // Clear display array
  273. for (int i = 0; i < 12; i++) {
  274. for (int j = 0; j < 4; j++) {
  275. leds[i][j] = 7;
  276. }
  277. }
  278. }
  279. // Interrupt routine
  280. void display() {
  281. switch (col) { // Turn whole previous column off
  282. case 0:
  283. FastGPIO::Pin<6>::setOutputValueLow();
  284. break;
  285. case 1:
  286. FastGPIO::Pin<7>::setOutputValueLow();
  287. break;
  288. case 2:
  289. FastGPIO::Pin<8>::setOutputValueLow();
  290. break;
  291. case 3:
  292. FastGPIO::Pin<9>::setOutputValueLow();
  293. break;
  294. case 4:
  295. FastGPIO::Pin<10>::setOutputValueLow();
  296. break;
  297. case 5:
  298. FastGPIO::Pin<16>::setOutputValueLow();
  299. break;
  300. case 6:
  301. FastGPIO::Pin<14>::setOutputValueLow();
  302. break;
  303. case 7:
  304. FastGPIO::Pin<15>::setOutputValueLow();
  305. break;
  306. case 8:
  307. FastGPIO::Pin<18>::setOutputValueLow();
  308. break;
  309. case 9:
  310. FastGPIO::Pin<19>::setOutputValueLow();
  311. break;
  312. case 10:
  313. FastGPIO::Pin<20>::setOutputValueLow();
  314. break;
  315. case 11:
  316. FastGPIO::Pin<21>::setOutputValueLow();
  317. break;
  318. }
  319. col++;
  320. if (col == 12) {
  321. col = 0;
  322. pass++;
  323. if (pass > 8) {
  324. pass = 1;
  325. }
  326. }
  327. for (int row = 0; row < 4; row++) {
  328. if (leds[col][row] > pass) {
  329. switch (row) { // Turn on this led
  330. case 0:
  331. FastGPIO::Pin<2>::setOutputValueLow();
  332. break;
  333. case 1:
  334. FastGPIO::Pin<3>::setOutputValueLow();
  335. break;
  336. case 2:
  337. FastGPIO::Pin<4>::setOutputValueLow();
  338. break;
  339. case 3:
  340. FastGPIO::Pin<5>::setOutputValueLow();
  341. break;
  342. }
  343. }
  344. else {
  345. switch (row) { // Turn off this led
  346. case 0:
  347. FastGPIO::Pin<2>::setOutputValueHigh();
  348. break;
  349. case 1:
  350. FastGPIO::Pin<3>::setOutputValueHigh();
  351. break;
  352. case 2:
  353. FastGPIO::Pin<4>::setOutputValueHigh();
  354. break;
  355. case 3:
  356. FastGPIO::Pin<5>::setOutputValueHigh();
  357. break;
  358. }
  359. }
  360. }
  361. switch (col) { // Turn column on
  362. case 0:
  363. FastGPIO::Pin<6>::setOutputValueHigh();
  364. break;
  365. case 1:
  366. FastGPIO::Pin<7>::setOutputValueHigh();
  367. break;
  368. case 2:
  369. FastGPIO::Pin<8>::setOutputValueHigh();
  370. break;
  371. case 3:
  372. FastGPIO::Pin<9>::setOutputValueHigh();
  373. break;
  374. case 4:
  375. FastGPIO::Pin<10>::setOutputValueHigh();
  376. break;
  377. case 5:
  378. FastGPIO::Pin<16>::setOutputValueHigh();
  379. break;
  380. case 6:
  381. FastGPIO::Pin<14>::setOutputValueHigh();
  382. break;
  383. case 7:
  384. FastGPIO::Pin<15>::setOutputValueHigh();
  385. break;
  386. case 8:
  387. FastGPIO::Pin<18>::setOutputValueHigh();
  388. break;
  389. case 9:
  390. FastGPIO::Pin<19>::setOutputValueHigh();
  391. break;
  392. case 10:
  393. FastGPIO::Pin<20>::setOutputValueHigh();
  394. break;
  395. case 11:
  396. FastGPIO::Pin<21>::setOutputValueHigh();
  397. break;
  398. }
  399. }