From a62a795f171247ffdfdd3fadaf2db86a5c3af8a2 Mon Sep 17 00:00:00 2001 From: Ping Zheng Date: Thu, 2 Feb 2023 20:31:44 +0100 Subject: [PATCH 1/5] p0S2 done --- .gitignore | 2 + application.c | 134 +++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 106 insertions(+), 30 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a67c794 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +RTS-Lab.code-workspace +Debug/ \ No newline at end of file diff --git a/application.c b/application.c index a0aa821..c465ccc 100644 --- a/application.c +++ b/application.c @@ -1,59 +1,133 @@ #include "TinyTimber.h" #include "sciTinyTimber.h" -#include "canTinyTimber.h" #include #include -typedef struct { +typedef struct +{ Object super; int count; - char c; + int index; + int index1; //index for intHistory[] + char buffer[50]; + int intHistory[3]; } App; -App app = { initObject(), 0, 'X' }; +App app = {initObject(), 0, 0, 0, {}, {}}; -void reader(App*, int); -void receiver(App*, int); +void reader(App *, int); +void receiver(App *, int); +int median(int[], int); +int sum(int[], int); Serial sci0 = initSerial(SCI_PORT0, &app, reader); -Can can0 = initCan(CAN_PORT0, &app, receiver); - -void receiver(App *self, int unused) { - CANMsg msg; - CAN_RECEIVE(&can0, &msg); - SCI_WRITE(&sci0, "Can msg received: "); - SCI_WRITE(&sci0, msg.buff); +void receiver(App *self, int unused) +{ } -void reader(App *self, int c) { +void reader(App *self, int c) +{ + int bufferValue; + char tempBuffer[50]; SCI_WRITE(&sci0, "Rcv: \'"); SCI_WRITECHAR(&sci0, c); SCI_WRITE(&sci0, "\'\n"); + + switch (c) + { + case '0' ... '9': + case '-': + self->buffer[self->index++] = c; + break; + case 'e': + self->buffer[self->index] = '\0'; + self->index = 0; + bufferValue = atoi(self->buffer); + if (self->count+1 > 3) + { + //eliminate the oldest one and add the int to that position + self->index1 = (self->index1 + 1) % 3; + self->intHistory[self->index1] = bufferValue; + } + // <=3 + self->intHistory[self->index1] = bufferValue; + self->index1++; + self->count++; + sprintf(tempBuffer, "Entered integer: %d, sum = %d, median = %d\n", bufferValue, sum(self->intHistory, self->count), median(self->intHistory, self->count)); + SCI_WRITE(&sci0, tempBuffer); + break; + case 'F': + self->count = 0; + self->index1 = 0; + SCI_WRITE(&sci0, "The 3-history has been erased\n"); + + + } } -void startApp(App *self, int arg) { - CANMsg msg; +// +// +void sort(int *history) +{ + int i, j, temp, m; + m = 3; + for (i = 0; i < 3; i++) + { + int exchange = 0; + for (j = 0; j < m - 1; j++) + { + if (history[j] > history[j + 1]) + { + temp = history[j]; + history[j] = history[j + 1]; + history[j + 1] = temp; + exchange = 1; + } + } + m--; + if (!exchange) + break; + } +} + +int median(int history[], int count) +{ + if (count == 1) + { + return history[0]; + } + else if (count == 2) + { + return (history[0] + history[1]) / 2; + } + else + { + sort(history); + return history[1]; + } +} + +int sum(int a[], int len) { + int sum = 0; + for (size_t i = 0; i < len; i++) + { + sum += a[i]; + } + return sum; + +} + +void startApp(App *self, int arg) +{ - CAN_INIT(&can0); SCI_INIT(&sci0); SCI_WRITE(&sci0, "Hello, hello...\n"); - - msg.msgId = 1; - msg.nodeId = 1; - msg.length = 6; - msg.buff[0] = 'H'; - msg.buff[1] = 'e'; - msg.buff[2] = 'l'; - msg.buff[3] = 'l'; - msg.buff[4] = 'o'; - msg.buff[5] = 0; - CAN_SEND(&can0, &msg); } -int main() { +int main() +{ INSTALL(&sci0, sci_interrupt, SCI_IRQ0); - INSTALL(&can0, can_interrupt, CAN_IRQ0); TINYTIMBER(&app, startApp, 0); return 0; } From 854c10b3b829c54c07605c9609f869a613b4f899 Mon Sep 17 00:00:00 2001 From: Ping Zheng Date: Fri, 3 Feb 2023 18:24:09 +0100 Subject: [PATCH 2/5] add Step 6, all part0 is done --- application.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/application.c b/application.c index c465ccc..46bb254 100644 --- a/application.c +++ b/application.c @@ -2,6 +2,7 @@ #include "sciTinyTimber.h" #include #include +#include typedef struct { @@ -13,12 +14,19 @@ typedef struct int intHistory[3]; } App; +bool key = false; // Step 6 + +int frequency_indices[32] = {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}; +// us +int periods[] = {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}; + App app = {initObject(), 0, 0, 0, {}, {}}; void reader(App *, int); void receiver(App *, int); int median(int[], int); int sum(int[], int); +void period_lookup(int); Serial sci0 = initSerial(SCI_PORT0, &app, reader); @@ -44,6 +52,19 @@ void reader(App *self, int c) self->buffer[self->index] = '\0'; self->index = 0; bufferValue = atoi(self->buffer); + if (key) + { + // the function + if (bufferValue<-5 || bufferValue > 5) + { + SCI_WRITE(&sci0, " -5<=key<=5, try again!\n"); + break; + } + + period_lookup(bufferValue); + break; + } + if (self->count+1 > 3) { //eliminate the oldest one and add the int to that position @@ -61,13 +82,18 @@ void reader(App *self, int c) self->count = 0; self->index1 = 0; SCI_WRITE(&sci0, "The 3-history has been erased\n"); + break; + case 'K': + SCI_WRITE(&sci0, "Please input the key(-5~5) you want:\n"); + key = true; // next interger is for the key + break; } } // -// +// void sort(int *history) { int i, j, temp, m; @@ -91,6 +117,8 @@ void sort(int *history) } } +// +// int median(int history[], int count) { if (count == 1) @@ -118,6 +146,24 @@ int sum(int a[], int len) { } +void period_lookup(int key){ //step 6 + char tempBuffer[50]; + sprintf(tempBuffer, "Key: %d\n", key); + SCI_WRITE(&sci0, tempBuffer); + int frequency_index; + int period; + for (size_t i = 0; i < 32; i++) + { + frequency_index = frequency_indices[i] + key; + period = periods[frequency_index+10]; + sprintf(tempBuffer, "%d ", period); + SCI_WRITE(&sci0, tempBuffer); + } + SCI_WRITE(&sci0, "\n"); + +} +// +// void startApp(App *self, int arg) { From 96fabca3a6daf7b7aee8b5a9d93bface37ebbca2 Mon Sep 17 00:00:00 2001 From: Ping Zheng Date: Fri, 3 Feb 2023 19:29:29 +0100 Subject: [PATCH 3/5] bui --- application.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/application.c b/application.c index 46bb254..9c2d394 100644 --- a/application.c +++ b/application.c @@ -14,7 +14,7 @@ typedef struct int intHistory[3]; } App; -bool key = false; // Step 6 +bool keybool = false; // Step 6 int frequency_indices[32] = {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}; // us @@ -52,7 +52,7 @@ void reader(App *self, int c) self->buffer[self->index] = '\0'; self->index = 0; bufferValue = atoi(self->buffer); - if (key) + if (keybool) { // the function if (bufferValue<-5 || bufferValue > 5) @@ -85,7 +85,7 @@ void reader(App *self, int c) break; case 'K': SCI_WRITE(&sci0, "Please input the key(-5~5) you want:\n"); - key = true; // next interger is for the key + keybool = true; // next interger is for the key break; @@ -147,12 +147,20 @@ int sum(int a[], int len) { } void period_lookup(int key){ //step 6 + keybool = false; char tempBuffer[50]; sprintf(tempBuffer, "Key: %d\n", key); SCI_WRITE(&sci0, tempBuffer); int frequency_index; int period; for (size_t i = 0; i < 32; i++) + { + frequency_index = frequency_indices[i]; + sprintf(tempBuffer, "%d ", frequency_index); + SCI_WRITE(&sci0, tempBuffer); + } + SCI_WRITE(&sci0, "\n"); + for (size_t i = 0; i < 32; i++) { frequency_index = frequency_indices[i] + key; period = periods[frequency_index+10]; @@ -161,6 +169,7 @@ void period_lookup(int key){ //step 6 } SCI_WRITE(&sci0, "\n"); + } // // From 12523e50d6600e344afc2bf60c497d2346103000 Mon Sep 17 00:00:00 2001 From: Ping Zheng Date: Tue, 7 Feb 2023 16:32:50 +0100 Subject: [PATCH 4/5] commented --- application.c | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/application.c b/application.c index 9c2d394..53c7b53 100644 --- a/application.c +++ b/application.c @@ -15,19 +15,16 @@ typedef struct } App; bool keybool = false; // Step 6 - int frequency_indices[32] = {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}; -// us int periods[] = {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}; -App app = {initObject(), 0, 0, 0, {}, {}}; - void reader(App *, int); void receiver(App *, int); int median(int[], int); int sum(int[], int); void period_lookup(int); +App app = {initObject(), 0, 0, 0, {}, {}}; Serial sci0 = initSerial(SCI_PORT0, &app, reader); void receiver(App *self, int unused) @@ -54,7 +51,7 @@ void reader(App *self, int c) bufferValue = atoi(self->buffer); if (keybool) { - // the function + // procedure for step6 if (bufferValue<-5 || bufferValue > 5) { SCI_WRITE(&sci0, " -5<=key<=5, try again!\n"); @@ -85,15 +82,17 @@ void reader(App *self, int c) break; case 'K': SCI_WRITE(&sci0, "Please input the key(-5~5) you want:\n"); - keybool = true; // next interger is for the key + keybool = true; // next interger is saved as the key and not saved in history buffer break; } } -// -// +/* + * Bubble sort + * helper function for median() + */ void sort(int *history) { int i, j, temp, m; @@ -117,8 +116,9 @@ void sort(int *history) } } -// -// +/* + * function that returns the median of numbers + */ int median(int history[], int count) { if (count == 1) @@ -136,6 +136,14 @@ int median(int history[], int count) } } +/* + * sum the integers in the queue + * + * parameters: a[] is the history buffer, len is length of the buffer + * + * return the sum + * + */ int sum(int a[], int len) { int sum = 0; for (size_t i = 0; i < len; i++) @@ -146,6 +154,12 @@ int sum(int a[], int len) { } +/* + * takes the key as input from the keyboard (in the form of an integer number) and prints the + * periods corresponding to the 32 frequency indices of the Brother John melody + * for the input key. + * + * */ void period_lookup(int key){ //step 6 keybool = false; char tempBuffer[50]; @@ -171,8 +185,7 @@ void period_lookup(int key){ //step 6 } -// -// + void startApp(App *self, int arg) { From e45a51292d79a60efa0bc83583ebe54e70187ee7 Mon Sep 17 00:00:00 2001 From: Ping Zheng Date: Wed, 8 Feb 2023 21:38:32 +0100 Subject: [PATCH 5/5] debug --- application.c | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/application.c b/application.c index 53c7b53..777bbb8 100644 --- a/application.c +++ b/application.c @@ -3,6 +3,13 @@ #include #include #include +/* + * F : input F to erase the buffer + * + * K : input K to indicate the next number will be used as key and print out the frequency indexs and periods + * + * e: end every number with e +*/ typedef struct { @@ -24,6 +31,8 @@ int median(int[], int); int sum(int[], int); void period_lookup(int); +void printhist(int[], int); + App app = {initObject(), 0, 0, 0, {}, {}}; Serial sci0 = initSerial(SCI_PORT0, &app, reader); @@ -65,14 +74,17 @@ void reader(App *self, int c) if (self->count+1 > 3) { //eliminate the oldest one and add the int to that position - self->index1 = (self->index1 + 1) % 3; + self->index1 = (self->index1) % 3; self->intHistory[self->index1] = bufferValue; - } + self->count = 2; + } // <=3 self->intHistory[self->index1] = bufferValue; self->index1++; self->count++; + // printhist(self->intHistory, self->count); sprintf(tempBuffer, "Entered integer: %d, sum = %d, median = %d\n", bufferValue, sum(self->intHistory, self->count), median(self->intHistory, self->count)); + // printhist(self->intHistory,self->count); SCI_WRITE(&sci0, tempBuffer); break; case 'F': @@ -89,6 +101,19 @@ void reader(App *self, int c) } } +// debug + +// void printhist(int a[], int cout) { +// char tempBuffer[50]; +// for (size_t i = 0; i < cout; i++) +// { +// sprintf(tempBuffer, "in buffer %d:%d \n",i, a[i]); +// SCI_WRITE(&sci0, tempBuffer); +// } + + +// } + /* * Bubble sort * helper function for median() @@ -121,6 +146,7 @@ void sort(int *history) */ int median(int history[], int count) { + int history1[3]; if (count == 1) { return history[0]; @@ -131,8 +157,11 @@ int median(int history[], int count) } else { - sort(history); - return history[1]; + history1[0] = history[0]; + history1[1] = history[1]; + history1[2] = history[2]; + sort(history1); + return history1[1]; } }