Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.39 KB | None | 0 0
  1. #include "TinyTimber.h"
  2. #include "sciTinyTimber.h"
  3. #include "canTinyTimber.h"
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6.  
  7. //{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},
  8. //{246, 261, 277, 293, 311, 329, 349, 369, 391, 415, 440, 466, 493, 523, 554, 587, 622, 659, 698, 739, 783, 830, 880, 932, 987},
  9.  
  10. #define DAC_port *((unsigned volatile char *) 0x4000741C)
  11.  
  12. typedef struct {
  13. Object super;
  14. int count;
  15. char c;
  16. } App;
  17.  
  18. /*--------------PART 0-------------*/
  19. typedef struct {
  20. Object super;
  21. int num;
  22. int sum;
  23. char buf[100];
  24. int i;
  25. } Add;
  26.  
  27. typedef struct {
  28. Object super;
  29. int x;
  30. char buf[100];
  31. int period[100];
  32. int song[100];
  33. } IFP;
  34. /*--------------PART 1-------------*/
  35.  
  36. typedef struct {
  37. Object super;
  38. Time period;
  39. Time deadline;
  40. int volume; //HEX
  41. int volumeSave;
  42. int state;
  43. int flag;
  44. }PeriodicTask;
  45.  
  46. typedef struct {
  47. Object super;
  48. int background_loop_range;
  49. Time period;
  50. Time deadline;
  51. char buf[100];
  52. }BackgroundTask;
  53.  
  54. /*--------------PART 2-------------*/
  55. typedef struct {
  56. Object super;
  57. int bpm;
  58. int key;
  59. char buf[100];
  60. int count;
  61. int songCount;
  62. int playing;
  63. int valueBpm;
  64. int valueKey;
  65. }MelodyClass;
  66.  
  67. void muteMelody(MelodyClass*, int);
  68.  
  69. App app = {initObject(), 0, 'X' };
  70. Add addp = {initObject(), 0, 0, {0}, 0};
  71.  
  72. PeriodicTask toneGen = {initObject(), USEC(500), USEC(100), 0x08, 0x05, 1, 0};
  73. BackgroundTask bt = {initObject(), 1000, USEC(1300), USEC(1300), {0}};
  74. MelodyClass melody = {initObject(), 120, 10, {0}, 0, 0, 0, 0, 0};
  75.  
  76. IFP ifp = {
  77. initObject(),
  78. 0,
  79. {0},
  80. {2024, 1911, 1803, 1702, 1607, 1516, 1431, 1351, 1275, 1203, 1136, 1072, 1012, 955, 901, 851, 803, 758, 715, 675, 637, 601, 568, 536, 506},
  81. {0, 2, 4, 0, 0, 2, 4, 0, 4, 5, 7, 4, 5, 7, 7, 9, 7, 5, 4, 0, 7, 9, 7, 5, 4, 0, 0, -5, 0, 0, -5, 0}
  82. };
  83.  
  84. int period[100] = {2024, 1911, 1803, 1702, 1607, 1516, 1431, 1351, 1275, 1203, 1136, 1072, 1012, 955, 901, 851, 803, 758, 715, 675, 637, 601, 568, 536, 506};
  85. int song[100] = {0, 2, 4, 0, 0, 2, 4, 0, 4, 5, 7, 4, 5, 7, 7, 9, 7, 5, 4, 0, 7, 9, 7, 5, 4, 0, 0, -5, 0, 0, -5, 0};
  86. int beat[100] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 4, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 4, 2, 2, 4};
  87.  
  88. void reader(App*, int);
  89. void receiver(App*, int);
  90. void pulseWave(PeriodicTask*, int);
  91. void playMelody(MelodyClass*, int);
  92.  
  93. /*---------CHANGE LEADER/SLAVE AND CANON/CHORUS---------*/
  94. int leader = 1;
  95. int canon = 1;
  96. #define NODE 1 //Leader 1 = NODE 1, Slave 2 = NODE 2, Slave 3 = NODE 3
  97. #define TO_NODE 2 //Unused
  98. /*------------------------------------------------------*/
  99.  
  100. int bpmWasChanged = 0;
  101.  
  102. Serial sci0 = initSerial(SCI_PORT0, &app, reader);
  103.  
  104. Can can0 = initCan(CAN_PORT0, &app, receiver);
  105.  
  106. void printPeriod(IFP *self, int unused){
  107. char buf[100];
  108.  
  109. for(int i = 0; i < 32; i++){
  110. snprintf(buf, 100, "Period %d: %d\n", i, self->period[self->song[i] + 10]);
  111. SCI_WRITE(&sci0, buf);
  112. }
  113. }
  114.  
  115. void printPeriodKey(IFP *self, char c){
  116. int key, x = 0;
  117.  
  118. if(c == 'e'){
  119. self->buf[x] = '\0';
  120. key = atoi(self->buf);
  121.  
  122. for(int i = 0; i < 32; i++){
  123. snprintf(self->buf, 100, "Period %d: %d\n", i, self->period[self->song[i] + 10 + key]);
  124. SCI_WRITE(&sci0, self->buf);
  125. }
  126. x = 0;
  127. }
  128. else{
  129. self->buf[x] = c;
  130. x++;
  131. }
  132. }
  133.  
  134. void add(Add *addp, int c){
  135. if(c == 'e'){
  136. addp->buf[addp->i] = '\0';
  137. addp->num = atoi(addp->buf);
  138. addp->i = 0;
  139.  
  140. snprintf(addp->buf, 100, "The entered number is: %d\n", addp->num);
  141. SCI_WRITE(&sci0, addp->buf);
  142.  
  143. addp->sum += addp->num;
  144.  
  145. snprintf(addp->buf, 100, "The running sum is: %d\n", addp->sum);
  146. SCI_WRITE(&sci0, addp->buf);
  147. }
  148. else if(c == 'f'){
  149. addp->sum = 0;
  150. snprintf(addp->buf, 100, "The running sum is: %d\n", addp->sum);
  151. SCI_WRITE(&sci0, addp->buf);
  152. }
  153. else{
  154. addp->buf[addp->i] = c;
  155. addp->i++;
  156. }
  157. }
  158.  
  159. void changeVolume(PeriodicTask *self, int c){
  160. if(c == 'h' && self->volume < 20){
  161. self->volume++;
  162. self->volumeSave = self->volume;
  163. }
  164. else if(c == 'l' && self->volume > 1){
  165. self->volume--;
  166. self->volumeSave = self->volume;
  167. }
  168. else if(c == 'm'){
  169. if(self->volume == 0){
  170. self->volume = self->volumeSave;
  171. }
  172. else{
  173. self->volume = 0;
  174. }
  175. }
  176. }
  177.  
  178. void deadlinePeriod(PeriodicTask *self, int unused){
  179. if(self->deadline)
  180. self->deadline = 0;
  181. else
  182. self->deadline = USEC(100);
  183. }
  184.  
  185. void deadlineBt(BackgroundTask *self, int unused){
  186. if(self->deadline)
  187. self->deadline = 0;
  188. else
  189. self->deadline = USEC(1300);
  190. }
  191.  
  192. void changeDistortion(BackgroundTask *self, int c){
  193. if(c == 'y' && self->background_loop_range < 20000){
  194. self->background_loop_range += 500;
  195. }
  196. else if(c == 'o' && self->background_loop_range > 1000){
  197. self->background_loop_range -= 500;
  198. }
  199.  
  200. snprintf(self->buf, 100, "Current loop range: %d\n", self->background_loop_range);
  201. SCI_WRITE(&sci0, self->buf);
  202. }
  203.  
  204. void distortion(BackgroundTask *self, int unused){
  205. for(int i = self->background_loop_range; i >= 0; i--){}
  206. SEND(self->period, self->deadline, self, distortion, unused);
  207. }
  208.  
  209. void changeKeyBpm(MelodyClass *self, int c){
  210.  
  211. CANMsg msg;
  212.  
  213. if(c == 'k'){
  214. self->buf[self->count] = '\0';
  215.  
  216. self->valueKey = atoi(self->buf); //+10
  217.  
  218. if(self->valueKey >= 15){
  219. self->valueKey = 15;
  220. }
  221. else if(self->valueKey <= 5){
  222. self->valueKey = 5;
  223. }
  224.  
  225. if(leader){
  226. msg.nodeId = TO_NODE;
  227. msg.msgId = 2;
  228. msg.length = 1;
  229. msg.buff[0] = self->valueKey;
  230. CAN_SEND(&can0, &msg);
  231. }
  232.  
  233. if(leader)
  234. self->key = self->valueKey;
  235.  
  236. // snprintf(self->buf, 100, "KEY: %d\n", self->key - 10);
  237. // SCI_WRITE(&sci0, self->buf);
  238.  
  239. self->count = 0;
  240. }
  241. else if(c == 'b'){
  242. self->buf[self->count] = '\0';
  243. self->valueBpm = atoi(self->buf);
  244. if(self->valueBpm >= 240){
  245. self->valueBpm = 240;
  246. }
  247. else if(self->valueBpm <= 60){
  248. self->valueBpm = 60;
  249. }
  250. bpmWasChanged = 1;
  251.  
  252. // if(beatCounter % 4 = 0){
  253. // if(leader){
  254. // msg.nodeId = TO_NODE;
  255. // msg.msgId = 3;
  256. // msg.length = 1;
  257. // msg.buff[0] = self->valueBpm;
  258. // CAN_SEND(&can0, &msg);
  259. // }
  260. //
  261. // if(leader)
  262. // self->bpm = self->valueBpm;
  263. // }
  264.  
  265. // snprintf(self->buf, 100, "BPM: %d\n", self->bpm);
  266. // SCI_WRITE(&sci0, self->buf);
  267.  
  268. self->count = 0;
  269. }
  270. else{
  271. self->buf[self->count] = c;
  272. self->count++;
  273. }
  274. }
  275.  
  276. void changePeriod(PeriodicTask *self, int periodIn){
  277. self->period = periodIn;
  278. }
  279.  
  280. void startStop(PeriodicTask *self, int flagValue){
  281. self->flag = flagValue;
  282. }
  283.  
  284. void pause(MelodyClass *self, int unused){
  285. CANMsg msg;
  286. msg.msgId = 1;
  287. msg.length = 1;
  288.  
  289. if(self->playing == 0){
  290. msg.buff[0] = 1;
  291. //SCI_WRITE(&sci0, "PLAYING\n");
  292. if(!canon)
  293. CAN_SEND(&can0, &msg);
  294.  
  295. ASYNC(&melody, playMelody, 0);
  296. self->playing = 1;
  297. }
  298. else if(self->playing == 1){
  299. msg.buff[0] = 0;
  300.  
  301. msg.nodeId = 2;
  302. CAN_SEND(&can0, &msg);
  303. msg.nodeId = 3;
  304. CAN_SEND(&can0, &msg);
  305.  
  306. //SCI_WRITE(&sci0, "STOPPED\n");
  307. self->playing = 0;
  308. }
  309. }
  310.  
  311. int beatCounter = 0;
  312.  
  313. void playMelody(MelodyClass *self, int unused){
  314. int i = 0;
  315.  
  316. //char buff[100];
  317.  
  318. CANMsg msg;
  319. msg.msgId = 1;
  320. msg.length = 1;
  321. msg.buff[0] = 1;
  322.  
  323. if(self->playing){
  324. ASYNC(&toneGen, startStop, 1);
  325. i = self->songCount % 32;
  326.  
  327. if(canon && leader){
  328. beatCounter += beat[self->songCount];
  329.  
  330. if(beatCounter == 10){ //18
  331. msg.nodeId = 2;
  332. //SCI_WRITE(&sci0, "SENDING START TO NODE 2\n");
  333. CAN_SEND(&can0, &msg);
  334. }
  335. if(beatCounter == 18){ //33
  336. msg.nodeId = 3;
  337. //SCI_WRITE(&sci0, "SENDING START TO NODE 3\n");
  338. CAN_SEND(&can0, &msg);
  339. }
  340. }
  341.  
  342. // if(i == 3 || i == 7 || i == 10 || i == 13 || i == 19 || i == 25 || i == 28 || i == 31){
  343. if(i == 3 || i == 7 || i == 10 || i == 13 || i == 19 || i == 25 || i == 28 || i == 31){
  344. if(leader && bpmWasChanged){
  345. msg.nodeId = TO_NODE;
  346. msg.msgId = 3;
  347. msg.length = 1;
  348. msg.buff[0] = self->valueBpm;
  349. CAN_SEND(&can0, &msg);
  350.  
  351. SCI_WRITE(&sci0, "BPM CHANGED \n");
  352. self->bpm = self->valueBpm;
  353. bpmWasChanged = 0;
  354. }
  355. }
  356.  
  357.  
  358. self->songCount++;
  359. ASYNC(&toneGen, changePeriod, USEC(period[song[i] + self->key]));
  360. AFTER(MSEC(beat[i]*60000/self->bpm/2-50), self, muteMelody, unused);
  361. }
  362. else{
  363. self->songCount = 0;
  364. }
  365. }
  366.  
  367. void muteMelody(MelodyClass *self, int unused){
  368. ASYNC(&toneGen, startStop, 0);
  369. AFTER(MSEC(50), self, playMelody, unused);
  370. }
  371.  
  372. void pulseWave(PeriodicTask *self, int start){ // aka toneGen
  373. if(self->flag && start == 1){
  374. if(self->state == 1){
  375. DAC_port = self->volume;
  376. self->state = 0;
  377. }
  378. else{
  379. DAC_port = 0;
  380. self->state = 1;
  381. }
  382. }
  383.  
  384. SEND(self->period, self->deadline, self, pulseWave, 1);
  385. }
  386.  
  387. void slaveKey(MelodyClass *self, int c){
  388. self->key = c;
  389. }
  390.  
  391. void slaveBpm(MelodyClass *self, int c){
  392. self->bpm = c;
  393. }
  394.  
  395. void startStopCAN(MelodyClass *self, int c){
  396. if(c == 1){
  397. self->playing = 1;
  398. ASYNC(&melody, playMelody, 0);
  399. }else{
  400. self->playing = 0;
  401. }
  402. }
  403.  
  404. void reader(App *self, int c) {
  405. SCI_WRITE(&sci0, "Rcv: \'");
  406. SCI_WRITECHAR(&sci0, c);
  407. SCI_WRITE(&sci0, "\'\n");
  408.  
  409.  
  410. if(c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9' || c == '0' || c == 'k' || c == 'b' || c == '-')
  411. ASYNC(&melody, changeKeyBpm, c);
  412.  
  413. if(c == 'y' || c == 'o')
  414. ASYNC(&bt, changeDistortion, c);
  415.  
  416. if(c == 'h' || c == 'l' || c == 'm')
  417. ASYNC(&toneGen, changeVolume, c);
  418.  
  419. if(c == 's'){
  420. beatCounter = 0;
  421. ASYNC(&melody, pause, 0);
  422. }
  423. }
  424.  
  425. void receiver(App *self, int unused) {
  426. CANMsg msg;
  427. CAN_RECEIVE(&can0, &msg);
  428.  
  429. int c = msg.buff[0];
  430.  
  431. if(!leader){
  432. if(msg.msgId == 1 && msg.nodeId == NODE){
  433. ASYNC(&melody, startStopCAN, c);
  434. }
  435. else if(msg.msgId == 2)
  436. ASYNC(&melody, slaveKey, c);
  437. else if(msg.msgId == 3)
  438. ASYNC(&melody, slaveBpm, c);
  439. }
  440.  
  441.  
  442. char buf[50];
  443.  
  444. // if(msg.msgId == 1){
  445. // SCI_WRITE(&sci0, "STATUS RECEIVED \n");
  446. // snprintf(buf, 50, "CAN STATUS: %d\n", msg.buff[0]);
  447. // SCI_WRITE(&sci0, buf);
  448. // }
  449. //
  450. // if(msg.msgId == 2){
  451. // SCI_WRITE(&sci0, "KEY RECEIVED \n");
  452. // snprintf(buf, 50, "CAN KEY: %d\n", msg.buff[0]);
  453. // SCI_WRITE(&sci0, buf);
  454. // }
  455. //
  456. if(msg.msgId == 3){
  457. SCI_WRITE(&sci0, "BPM RECEIVED \n");
  458.  
  459. snprintf(buf, 50, "CAN BPM: %d\n", msg.buff[0]);
  460. SCI_WRITE(&sci0, buf);
  461. }
  462. }
  463.  
  464. void startApp(App *self, int arg) {
  465. //CANMsg msg;
  466.  
  467. CAN_INIT(&can0);
  468. SCI_INIT(&sci0);
  469. SCI_WRITE(&sci0, "Hello, hello...\n");
  470.  
  471.  
  472. ASYNC(&bt, distortion, 0);
  473. ASYNC(&toneGen, pulseWave, 0);
  474. }
  475.  
  476. int main() {
  477. INSTALL(&sci0, sci_interrupt, SCI_IRQ0);
  478. INSTALL(&can0, can_interrupt, CAN_IRQ0);
  479. TINYTIMBER(&app, startApp, 0);
  480. return 0;
  481. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement