Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
RTS-Lab.code-workspace
Debug/
231 changes: 201 additions & 30 deletions application.c
Original file line number Diff line number Diff line change
@@ -1,59 +1,230 @@
#include "TinyTimber.h"
#include "sciTinyTimber.h"
#include "canTinyTimber.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
/*
* 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 {
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' };
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};
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};

void reader(App*, int);
void receiver(App*, int);
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);
void printhist(int[], int);

Can can0 = initCan(CAN_PORT0, &app, receiver);
App app = {initObject(), 0, 0, 0, {}, {}};
Serial sci0 = initSerial(SCI_PORT0, &app, reader);

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 (keybool)
{
// procedure for step6
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
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':
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");
keybool = true; // next interger is saved as the key and not saved in history buffer
break;


}
}

// 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()
*/
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;
}
}

/*
* function that returns the median of numbers
*/
int median(int history[], int count)
{
int history1[3];
if (count == 1)
{
return history[0];
}
else if (count == 2)
{
return (history[0] + history[1]) / 2;
}
else
{
history1[0] = history[0];
history1[1] = history[1];
history1[2] = history[2];
sort(history1);
return history1[1];
}
}

/*
* 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++)
{
sum += a[i];
}
return sum;

}

/*
* 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];
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];
sprintf(tempBuffer, "%d ", period);
SCI_WRITE(&sci0, tempBuffer);
}
SCI_WRITE(&sci0, "\n");


}

void startApp(App *self, int arg) {
CANMsg msg;
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;
}