Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "TinyTimber.h"
- #include "sciTinyTimber.h"
- #include "canTinyTimber.h"
- #include <stdlib.h>
- #include <stdio.h>
- //{-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},
- //{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},
- #define DAC_port *((unsigned volatile char *) 0x4000741C)
- typedef struct {
- Object super;
- int count;
- char c;
- } App;
- /*--------------PART 0-------------*/
- typedef struct {
- Object super;
- int num;
- int sum;
- char buf[100];
- int i;
- } Add;
- typedef struct {
- Object super;
- int x;
- char buf[100];
- int period[100];
- int song[100];
- } IFP;
- /*--------------PART 1-------------*/
- typedef struct {
- Object super;
- Time period;
- Time deadline;
- int volume; //HEX
- int volumeSave;
- int state;
- int flag;
- }PeriodicTask;
- typedef struct {
- Object super;
- int background_loop_range;
- Time period;
- Time deadline;
- char buf[100];
- }BackgroundTask;
- /*--------------PART 2-------------*/
- typedef struct {
- Object super;
- int bpm;
- int key;
- char buf[100];
- int count;
- int songCount;
- int playing;
- int valueBpm;
- int valueKey;
- }MelodyClass;
- void muteMelody(MelodyClass*, int);
- App app = {initObject(), 0, 'X' };
- Add addp = {initObject(), 0, 0, {0}, 0};
- PeriodicTask toneGen = {initObject(), USEC(500), USEC(100), 0x08, 0x05, 1, 0};
- BackgroundTask bt = {initObject(), 1000, USEC(1300), USEC(1300), {0}};
- MelodyClass melody = {initObject(), 120, 10, {0}, 0, 0, 0, 0, 0};
- IFP ifp = {
- initObject(),
- 0,
- {0},
- {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},
- {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}
- };
- 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};
- 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};
- 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};
- void reader(App*, int);
- void receiver(App*, int);
- void pulseWave(PeriodicTask*, int);
- void playMelody(MelodyClass*, int);
- /*---------CHANGE LEADER/SLAVE AND CANON/CHORUS---------*/
- int leader = 1;
- int canon = 1;
- #define NODE 1 //Leader 1 = NODE 1, Slave 2 = NODE 2, Slave 3 = NODE 3
- #define TO_NODE 2 //Unused
- /*------------------------------------------------------*/
- int bpmWasChanged = 0;
- Serial sci0 = initSerial(SCI_PORT0, &app, reader);
- Can can0 = initCan(CAN_PORT0, &app, receiver);
- void printPeriod(IFP *self, int unused){
- char buf[100];
- for(int i = 0; i < 32; i++){
- snprintf(buf, 100, "Period %d: %d\n", i, self->period[self->song[i] + 10]);
- SCI_WRITE(&sci0, buf);
- }
- }
- void printPeriodKey(IFP *self, char c){
- int key, x = 0;
- if(c == 'e'){
- self->buf[x] = '\0';
- key = atoi(self->buf);
- for(int i = 0; i < 32; i++){
- snprintf(self->buf, 100, "Period %d: %d\n", i, self->period[self->song[i] + 10 + key]);
- SCI_WRITE(&sci0, self->buf);
- }
- x = 0;
- }
- else{
- self->buf[x] = c;
- x++;
- }
- }
- void add(Add *addp, int c){
- if(c == 'e'){
- addp->buf[addp->i] = '\0';
- addp->num = atoi(addp->buf);
- addp->i = 0;
- snprintf(addp->buf, 100, "The entered number is: %d\n", addp->num);
- SCI_WRITE(&sci0, addp->buf);
- addp->sum += addp->num;
- snprintf(addp->buf, 100, "The running sum is: %d\n", addp->sum);
- SCI_WRITE(&sci0, addp->buf);
- }
- else if(c == 'f'){
- addp->sum = 0;
- snprintf(addp->buf, 100, "The running sum is: %d\n", addp->sum);
- SCI_WRITE(&sci0, addp->buf);
- }
- else{
- addp->buf[addp->i] = c;
- addp->i++;
- }
- }
- void changeVolume(PeriodicTask *self, int c){
- if(c == 'h' && self->volume < 20){
- self->volume++;
- self->volumeSave = self->volume;
- }
- else if(c == 'l' && self->volume > 1){
- self->volume--;
- self->volumeSave = self->volume;
- }
- else if(c == 'm'){
- if(self->volume == 0){
- self->volume = self->volumeSave;
- }
- else{
- self->volume = 0;
- }
- }
- }
- void deadlinePeriod(PeriodicTask *self, int unused){
- if(self->deadline)
- self->deadline = 0;
- else
- self->deadline = USEC(100);
- }
- void deadlineBt(BackgroundTask *self, int unused){
- if(self->deadline)
- self->deadline = 0;
- else
- self->deadline = USEC(1300);
- }
- void changeDistortion(BackgroundTask *self, int c){
- if(c == 'y' && self->background_loop_range < 20000){
- self->background_loop_range += 500;
- }
- else if(c == 'o' && self->background_loop_range > 1000){
- self->background_loop_range -= 500;
- }
- snprintf(self->buf, 100, "Current loop range: %d\n", self->background_loop_range);
- SCI_WRITE(&sci0, self->buf);
- }
- void distortion(BackgroundTask *self, int unused){
- for(int i = self->background_loop_range; i >= 0; i--){}
- SEND(self->period, self->deadline, self, distortion, unused);
- }
- void changeKeyBpm(MelodyClass *self, int c){
- CANMsg msg;
- if(c == 'k'){
- self->buf[self->count] = '\0';
- self->valueKey = atoi(self->buf); //+10
- if(self->valueKey >= 15){
- self->valueKey = 15;
- }
- else if(self->valueKey <= 5){
- self->valueKey = 5;
- }
- if(leader){
- msg.nodeId = TO_NODE;
- msg.msgId = 2;
- msg.length = 1;
- msg.buff[0] = self->valueKey;
- CAN_SEND(&can0, &msg);
- }
- if(leader)
- self->key = self->valueKey;
- // snprintf(self->buf, 100, "KEY: %d\n", self->key - 10);
- // SCI_WRITE(&sci0, self->buf);
- self->count = 0;
- }
- else if(c == 'b'){
- self->buf[self->count] = '\0';
- self->valueBpm = atoi(self->buf);
- if(self->valueBpm >= 240){
- self->valueBpm = 240;
- }
- else if(self->valueBpm <= 60){
- self->valueBpm = 60;
- }
- bpmWasChanged = 1;
- // if(beatCounter % 4 = 0){
- // if(leader){
- // msg.nodeId = TO_NODE;
- // msg.msgId = 3;
- // msg.length = 1;
- // msg.buff[0] = self->valueBpm;
- // CAN_SEND(&can0, &msg);
- // }
- //
- // if(leader)
- // self->bpm = self->valueBpm;
- // }
- // snprintf(self->buf, 100, "BPM: %d\n", self->bpm);
- // SCI_WRITE(&sci0, self->buf);
- self->count = 0;
- }
- else{
- self->buf[self->count] = c;
- self->count++;
- }
- }
- void changePeriod(PeriodicTask *self, int periodIn){
- self->period = periodIn;
- }
- void startStop(PeriodicTask *self, int flagValue){
- self->flag = flagValue;
- }
- void pause(MelodyClass *self, int unused){
- CANMsg msg;
- msg.msgId = 1;
- msg.length = 1;
- if(self->playing == 0){
- msg.buff[0] = 1;
- //SCI_WRITE(&sci0, "PLAYING\n");
- if(!canon)
- CAN_SEND(&can0, &msg);
- ASYNC(&melody, playMelody, 0);
- self->playing = 1;
- }
- else if(self->playing == 1){
- msg.buff[0] = 0;
- msg.nodeId = 2;
- CAN_SEND(&can0, &msg);
- msg.nodeId = 3;
- CAN_SEND(&can0, &msg);
- //SCI_WRITE(&sci0, "STOPPED\n");
- self->playing = 0;
- }
- }
- int beatCounter = 0;
- void playMelody(MelodyClass *self, int unused){
- int i = 0;
- //char buff[100];
- CANMsg msg;
- msg.msgId = 1;
- msg.length = 1;
- msg.buff[0] = 1;
- if(self->playing){
- ASYNC(&toneGen, startStop, 1);
- i = self->songCount % 32;
- if(canon && leader){
- beatCounter += beat[self->songCount];
- if(beatCounter == 10){ //18
- msg.nodeId = 2;
- //SCI_WRITE(&sci0, "SENDING START TO NODE 2\n");
- CAN_SEND(&can0, &msg);
- }
- if(beatCounter == 18){ //33
- msg.nodeId = 3;
- //SCI_WRITE(&sci0, "SENDING START TO NODE 3\n");
- CAN_SEND(&can0, &msg);
- }
- }
- // if(i == 3 || i == 7 || i == 10 || i == 13 || i == 19 || i == 25 || i == 28 || i == 31){
- if(i == 3 || i == 7 || i == 10 || i == 13 || i == 19 || i == 25 || i == 28 || i == 31){
- if(leader && bpmWasChanged){
- msg.nodeId = TO_NODE;
- msg.msgId = 3;
- msg.length = 1;
- msg.buff[0] = self->valueBpm;
- CAN_SEND(&can0, &msg);
- SCI_WRITE(&sci0, "BPM CHANGED \n");
- self->bpm = self->valueBpm;
- bpmWasChanged = 0;
- }
- }
- self->songCount++;
- ASYNC(&toneGen, changePeriod, USEC(period[song[i] + self->key]));
- AFTER(MSEC(beat[i]*60000/self->bpm/2-50), self, muteMelody, unused);
- }
- else{
- self->songCount = 0;
- }
- }
- void muteMelody(MelodyClass *self, int unused){
- ASYNC(&toneGen, startStop, 0);
- AFTER(MSEC(50), self, playMelody, unused);
- }
- void pulseWave(PeriodicTask *self, int start){ // aka toneGen
- if(self->flag && start == 1){
- if(self->state == 1){
- DAC_port = self->volume;
- self->state = 0;
- }
- else{
- DAC_port = 0;
- self->state = 1;
- }
- }
- SEND(self->period, self->deadline, self, pulseWave, 1);
- }
- void slaveKey(MelodyClass *self, int c){
- self->key = c;
- }
- void slaveBpm(MelodyClass *self, int c){
- self->bpm = c;
- }
- void startStopCAN(MelodyClass *self, int c){
- if(c == 1){
- self->playing = 1;
- ASYNC(&melody, playMelody, 0);
- }else{
- self->playing = 0;
- }
- }
- void reader(App *self, int c) {
- SCI_WRITE(&sci0, "Rcv: \'");
- SCI_WRITECHAR(&sci0, c);
- SCI_WRITE(&sci0, "\'\n");
- 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 == '-')
- ASYNC(&melody, changeKeyBpm, c);
- if(c == 'y' || c == 'o')
- ASYNC(&bt, changeDistortion, c);
- if(c == 'h' || c == 'l' || c == 'm')
- ASYNC(&toneGen, changeVolume, c);
- if(c == 's'){
- beatCounter = 0;
- ASYNC(&melody, pause, 0);
- }
- }
- void receiver(App *self, int unused) {
- CANMsg msg;
- CAN_RECEIVE(&can0, &msg);
- int c = msg.buff[0];
- if(!leader){
- if(msg.msgId == 1 && msg.nodeId == NODE){
- ASYNC(&melody, startStopCAN, c);
- }
- else if(msg.msgId == 2)
- ASYNC(&melody, slaveKey, c);
- else if(msg.msgId == 3)
- ASYNC(&melody, slaveBpm, c);
- }
- char buf[50];
- // if(msg.msgId == 1){
- // SCI_WRITE(&sci0, "STATUS RECEIVED \n");
- // snprintf(buf, 50, "CAN STATUS: %d\n", msg.buff[0]);
- // SCI_WRITE(&sci0, buf);
- // }
- //
- // if(msg.msgId == 2){
- // SCI_WRITE(&sci0, "KEY RECEIVED \n");
- // snprintf(buf, 50, "CAN KEY: %d\n", msg.buff[0]);
- // SCI_WRITE(&sci0, buf);
- // }
- //
- if(msg.msgId == 3){
- SCI_WRITE(&sci0, "BPM RECEIVED \n");
- snprintf(buf, 50, "CAN BPM: %d\n", msg.buff[0]);
- SCI_WRITE(&sci0, buf);
- }
- }
- void startApp(App *self, int arg) {
- //CANMsg msg;
- CAN_INIT(&can0);
- SCI_INIT(&sci0);
- SCI_WRITE(&sci0, "Hello, hello...\n");
- ASYNC(&bt, distortion, 0);
- ASYNC(&toneGen, pulseWave, 0);
- }
- int main() {
- INSTALL(&sci0, sci_interrupt, SCI_IRQ0);
- INSTALL(&can0, can_interrupt, CAN_IRQ0);
- TINYTIMBER(&app, startApp, 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement