cube_control.ino 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //Arduino\hardware\arduino\avr\cores\arduino
  2. #define SERIAL_RX_BUFFER_SIZE = 256
  3. #include <dataflash.h>
  4. #include <EEPROM.h>
  5. #include <TimerOne.h>
  6. int animationId = 0;
  7. unsigned long animationStart = 0;
  8. unsigned long animationLength = 0;
  9. unsigned long frameIndex = 0;
  10. bool animate = 0;
  11. Dataflash dflash;
  12. unsigned char buf[32];
  13. byte zpin[4]={6,5,9,10}; // z axis on pwmable pins
  14. byte sw[3]={A4,A5,2}; // 3 switches
  15. unsigned long led[4][2];//led[plane] [regNo] = brightness + (ledNo(8) << 16)
  16. unsigned int brightness[16] = {0, 1, 257, 2113, 4369, 9361, 35476, 21833, 43690, 21931, 30422, 61142, 30583, 65518, 63479, 65535};
  17. void setup() {
  18. Serial.begin(115200);
  19. pinMode(A0, OUTPUT);//data
  20. pinMode(A1, OUTPUT);//sck
  21. pinMode(A2, OUTPUT);//rck
  22. pinMode(A3, OUTPUT);//sclr
  23. for(byte i=0; i<4; i++) {
  24. pinMode(zpin[i], OUTPUT);
  25. }
  26. for(byte i=0; i<3; i++) {
  27. pinMode(sw[i], INPUT_PULLUP);
  28. }
  29. attachInterrupt(0, ta3, FALLING );
  30. //attachInterrupt(1, suspend, CHANGE );
  31. digitalWrite(A3, 0);
  32. digitalWrite(A3, 1);
  33. Timer1.initialize(16667);
  34. Timer1.attachInterrupt( timerIsr );
  35. dflash.init();
  36. Serial.println("AtMega168PA");
  37. }
  38. void loop() {
  39. while(Serial.available()>=1) {
  40. char com = Serial.read();
  41. if(com == '~') {
  42. if(waitForSerial(1)) return;
  43. exec(Serial.read());
  44. } else {
  45. error("no cmd",com);
  46. }
  47. }
  48. show();
  49. }
  50. void exec(byte com) {
  51. if(com < 48) { //data
  52. return;
  53. }
  54. else if(com == '`') { //set 1 led
  55. if(waitForSerial(2)) return;
  56. byte get[2]={Serial.read()-32,Serial.read()-32};
  57. if(get[0]<64 && get[1]<16) {
  58. set(get[0], get[1]);
  59. }
  60. }
  61. else if(com == 'a') {//set all leds
  62. byte i = 0;
  63. for(byte k=16; k<=64; k+=16) {
  64. if(waitForSerial(16)) return;
  65. while(i < k) {
  66. set(i++, Serial.read()-32);
  67. }
  68. }
  69. }
  70. else if(com == 'b') {//set eeprom
  71. if(waitForSerial(9)) return;//0:id, 1-4:start, 5-8:length
  72. int i = Serial.read() * 8; //64 Id's
  73. if(i>=512) {
  74. error("EEPROM index", i);
  75. return;
  76. }
  77. for(byte n=0;n<8;n++) {
  78. EEPROM.write(i+n, Serial.read());
  79. }
  80. }
  81. else if(com == 'B') {//get eeprom
  82. //0-3:start, 4-7:length
  83. //64 Id's
  84. for(int i=0; i<64; i++) {
  85. Serial.print(i);
  86. Serial.print(',');
  87. Serial.print(eepromReadLong(i*8));
  88. Serial.print(',');
  89. Serial.println(eepromReadLong(i*8+4));
  90. }
  91. }
  92. else if(com == 'c') {//set all leds to a value
  93. if(waitForSerial(1)) return;
  94. byte x = Serial.read()-32;
  95. memset(led,x+(x<<4),sizeof(led));
  96. }
  97. else if(com == 'd') {//dump buffer
  98. for(byte i=0;i<16;i++)
  99. debug(String(dflash.Buffer_Read_Byte(1,i*32)), i);
  100. }
  101. else if(com == 'g') {//get frame count
  102. Serial.println(frameIndex);
  103. }
  104. else if(com == 'h') {//set frame count
  105. if(waitForSerial(4)) return;
  106. frameIndex = readLong();
  107. if(frameIndex >= 65536)
  108. frameIndex = 0;
  109. dflash.Page_To_Buffer(frameIndex/16, 2);
  110. dflash.Buffer_Read_Str(2, (frameIndex % 16)*32, 32, buf);
  111. for(byte i=0;i<8;i++) {
  112. led[i>>1][i&1] = buf2Long(i*4);
  113. }
  114. }
  115. else
  116. if(com == 's') {
  117. animate = 1;
  118. if(waitForSerial(1)) return;
  119. int i = Serial.read(); //64 Id's
  120. if(i>=64) {
  121. error("EEPROM index", i);
  122. return;
  123. }
  124. animationId = i;
  125. readAnim();
  126. }
  127. else if(com == 't') {
  128. if(frameIndex<animationStart+animationLength)
  129. animate = !animate;
  130. }
  131. else if(com == 'u') {
  132. animate = 0;
  133. //readAnim();
  134. }
  135. return;
  136. if(com == 'x') {//load page to buffer 1
  137. //4196 pages
  138. if(waitForSerial(2)) return;
  139. int i = readInt();
  140. if(i>=4196) {
  141. error("read page index", i);
  142. return;
  143. }
  144. dflash.Page_To_Buffer(i, 1);
  145. }
  146. else if(com == 'y') {//set pattern in buffer 1
  147. if(waitForSerial(1)) return;
  148. int i = Serial.read() * 32;//16
  149. if(i>=512) {
  150. error("buffer index", i);
  151. return;
  152. }
  153. for(byte i=0;i<8;i++) {
  154. buf[i*4] = (led[i>>1][i&1]>>24)&0xff;
  155. buf[i*4+1] = (led[i>>1][i&1]>>16)&0xff;
  156. buf[i*4+2] = (led[i>>1][i&1]>>8)&0xff;
  157. buf[i*4+3] = (led[i>>1][i&1])&0xff;
  158. }
  159. dflash.Buffer_Write_Str(1,i,32,buf);
  160. }
  161. else if(com == 'z') {//write buffer 1 to page
  162. /* 4bit/led * 64 = 32 byte
  163. 2097152 byte / 32 = 65536
  164. 18m12@60p/s
  165. 528/32=16.5
  166. * 4196 pages
  167. */
  168. if(waitForSerial(2)) return;
  169. int i = readInt();
  170. if(i>=4196) {
  171. error("write page index", i);
  172. return;
  173. }
  174. dflash.Buffer_To_Page(1, i);
  175. debug("buffer to page", i);
  176. }
  177. else {
  178. error("no cmd", com);
  179. }
  180. }
  181. bool waitForSerial(byte n) {
  182. unsigned long t = millis();
  183. while(Serial.available()<n) {
  184. if(millis() - t > n * 2) {
  185. error("timeout at byte", Serial.available());
  186. return true;
  187. }
  188. show();
  189. }
  190. return false;
  191. }
  192. unsigned int readInt() {
  193. return (Serial.read()<<8)+(Serial.read());
  194. }
  195. unsigned long readLong() {
  196. return (Serial.read()<<24)+(Serial.read()<<16)+(Serial.read()<<8)+(Serial.read());
  197. }
  198. unsigned long eepromReadLong(int addr) {
  199. return ((unsigned long)EEPROM.read(addr)<<24)+
  200. ((unsigned long)EEPROM.read(addr+1)<<16)+
  201. ((unsigned long)EEPROM.read(addr+2)<<8)+
  202. ((unsigned long)EEPROM.read(addr+3));
  203. }
  204. unsigned long buf2Long(byte addr) {
  205. return ((unsigned long)buf[addr]<<24)+
  206. ((unsigned long)buf[addr+1]<<16)+
  207. ((unsigned long)buf[addr+2]<<8)+
  208. ((unsigned long)buf[addr+3]);
  209. }
  210. void set(byte i, unsigned long b) {
  211. led[i>>4][(i&B100)>>2] &= ~((unsigned long)0xf << (((i&B11) << 3) + ((i&B1000)>>1)));
  212. led[i>>4][(i&B100)>>2] |= (b&B1111) << (((i&B11) << 3) + ((i&B1000)>>1));
  213. }
  214. void add(byte x, byte y, byte z, unsigned long b) {
  215. led[z][y%2] += b << (x*8+y*2);
  216. }
  217. void show() {
  218. byte hOld = 3;
  219. for(byte i=0;i<16;i++) { //brightnesses
  220. for(byte h=0;h<4;h++) { //z axis
  221. for(byte k=0;k<8; k++) {
  222. PORTC |= (brightness[(led[h][0] >> (k<<2)) & 0xf] >> i) & 1;
  223. PORTC |= B00000010;
  224. PORTC &= B11111100;
  225. PORTC |= (brightness[(led[h][1] >> (k<<2)) & 0xf] >> i) & 1;
  226. PORTC |= B00000010;
  227. PORTC &= B11111100;
  228. }
  229. digitalWrite(zpin[hOld],0);
  230. PORTC |= B00000100;
  231. PORTC &= B11111011;
  232. digitalWrite(zpin[h],1);
  233. hOld = h;
  234. delayMicroseconds(5);
  235. }
  236. }
  237. }
  238. void suspend() {
  239. Serial.println("ISR: suspend");
  240. }
  241. void ta3() {
  242. readAnim();
  243. if(++animationId==64 || animationLength == 0) animationId = 0;
  244. if(animationLength == 0) {
  245. animate=0;
  246. memset(led,0,sizeof(led));
  247. } else {
  248. animate=1;
  249. }
  250. }
  251. void readAnim() {
  252. animationStart = eepromReadLong(animationId*8);
  253. animationLength = eepromReadLong(animationId*8+4);
  254. debug("Animation Id:", animationId);
  255. debug("Start:", animationStart);
  256. debug("length:", animationLength);
  257. frameIndex=animationStart;
  258. }
  259. void timerIsr() {
  260. if(animate) {
  261. if(frameIndex >= 65536) frameIndex = 0;
  262. if(frameIndex % 4 == 0) dflash.Page_To_Buffer(frameIndex/4, 2);
  263. dflash.Buffer_Read_Str(2, (frameIndex % 4)*32, 32, buf);
  264. for(byte i=0;i<8;i++) {
  265. led[i>>1][i&1] = buf2Long(i*4);
  266. }
  267. frameIndex++;
  268. if(frameIndex>=animationStart+animationLength) {
  269. animate = 0;
  270. memset(led,0,sizeof(led));
  271. debug("animation stopped", frameIndex);
  272. }
  273. }
  274. }
  275. void error(const String& txt, const uint32_t& val) {
  276. Serial.print("ERR: ");
  277. Serial.print(txt);
  278. Serial.print(" ");
  279. Serial.println(val);
  280. }
  281. void debug(const String& txt, const uint32_t& val) {
  282. Serial.print("DEBUG: ");
  283. Serial.print(txt);
  284. Serial.print(" ");
  285. Serial.println(val);
  286. }