From 1d4d0cf5aa37f3887c29457d19dcd35198bba047 Mon Sep 17 00:00:00 2001 From: Udaya Date: Wed, 5 Apr 2017 04:33:16 -0400 Subject: [PATCH 1/2] tested dstack, median. Need to test with grid-eye count --- GccBoardProject1/src/main.c | 75 ++++++--------- GccBoardProject1/src/numpyInC.c | 84 ++++++++--------- GccBoardProject1/src/numpyInC.h | 23 +++++ GccBoardProject1/src/peopleCounting.c | 131 +++++++++++++++----------- GccBoardProject1/src/peopleCounting.h | 28 +++--- 5 files changed, 187 insertions(+), 154 deletions(-) create mode 100644 GccBoardProject1/src/numpyInC.h diff --git a/GccBoardProject1/src/main.c b/GccBoardProject1/src/main.c index f53ddc9..e1dd624 100755 --- a/GccBoardProject1/src/main.c +++ b/GccBoardProject1/src/main.c @@ -35,47 +35,34 @@ #include #include #include -#include "numpyInC.c" +#include static uint16_t PIXEL_BUFFER[NUM_PIXELS]; -static uint16_t * COUNT_BUFF[COUNT_BUFF_SIZE]; - -/* -struct usart_module usart_instance; - -void init_uart(void); -void uart_out(const uint8_t *string, uint16_t length); +static uint16_t frame1[NUM_PIXELS]; +static uint16_t* MEDIAN_FILTER_POINTERS_BUFF[MEDIAN_FILTER_POINTERS_BUFF_SIZE]; +static uint16_t frame[NUM_RAW_FRAMES][NUM_PIXELS]; +static uint16_t COUNT_BUFF[COUNT_BUFF_SIZE]; +static uint16_t frame_conv[GRID_SIZE][GRID_SIZE]; +static struct keepCount count; -void init_uart() { - struct usart_config config_usart; - usart_get_config_defaults(&config_usart); - - config_usart.baudrate = 9600; - config_usart.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING; - config_usart.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0; - config_usart.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1; - config_usart.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2; - config_usart.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3; - - while (usart_init(&usart_instance, EDBG_CDC_MODULE, &config_usart) != STATUS_OK) { +void median_buffer_init(uint16_t* MEDIAN_FILTER_POINTERS_BUFF[], uint16_t** frame) { + for (int i = 0; i < NUM_RAW_FRAMES; i++) { + MEDIAN_FILTER_POINTERS_BUFF[i] = &frame[i]; } - - usart_enable(&usart_instance); + return; } -void uart_out(const uint8_t *string, uint16_t length) { - while(usart_write_buffer_wait(&usart_instance, string, length) != STATUS_OK){} -} -*/ -/* + + int main (void) { system_init(); init_uart(); init_grideye(); delay_init(); + median_buffer_init(MEDIAN_FILTER_POINTERS_BUFF, frame); + - struct keepCount * count; count->countIn = 0; count->countOut = 0; uint16_t frameCount = 0; @@ -83,14 +70,14 @@ int main (void) { count->TRIGGER_COLUNM[0][0] = TRIGGER_COLUNM_2; count->TRIGGER_COLUNM[0][1] = CHECK_OFFSET_2; count->TRIGGER_COLUNM[1][0] = TRIGGER_COLUNM_5; - count->TRIGGER_COLUNM[1][1] = CHECk_OFFSET_5; - count->counted = False; + count->TRIGGER_COLUNM[1][1] = CHECK_OFFSET_5; + count->counted = false; /* Insert application code here, after the board has been initialized. */ - /* This skeleton code simply sets the LED to the state of the button. + /* This skeleton code simply sets the LED to the state of the button. */ while (1) { - /* Is button pressed? + /* Is button pressed? */ if (port_pin_get_input_level(BUTTON_0_PIN) == BUTTON_0_ACTIVE) { if (ge_is_sleeping()) { ge_set_mode(GE_MODE_NORMAL); @@ -117,22 +104,16 @@ int main (void) { } buffer[size-1] = '\r'; buffer[size] = '\n'; - //frameCount++; - //count = countPeople(count, PIXEL_BUFFER, frameCount, COUNT_BUFF, lastFrame); + frameCount++; + if (frameCount <= NUM_RAW_FRAMES) { + initialize_mem(PIXEL_BUFFER, frameCount); + } else { + dstack(MEDIAN_FILTER_POINTERS_BUFF, PIXEL_BUFFER);; + } + uint16_t *frame_median = median_frame(MEDIAN_FILTER_POINTERS_BUFF, frame1); + + count = countPeople(&count, frame_median, frameCount, COUNT_BUFF, lastFrame); uart_out(buffer, size+1); } } } -*/ -int main(void) { - system_init(); - init_uart(); - uint8_t debug[] = "is this printing\r\n"; - uart_out(debug,sizeof(debug)-1); - uint8_t array[] = "123"; - uint8_t b[] = "456"; - uint8_t ** C; - C = dstack(array, b); - uart_out(C[0], sizeof(C[0]) - 1); - return 0; -} \ No newline at end of file diff --git a/GccBoardProject1/src/numpyInC.c b/GccBoardProject1/src/numpyInC.c index f414fee..5806a88 100644 --- a/GccBoardProject1/src/numpyInC.c +++ b/GccBoardProject1/src/numpyInC.c @@ -7,8 +7,11 @@ #include #include +#include struct usart_module usart_instance; +static uint16_t frame_median[NUM_PIXELS]; +static uint16_t array[NUM_RAW_FRAMES]; void init_uart(void); void uart_out(const uint8_t *string, uint16_t length); @@ -36,53 +39,50 @@ void uart_out(const uint8_t *string, uint16_t length) { } -int * fromString(char* data, const char* delim) { - int * array; - char * pt = strtok(data, delim); - uint8_t debug1[] = "NOW is this printing\r\n"; - uart_out(debug1, sizeof(debug1) - 1); - if (pt != NULL) { - uint8_t debug2[] = "is this printing\r\n"; - uart_out(debug2, sizeof(debug2) - 1); - }else { - uint8_t debug3[] = "NOW 2 is this printing\r\n"; - uart_out(debug3, sizeof(debug3) - 1); +void dstack(uint16_t * MEDIAN_FILTER_POINTERS_BUFF[], uint16_t PIXEL_BUFFER[]) { + uint16_t *oldFrame= MEDIAN_FILTER_POINTERS_BUFF[0]; + for (int i = 0; i < NUM_PIXELS; i++){ + oldFrame[i] = PIXEL_BUFFER[i]; } - uart_out((uint8_t *) pt, sizeof(pt) - 1); - int i = 0; - while (pt != NULL) { - array[i] = atoi(pt); - uint8_t msg = array[i]; - uart_out(msg, sizeof(msg) - 1); - pt = strtok(NULL, delim); + for (int i = 0; i < MEDIAN_FILTER_POINTERS_BUFF_SIZE - 1; i++) { + MEDIAN_FILTER_POINTERS_BUFF[i] = MEDIAN_FILTER_POINTERS_BUFF[i+1]; } - return array; + MEDIAN_FILTER_POINTERS_BUFF[9] = &oldFrame[0]; + return; } -uint8_t * dstack(uint8_t * array1, uint8_t * array2) { - uart_out(array2, sizeof(array1)-1); - uint8_t * newArray[3]; - uint8_t i = 0; - uint8_t i1=0; - uint8_t i2 = 0; - uint8_t pt1, pt2; - pt1 = array1[i1]; - pt2 = array2[i2]; +float median(uint16_t array[], int len) { + float temp; + for (int i = 0; i < len; i++) { + for (int j = i+1; j < len; j++) { + if (array[j] < array[i]) { + temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } + } + } - while (pt1 != NULL && pt2 != NULL){ - uint8_t pt3[2]; - pt3[0] = pt1; - pt3[1] = pt2; - uart_out(pt3, sizeof(pt3)); - newArray[i] = pt3; - uart_out(newArray[i], sizeof(newArray[i])-1); - i ++; - pt1 = array1[i1++]; - pt2 = array2[i2++]; + if + (len % 2 == 0) { + return ((array[len/2] + array[len/2 - 1])/2.0); + } else { + return array[len/2]; } - uint8_t debug[] = "is this printing\r\n"; - uart_out(debug,sizeof(debug)-1); - uart_out(*newArray, sizeof(*newArray)-1); - return (newArray); + } +uint16_t* median_frame(uint16_t* MEDIAN_FILTER_POINTERS_BUFF[], uint16_t frame1[]) { + int j = 0; + while (j < NUM_PIXELS) { + for (int i = 0; i < MEDIAN_FILTER_POINTERS_BUFF_SIZE; i++) { + frame1 = MEDIAN_FILTER_POINTERS_BUFF[i]; + array[i] = frame1[i]; + } + for (int i = 0; i < 10; i++) { + } + frame_median[j] = median(array, NUM_RAW_FRAMES); + j++; + } + return &frame_median[0]; +} \ No newline at end of file diff --git a/GccBoardProject1/src/numpyInC.h b/GccBoardProject1/src/numpyInC.h new file mode 100644 index 0000000..a1a22a7 --- /dev/null +++ b/GccBoardProject1/src/numpyInC.h @@ -0,0 +1,23 @@ +/* + * numpyInC.h + * + * Created: 3/24/2017 8:59 PM + * Author: Udaya Malik + */ + +#ifndef NUMPYINC_H_ +#define NUMPYINC_H_ + +#include +#include + +void init_uart(void); +void uart_out(const uint8_t *string, uint16_t length); +static uint16_t frame_median[NUM_PIXELS]; +static uint16_t array[NUM_RAW_FRAMES]; + +void dstack(uint16_t * MEDIAN_FILTER_POINTERS_BUFF[], uint16_t PIXEL_BUFFER[]); +float median(uint16_t array[], int len); +uint16_t* median_frame(uint16_t* MEDIAN_FILTER_POINTERS_BUFF[], uint16_t frame1[]); + +#endif diff --git a/GccBoardProject1/src/peopleCounting.c b/GccBoardProject1/src/peopleCounting.c index e8547c9..f4e5154 100644 --- a/GccBoardProject1/src/peopleCounting.c +++ b/GccBoardProject1/src/peopleCounting.c @@ -8,81 +8,99 @@ #include #include #include +#include -bool is_local_max(uint16_t ** frame, uint16_t row, uint16_t col) { - uint16_t current_max = frame[row][col]; +bool is_local_max(uint16_t COUNT_BUFF[], uint16_t frame_index, uint16_t row, uint16_t col) { + convertToFrame(COUNT_BUFF, frame_index); + uint16_t current_max = frame_conv[row][col]; + // Greater than (row+1, col), (row-1, col) - if (current_max < frame[row+1][col] || current_max < frame[row-1][col]) { + if (current_max < frame_conv[row+1][col] || current_max < frame_conv[row-1][col]) { return false; } // Greater than (row, col+1), (row, col-1) - if (current_max < frame[row][col+1] || current_max < frame[row][col-1]) { + if (current_max < frame_conv[row][col+1] || current_max < frame_conv[row][col-1]) { return false; } // Greater than (row+1, col+1), (row-1, col-1) - if (current_max < frame[row+1][col+1] || current_max < frame[row-1][col-1]) { + if (current_max < frame_conv[row+1][col+1] || current_max < frame_conv[row-1][col-1]) { return false; } // Greater than (row+1, col-1), (row-1, col+1) - if (current_max < frame[row+1][col-1] || current_max < frame[row-1][col+1]) { + if (current_max < frame_conv[row+1][col-1] || current_max < frame_conv[row-1][col+1]) { return false; } return true; } -uint16_t *** COUNT_BUFF addFrame(uint16_t ** pix_buff, uint16_t*** COUNT_BUFF, uint16_t frameCount) { +uint16_t* addFrame(uint16_t PIXEL_BUFFER[], uint16_t COUNT_BUFF[], uint16_t frameCount, struct keepCount *count) { if (frameCount < 7) { - COUNT_BUFF[frameCount-1] = pix_buff; + for (int i = 0; i < NUM_PIXELS; i++) { + COUNT_BUFF[(frameCount-1)*NUM_PIXELS+i] = PIXEL_BUFFER[i]; + } } else { - for (int i = 0; i < 6; i++){ - COUNT_BUFF[i] = COUNT_BUFF[i+1]; + for (int i = 0; i < 6*NUM_PIXELS; i++){ + COUNT_BUFF[i] = COUNT_BUFF[NUM_PIXELS+i]; + } + for (int i = 0; i < NUM_PIXELS; i++) { + COUNT_BUFF[7*NUM_PIXELS+i] = PIXEL_BUFFER[i]; } - COUNT_BUFF[7] = pix_buff; } + count->counted = false; return COUNT_BUFF; } -uint16_t getMaxId(uint16_t ** array, uint16_t col) { - uint16_t maxNum = array[0]; +uint16_t getMaxId(uint16_t COUNT_BUFF[], uint16_t ind, uint16_t col) { + convertToFrame(COUNT_BUFF, ind); + + uint16_t maxNum = frame_conv[0][0]; uint16_t maxRowId = 0; for (int i=1; i < GRID_SIZE; i++){ - if (maxNum > array[i][col]) { - maxNum = array[i][col]; + if (maxNum > frame_conv[i][col]) { + maxNum = frame_conv[i][col]; maxRowId = i; } } return maxRowId; } +void convertToFrame(uint16_t COUNT_BUFF[], uint16_t ind) { + for (int i = 0; i < GRID_SIZE; i++) { + for (int j = 0; j < GRID_SIZE; j++) { + frame_conv[i][j] = COUNT_BUFF[(ind*NUM_PIXELS) + GRID_SIZE*i + j]; + } + } + return; +} -bool determine_direction(uint16_t trigger_col, uint16_t offset, uint16_t *** COUNT_BUFF) { - if (TRIGGER_INDEX >= COUNT_BUFF_SIZE - 1 || TRIGGER_INDEX < 1){ +int determine_direction(uint16_t trigger_col, uint16_t offset, uint16_t COUNT_BUFF[]) { + if (TRIGGER_INDEX >= COUNT_BUFF_FRAMES - 1 || TRIGGER_INDEX < 1){ //exception } if (trigger_col + offset < 0 || trigger_col + offset >= GRID_SIZE) { //exception } - + printf("In determine_direction\n"); uint16_t check_col = trigger_col + offset; + uint16_t max_idx = getMaxId(COUNT_BUFF, TRIGGER_INDEX, trigger_col); + uint16_t current_max = COUNT_BUFF[TRIGGER_INDEX*NUM_PIXELS + max_idx*GRID_SIZE + trigger_col]; - uint16_t max_idx = getMaxId(COUNT_BUFF[TRIGGER_INDEX], trigger_col); - uint16_t current_max = COUNT_BUFF[TRIGGER_INDEX][max_idx][trigger_col]; - - if (current_max >= TRIGGER_THRESHOLD && is_local_max(COUNT_BUFF[TRIGGER_INDEX], max_idx, trigger_col)) { + if (current_max >= TRIGGER_THRESHOLD && is_local_max(COUNT_BUFF, TRIGGER_INDEX, max_idx, trigger_col)) { // Check check_col in the past // (Change upper bound to look further into the past) - for (int i = 1; i < 3; i++) { - uint16_t ** pastFrame = COUNT_BUFF[TRIGGER_INDEX - i]; - uint16_t pastMaxIdx = getMaxId(pastFrame, check_col); - uint16_t pastMax = pastFrame[pastMaxIdx][check_col]; - if (abs(pastMax - current_max) <= MAX_THRESHOLD && is_local_max(pastFrame, pastMaxIdx, check_col)) { + for (uint16_t i = 1; i < 3; i++) { + uint16_t pastMaxIdx = getMaxId(COUNT_BUFF, TRIGGER_INDEX-i, check_col); + uint16_t pastMax = COUNT_BUFF[(TRIGGER_INDEX-i)*NUM_PIXELS + pastMaxIdx*GRID_SIZE + check_col]; + if (abs(pastMax - current_max) <= MAX_THRESHOLD && is_local_max(COUNT_BUFF, TRIGGER_INDEX-i, pastMaxIdx, check_col)) { if (offset < 0) { + printf("Exiting determine_direction 1\n"); return IN; } else if (offset > 0) { + printf("Exiting determine_direction 2\n"); return OUT; } } @@ -90,57 +108,64 @@ bool determine_direction(uint16_t trigger_col, uint16_t offset, uint16_t *** COU // Check check_col in the future // (Change upper bound to look further into the future) - for (i = 1; i < 3; i++) { - uint16_t ** futureFrame = COUNT_BUFF[TRIGGER_INDEX + i]; - uint16_t futureMaxIdx = getMaxId(futureFrame, check_col); - uint16_t futureMax = futureFrame[futureMaxIdx][check_col]; - if (abs(futureMax - current_max) <= MAX_THRESHOLD && is_local_max(futureFrame, futureMaxIdx, check_col)) { + for (uint16_t i = 1; i < 3; i++) { + uint16_t futureMaxIdx = getMaxId(COUNT_BUFF, TRIGGER_INDEX + i, check_col); + uint16_t futureMax = COUNT_BUFF[(TRIGGER_INDEX+i)*NUM_PIXELS + futureMaxIdx*GRID_SIZE + check_col]; + if (abs(futureMax - current_max) <= MAX_THRESHOLD && is_local_max(COUNT_BUFF, TRIGGER_INDEX + i, futureMaxIdx, check_col)) { if (offset < 0) { + printf("Exiting determine_direction 3\n"); return OUT; - } else if (offset > 0) { + } else if (offset > 0) { + printf("Exiting determine_direction 4\n"); return IN; } } } } - return NULL; //check if I can do that + printf("Exiting determine_direction 5\n"); + return -1; //check if I can do that } -void doCount(uint16_t ** pix_buff, struct keepCount * count, uint16_t*** COUNT_BUFF, uint16_t frameCount) { - if (frameCountcounted) { + printf("Exiting doCount 2\n"); return; } for (int i = 0; i < 2; i++){ uint16_t trigger_col = count->TRIGGER_COLUNM[i][0]; uint16_t offset = count->TRIGGER_COLUNM[i][1]; - bool direction = determine_direction(trigger_col, offset, COUNT_BUFF); + int direction = determine_direction(trigger_col, offset, COUNT_BUFF); if (direction == IN) { count->countIn = count->countIn + 0.5; - } else { + } else { count->countOut = count->countOut + 0.5; } } count->counted = true; + printf("Exiting doCount 3\n"); return; - } -struct keepCount * countPeople(struct keepCount * count, uint16_t PIXEL_BUFFER, uint16_t frameCount, uint16_t*** COUNT_BUFF, uint16_t lastFrame) { - uint16_t pix_buff [8][8]; - for (int i = 0; i < 8; i++) { - for (int j = 0; j < 8; j++) { - pix_buff[i][j] = PIXEL_BUFFER[8*i+j]; +void countPeople(struct keepCount* count, uint16_t PIXEL_BUFFER[], uint16_t frameCount, uint16_t COUNT_BUFF[], uint16_t lastFrame) { + printf("In countPeople\n"); + COUNT_BUFF = addFrame(PIXEL_BUFFER, COUNT_BUFF, frameCount, count); + printf("counted = %d\n", count->counted); + doCount(count, COUNT_BUFF, frameCount); + printf("Exiting countPeople 2\n"); + return; + +} +void initialize_mem(uint16_t PIXEL_BUFFER[], uint16_t frameCount) { + if (frameCount < NUM_RAW_FRAMES) { + for (int i = 0; i < NUM_PIXELS; i++) { + frame[frameCount-1][i] = PIXEL_BUFFER[i]; } } - if (count->counted) { - return count; - } - COUNT_BUFF = addFrame(pix_buff, COUNT_BUFF, frameCount); - doCount(pix_buff, count, COUNT_BUFF, frameCount); - return count; - -} \ No newline at end of file + return; +} diff --git a/GccBoardProject1/src/peopleCounting.h b/GccBoardProject1/src/peopleCounting.h index decac42..9b53b40 100644 --- a/GccBoardProject1/src/peopleCounting.h +++ b/GccBoardProject1/src/peopleCounting.h @@ -8,17 +8,20 @@ #ifndef PEOPLECOUNTING_H_ #define PEOPLECOUNTING_H_ -#define COUNT_BUFF_SIZE 7 +#define COUNT_BUFF_SIZE 448 +#define COUNT_BUFF_FRAMES 7 #define TRIGGER_COLUNM_2 2 #define TRIGGER_COLUNM_5 5 #define CHECK_OFFSET_2 1 -#define CHECk_OFFSET_5 -1 +#define CHECK_OFFSET_5 -1 #define IN false #define OUT true #define TRIGGER_INDEX COUNT_BUFF_SIZE/2 -#define GRID_SIZE 8 +#define GRID_SIZE 8 #define TRIGGER_THRESHOLD 20 -#define MAX_THRESHOLD 10 +#define MAX_THRESHOLD 10 +#define MEDIAN_FILTER_POINTERS_BUFF_SIZE 10 +#define NUM_RAW_FRAMES 10 struct keepCount{ @@ -28,11 +31,12 @@ struct keepCount{ bool counted; }; -bool is_local_max(uint16_t ** frame, uint16_t row, uint16_t col); -uint16_t *** COUNT_BUFF addFrame(uint16_t ** pix_buff, uint16_t*** COUNT_BUFF, uint16_t frameCount); -uint16_t getMaxId(uint16_t ** array, uint16_t col); -bool determine_direction(uint16_t trigger_col, uint16_t offset, uint16_t *** COUNT_BUFF); -void doCount(uint16_t ** pix_buff, struct keepCount * count, uint16_t*** COUNT_BUFF, uint16_t frameCount); -struct keepCount * countPeople(struct keepCount * count, uint16_t PIXEL_BUFFER, uint16_t frameCount, uint16_t*** COUNT_BUFF, uint16_t lastFrame); - -#endif /* UART_H_ */ +bool is_local_max(uint16_t COUNT_BUFF[], uint16_t frame_index, uint16_t row, uint16_t col); +uint16_t* addFrame(uint16_t PIXEL_BUFFER[], uint16_t COUNT_BUFF[], uint16_t frameCount, struct keepCount *count); +uint16_t getMaxId(uint16_t COUNT_BUFF[], uint16_t ind, uint16_t col); +void convertToFrame(uint16_t COUNT_BUFF[], uint16_t ind); +int determine_direction(uint16_t trigger_col, uint16_t offset, uint16_t* COUNT_BUFF); +void doCount(struct keepCount *count, uint16_t COUNT_BUFF[], uint16_t frameCount); +void countPeople(struct keepCount* count, uint16_t PIXEL_BUFFER[], uint16_t frameCount, uint16_t COUNT_BUFF[], uint16_t lastFrame); +void initialize_mem(uint16_t PIXEL_BUFFER[], uint16_t frameCount); +#endif From 83f40da8d2bfa92212416c941a7370e2ffbdb79f Mon Sep 17 00:00:00 2001 From: Udaya Date: Fri, 7 Apr 2017 22:43:15 -0400 Subject: [PATCH 2/2] Made changes acc. PR --- GccBoardProject1/src/grideye.c | 1 + GccBoardProject1/src/main.c | 53 ++-------- GccBoardProject1/src/numpyInC.c | 52 +++++----- GccBoardProject1/src/numpyInC.h | 15 +-- GccBoardProject1/src/peopleCounting.c | 143 +++++++++++++++----------- GccBoardProject1/src/peopleCounting.h | 20 ++-- GccBoardProject1/src/uart.c | 98 ------------------ GccBoardProject1/src/uart.h | 28 ----- 8 files changed, 136 insertions(+), 274 deletions(-) delete mode 100644 GccBoardProject1/src/uart.c delete mode 100644 GccBoardProject1/src/uart.h diff --git a/GccBoardProject1/src/grideye.c b/GccBoardProject1/src/grideye.c index d26ce88..658e1cf 100755 --- a/GccBoardProject1/src/grideye.c +++ b/GccBoardProject1/src/grideye.c @@ -7,6 +7,7 @@ #include #include +#include #define GE_BUFFER_DATA_LENGTH 128 static uint8_t ge_write_buffer[GE_BUFFER_DATA_LENGTH]; diff --git a/GccBoardProject1/src/main.c b/GccBoardProject1/src/main.c index e1dd624..fe58f68 100755 --- a/GccBoardProject1/src/main.c +++ b/GccBoardProject1/src/main.c @@ -38,21 +38,10 @@ #include static uint16_t PIXEL_BUFFER[NUM_PIXELS]; -static uint16_t frame1[NUM_PIXELS]; -static uint16_t* MEDIAN_FILTER_POINTERS_BUFF[MEDIAN_FILTER_POINTERS_BUFF_SIZE]; -static uint16_t frame[NUM_RAW_FRAMES][NUM_PIXELS]; +static uint16_t *MEDIAN_FILTER_POINTERS_BUFF[MEDIAN_FILTER_POINTERS_BUFF_SIZE]; static uint16_t COUNT_BUFF[COUNT_BUFF_SIZE]; -static uint16_t frame_conv[GRID_SIZE][GRID_SIZE]; -static struct keepCount count; -void median_buffer_init(uint16_t* MEDIAN_FILTER_POINTERS_BUFF[], uint16_t** frame) { - for (int i = 0; i < NUM_RAW_FRAMES; i++) { - MEDIAN_FILTER_POINTERS_BUFF[i] = &frame[i]; - } - return; -} - int main (void) { @@ -60,18 +49,11 @@ int main (void) { init_uart(); init_grideye(); delay_init(); - median_buffer_init(MEDIAN_FILTER_POINTERS_BUFF, frame); - + median_buffer_init(MEDIAN_FILTER_POINTERS_BUFF); + initialize_count(); + uint16_t frame_count = 0; - count->countIn = 0; - count->countOut = 0; - uint16_t frameCount = 0; - uint16_t lastFrame = 0; - count->TRIGGER_COLUNM[0][0] = TRIGGER_COLUNM_2; - count->TRIGGER_COLUNM[0][1] = CHECK_OFFSET_2; - count->TRIGGER_COLUNM[1][0] = TRIGGER_COLUNM_5; - count->TRIGGER_COLUNM[1][1] = CHECK_OFFSET_5; - count->counted = false; + initialize_count(); /* Insert application code here, after the board has been initialized. */ @@ -92,28 +74,11 @@ int main (void) { if (!ge_is_sleeping()) { // Send frame uint8_t buffer[512]; - - //double ambient_temp = get_ambient_temp(); - - //uint16_t len = snprintf((char *) buffer, sizeof(buffer), "It is %.2lf degrees Celsius.\r\n", ambient_temp); - //uart_out(buffer, len); + ge_get_frame(PIXEL_BUFFER); - uint16_t size = 0; - for (int i = 0; i < NUM_PIXELS; i++) { - size += sprintf((char *) (buffer + size), "%d,", PIXEL_BUFFER[i]); - } - buffer[size-1] = '\r'; - buffer[size] = '\n'; - frameCount++; - if (frameCount <= NUM_RAW_FRAMES) { - initialize_mem(PIXEL_BUFFER, frameCount); - } else { - dstack(MEDIAN_FILTER_POINTERS_BUFF, PIXEL_BUFFER);; - } - uint16_t *frame_median = median_frame(MEDIAN_FILTER_POINTERS_BUFF, frame1); - - count = countPeople(&count, frame_median, frameCount, COUNT_BUFF, lastFrame); - uart_out(buffer, size+1); + start_count(frame_count, PIXEL_BUFFER, MEDIAN_FILTER_POINTERS_BUFF, COUNT_BUFF); + frame_count++; } } + return 0; } diff --git a/GccBoardProject1/src/numpyInC.c b/GccBoardProject1/src/numpyInC.c index 5806a88..ade6787 100644 --- a/GccBoardProject1/src/numpyInC.c +++ b/GccBoardProject1/src/numpyInC.c @@ -10,11 +10,10 @@ #include struct usart_module usart_instance; -static uint16_t frame_median[NUM_PIXELS]; -static uint16_t array[NUM_RAW_FRAMES]; +static uint16_t FRAME_MEDIAN[NUM_PIXELS]; +static uint16_t ARRAY[NUM_RAW_FRAMES]; +static uint16_t FRAME_TEMP[NUM_PIXELS]; -void init_uart(void); -void uart_out(const uint8_t *string, uint16_t length); void init_uart() { @@ -39,50 +38,45 @@ void uart_out(const uint8_t *string, uint16_t length) { } -void dstack(uint16_t * MEDIAN_FILTER_POINTERS_BUFF[], uint16_t PIXEL_BUFFER[]) { - uint16_t *oldFrame= MEDIAN_FILTER_POINTERS_BUFF[0]; +void dstack(uint16_t * median_filter_buffer[], uint16_t pixel_buff[]) { + uint16_t *oldFrame= median_filter_buffer[0]; for (int i = 0; i < NUM_PIXELS; i++){ - oldFrame[i] = PIXEL_BUFFER[i]; + oldFrame[i] = pixel_buff[i]; } for (int i = 0; i < MEDIAN_FILTER_POINTERS_BUFF_SIZE - 1; i++) { - MEDIAN_FILTER_POINTERS_BUFF[i] = MEDIAN_FILTER_POINTERS_BUFF[i+1]; + median_filter_buffer[i] = median_filter_buffer[i+1]; } - MEDIAN_FILTER_POINTERS_BUFF[9] = &oldFrame[0]; + median_filter_buffer[MEDIAN_POINTER_BUFF_LAST_INDEX] = &oldFrame[0]; return; } -float median(uint16_t array[], int len) { - float temp; +uint16_t median(int len) { + uint16_t temp; for (int i = 0; i < len; i++) { for (int j = i+1; j < len; j++) { - if (array[j] < array[i]) { - temp = array[i]; - array[i] = array[j]; - array[j] = temp; + if (ARRAY[j] < ARRAY[i]) { + temp = ARRAY[i]; + ARRAY[i] = ARRAY[j]; + ARRAY[j] = temp; } } } - if - (len % 2 == 0) { - return ((array[len/2] + array[len/2 - 1])/2.0); + if (len % 2 == 0) { + return ((ARRAY[len/2] + ARRAY[len/2 - 1])/2); } else { - return array[len/2]; + return ARRAY[len/2]; } } -uint16_t* median_frame(uint16_t* MEDIAN_FILTER_POINTERS_BUFF[], uint16_t frame1[]) { - int j = 0; - while (j < NUM_PIXELS) { +uint16_t* median_frame(uint16_t* median_filter_buffer[]) { + for (int j = 0; j < NUM_PIXELS; j++) { for (int i = 0; i < MEDIAN_FILTER_POINTERS_BUFF_SIZE; i++) { - frame1 = MEDIAN_FILTER_POINTERS_BUFF[i]; - array[i] = frame1[i]; + FRAME_TEMP = median_filter_buffer[i]; + ARRAY[i] = FRAME_TEMP[j]; } - for (int i = 0; i < 10; i++) { - } - frame_median[j] = median(array, NUM_RAW_FRAMES); - j++; + FRAME_MEDIAN[j] = median(NUM_RAW_FRAMES); } - return &frame_median[0]; + return &FRAME_MEDIAN[0]; } \ No newline at end of file diff --git a/GccBoardProject1/src/numpyInC.h b/GccBoardProject1/src/numpyInC.h index a1a22a7..4bc5517 100644 --- a/GccBoardProject1/src/numpyInC.h +++ b/GccBoardProject1/src/numpyInC.h @@ -8,16 +8,19 @@ #ifndef NUMPYINC_H_ #define NUMPYINC_H_ +#define MEDIAN_POINTER_BUFF_LAST_INDEX 9 + #include #include +#include +#include void init_uart(void); void uart_out(const uint8_t *string, uint16_t length); -static uint16_t frame_median[NUM_PIXELS]; -static uint16_t array[NUM_RAW_FRAMES]; - -void dstack(uint16_t * MEDIAN_FILTER_POINTERS_BUFF[], uint16_t PIXEL_BUFFER[]); -float median(uint16_t array[], int len); -uint16_t* median_frame(uint16_t* MEDIAN_FILTER_POINTERS_BUFF[], uint16_t frame1[]); +void dstack(uint16_t * median_filter_buffer[], uint16_t pixel_buff[]); +float median(int len); +uint16_t* median_frame(uint16_t* median_filter_buffer[]); +void init_uart(void); +void uart_out(const uint8_t *string, uint16_t length); #endif diff --git a/GccBoardProject1/src/peopleCounting.c b/GccBoardProject1/src/peopleCounting.c index f4e5154..6b98a23 100644 --- a/GccBoardProject1/src/peopleCounting.c +++ b/GccBoardProject1/src/peopleCounting.c @@ -10,97 +10,128 @@ #include #include +static uint16_t FRAME_CONV[GRID_SIZE][GRID_SIZE]; +static uint16_t FRAME[NUM_RAW_FRAMES][NUM_PIXELS]; +static struct keepCount count; -bool is_local_max(uint16_t COUNT_BUFF[], uint16_t frame_index, uint16_t row, uint16_t col) { - convertToFrame(COUNT_BUFF, frame_index); - uint16_t current_max = frame_conv[row][col]; +void initialize_count() { + count.countIn = 0; + count.countOut = 0; + count.TRIGGER_COLUNM[0][0] = TRIGGER_COLUNM_2; + count.TRIGGER_COLUNM[0][1] = CHECK_OFFSET_2; + count.TRIGGER_COLUNM[1][0] = TRIGGER_COLUNM_5; + count.TRIGGER_COLUNM[1][1] = CHECK_OFFSET_5; + count.counted = false; +} + +void median_buffer_init(uint16_t *median_filters_buff[]) { + for (int i = 0; i < NUM_RAW_FRAMES; i++) { + median_filters_buff[i] = &FRAME[i]; + } + return; +} + +void pc_new_frame(uint16_t frame count, uint16_t pix_buff[], uint16_t *median_filters_buff[]) { + if (frame_count < NUM_RAW_FRAMES) { + initialize_mem(pix_buff, frame_count); + } else { + dstack(median_filters_buff, pix_buff); + } + return; +} + +void start_count(uint16_t frame count, uint16_t pix_buff[], uint16_t *median_filter_buff[], uint16_t count_buffer[]) { + pc_new_frame(frame_count, pix_buff, median_filter_buff); + uint16_t *frame_median = median_frame(median_filter_buff); + countPeople(frame_median, frame_count, count_buffer); + return; +} + +bool is_local_max(uint16_t count_buffer[], uint16_t frame_index, uint16_t row, uint16_t col) { + convert_to_frame(count_buffer, frame_index); + uint16_t current_max = FRAME_CONV[row][col]; // Greater than (row+1, col), (row-1, col) - if (current_max < frame_conv[row+1][col] || current_max < frame_conv[row-1][col]) { + if ((row < 7 && current_max < FRAME_CONV[row+1][col] )|| (row > 0 && current_max < FRAME_CONV[row-1][col])) { return false; } // Greater than (row, col+1), (row, col-1) - if (current_max < frame_conv[row][col+1] || current_max < frame_conv[row][col-1]) { + if ((col < 7 && current_max < FRAME_CONV[row][col+1]) || (col > 0 && current_max < FRAME_CONV[row][col-1])) { return false; } // Greater than (row+1, col+1), (row-1, col-1) - if (current_max < frame_conv[row+1][col+1] || current_max < frame_conv[row-1][col-1]) { + if ((row < 7 && col < 7 && current_max < FRAME_CONV[row+1][col+1]) || (row > 0 && col > 0 && current_max < FRAME_CONV[row-1][col-1])) { return false; } // Greater than (row+1, col-1), (row-1, col+1) - if (current_max < frame_conv[row+1][col-1] || current_max < frame_conv[row-1][col+1]) { + if ((row < 7 && col > 0 && current_max < FRAME_CONV[row+1][col-1]) || (row > 0 && col < 7 && current_max < FRAME_CONV[row-1][col+1])) { return false; } return true; } -uint16_t* addFrame(uint16_t PIXEL_BUFFER[], uint16_t COUNT_BUFF[], uint16_t frameCount, struct keepCount *count) { - if (frameCount < 7) { +void add_frame(uint16_t frame_median[], uint16_t count_buffer[], uint16_t frame_count) { + if (frame_count < 6) { for (int i = 0; i < NUM_PIXELS; i++) { - COUNT_BUFF[(frameCount-1)*NUM_PIXELS+i] = PIXEL_BUFFER[i]; + count_buffer[(frame_count)*NUM_PIXELS+i] = frame_median[i]; } - } - else { + } else { for (int i = 0; i < 6*NUM_PIXELS; i++){ - COUNT_BUFF[i] = COUNT_BUFF[NUM_PIXELS+i]; + count_buffer[i] = count_buffer[NUM_PIXELS+i]; } for (int i = 0; i < NUM_PIXELS; i++) { - COUNT_BUFF[7*NUM_PIXELS+i] = PIXEL_BUFFER[i]; + count_buffer[6*NUM_PIXELS+i] = frame_median[i]; } } - count->counted = false; - return COUNT_BUFF; + count.counted = false; + return; } -uint16_t getMaxId(uint16_t COUNT_BUFF[], uint16_t ind, uint16_t col) { - convertToFrame(COUNT_BUFF, ind); - - uint16_t maxNum = frame_conv[0][0]; +uint16_t getMaxId(uint16_t count_buffer[], uint16_t ind, uint16_t col) { + convert_to_frame(count_buffer, ind); + uint16_t maxNum = FRAME_CONV[0][0]; uint16_t maxRowId = 0; for (int i=1; i < GRID_SIZE; i++){ - if (maxNum > frame_conv[i][col]) { - maxNum = frame_conv[i][col]; + if (maxNum > FRAME_CONV[i][col]) { + maxNum = FRAME_CONV[i][col]; maxRowId = i; } } return maxRowId; } -void convertToFrame(uint16_t COUNT_BUFF[], uint16_t ind) { +void convert_to_frame(uint16_t count_buffer[], uint16_t ind) { for (int i = 0; i < GRID_SIZE; i++) { for (int j = 0; j < GRID_SIZE; j++) { - frame_conv[i][j] = COUNT_BUFF[(ind*NUM_PIXELS) + GRID_SIZE*i + j]; + FRAME_CONV[i][j] = count_buffer[(ind*NUM_PIXELS) + GRID_SIZE*i + j]; } } return; } -int determine_direction(uint16_t trigger_col, uint16_t offset, uint16_t COUNT_BUFF[]) { +int determine_direction(uint16_t trigger_col, uint16_t offset, uint16_t count_buffer[]) { if (TRIGGER_INDEX >= COUNT_BUFF_FRAMES - 1 || TRIGGER_INDEX < 1){ //exception } if (trigger_col + offset < 0 || trigger_col + offset >= GRID_SIZE) { //exception } - printf("In determine_direction\n"); uint16_t check_col = trigger_col + offset; - uint16_t max_idx = getMaxId(COUNT_BUFF, TRIGGER_INDEX, trigger_col); + uint16_t max_idx = getMaxId(count_buffer, TRIGGER_INDEX, trigger_col); uint16_t current_max = COUNT_BUFF[TRIGGER_INDEX*NUM_PIXELS + max_idx*GRID_SIZE + trigger_col]; - if (current_max >= TRIGGER_THRESHOLD && is_local_max(COUNT_BUFF, TRIGGER_INDEX, max_idx, trigger_col)) { + if (current_max >= TRIGGER_THRESHOLD && is_local_max(count_buffer, TRIGGER_INDEX, max_idx, trigger_col)) { // Check check_col in the past // (Change upper bound to look further into the past) for (uint16_t i = 1; i < 3; i++) { - uint16_t pastMaxIdx = getMaxId(COUNT_BUFF, TRIGGER_INDEX-i, check_col); - uint16_t pastMax = COUNT_BUFF[(TRIGGER_INDEX-i)*NUM_PIXELS + pastMaxIdx*GRID_SIZE + check_col]; - if (abs(pastMax - current_max) <= MAX_THRESHOLD && is_local_max(COUNT_BUFF, TRIGGER_INDEX-i, pastMaxIdx, check_col)) { + uint16_t pastMaxIdx = getMaxId(count_buffer, TRIGGER_INDEX-i, check_col); + uint16_t pastMax = count_buffer[(TRIGGER_INDEX-i)*NUM_PIXELS + pastMaxIdx*GRID_SIZE + check_col]; + if (abs(pastMax - current_max) <= MAX_THRESHOLD && is_local_max(count_buffer, TRIGGER_INDEX-i, pastMaxIdx, check_col)) { if (offset < 0) { - printf("Exiting determine_direction 1\n"); return IN; } else if (offset > 0) { - printf("Exiting determine_direction 2\n"); return OUT; } } @@ -109,62 +140,52 @@ int determine_direction(uint16_t trigger_col, uint16_t offset, uint16_t COUNT_BU // Check check_col in the future // (Change upper bound to look further into the future) for (uint16_t i = 1; i < 3; i++) { - uint16_t futureMaxIdx = getMaxId(COUNT_BUFF, TRIGGER_INDEX + i, check_col); - uint16_t futureMax = COUNT_BUFF[(TRIGGER_INDEX+i)*NUM_PIXELS + futureMaxIdx*GRID_SIZE + check_col]; - if (abs(futureMax - current_max) <= MAX_THRESHOLD && is_local_max(COUNT_BUFF, TRIGGER_INDEX + i, futureMaxIdx, check_col)) { + uint16_t futureMaxIdx = getMaxId(count_buffer, TRIGGER_INDEX + i, check_col); + uint16_t futureMax = count_buffer[(TRIGGER_INDEX+i)*NUM_PIXELS + futureMaxIdx*GRID_SIZE + check_col]; + if (abs(futureMax - current_max) <= MAX_THRESHOLD && is_local_max(count_buffer, TRIGGER_INDEX + i, futureMaxIdx, check_col)) { if (offset < 0) { - printf("Exiting determine_direction 3\n"); return OUT; } else if (offset > 0) { - printf("Exiting determine_direction 4\n"); return IN; } } } } - printf("Exiting determine_direction 5\n"); return -1; //check if I can do that } -void doCount(struct keepCount * count, uint16_t COUNT_BUFF[], uint16_t frameCount) { - printf("in doCount\n"); - if (frameCountcounted) { - printf("Exiting doCount 2\n"); + if (count.counted) { return; } for (int i = 0; i < 2; i++){ - uint16_t trigger_col = count->TRIGGER_COLUNM[i][0]; - uint16_t offset = count->TRIGGER_COLUNM[i][1]; - int direction = determine_direction(trigger_col, offset, COUNT_BUFF); + uint16_t trigger_col = count.TRIGGER_COLUNM[i][0]; + uint16_t offset = count.TRIGGER_COLUNM[i][1]; + int direction = determine_direction(trigger_col, offset, count_buffer); if (direction == IN) { - count->countIn = count->countIn + 0.5; + count.countIn = count.countIn + 0.5; } else { - count->countOut = count->countOut + 0.5; + count.countOut = count.countOut + 0.5; } } - count->counted = true; - printf("Exiting doCount 3\n"); + count.counted = true; return; } -void countPeople(struct keepCount* count, uint16_t PIXEL_BUFFER[], uint16_t frameCount, uint16_t COUNT_BUFF[], uint16_t lastFrame) { - printf("In countPeople\n"); - COUNT_BUFF = addFrame(PIXEL_BUFFER, COUNT_BUFF, frameCount, count); - printf("counted = %d\n", count->counted); - doCount(count, COUNT_BUFF, frameCount); - printf("Exiting countPeople 2\n"); +void countPeople(uint16_t frame_median[], uint16_t frame_count, uint16_t count_buffer[]) { + add_frame(frame_median, count_buffer, frame_count); + doCount(count_buffer, frame_count); return; } -void initialize_mem(uint16_t PIXEL_BUFFER[], uint16_t frameCount) { - if (frameCount < NUM_RAW_FRAMES) { +void initialize_mem(uint16_t pix_buff[], uint16_t frame_count) { + if (frame_count < NUM_RAW_FRAMES) { for (int i = 0; i < NUM_PIXELS; i++) { - frame[frameCount-1][i] = PIXEL_BUFFER[i]; + FRAME[i] = pix_buff[i]; } } return; diff --git a/GccBoardProject1/src/peopleCounting.h b/GccBoardProject1/src/peopleCounting.h index 9b53b40..12e2349 100644 --- a/GccBoardProject1/src/peopleCounting.h +++ b/GccBoardProject1/src/peopleCounting.h @@ -31,12 +31,16 @@ struct keepCount{ bool counted; }; -bool is_local_max(uint16_t COUNT_BUFF[], uint16_t frame_index, uint16_t row, uint16_t col); -uint16_t* addFrame(uint16_t PIXEL_BUFFER[], uint16_t COUNT_BUFF[], uint16_t frameCount, struct keepCount *count); -uint16_t getMaxId(uint16_t COUNT_BUFF[], uint16_t ind, uint16_t col); -void convertToFrame(uint16_t COUNT_BUFF[], uint16_t ind); -int determine_direction(uint16_t trigger_col, uint16_t offset, uint16_t* COUNT_BUFF); -void doCount(struct keepCount *count, uint16_t COUNT_BUFF[], uint16_t frameCount); -void countPeople(struct keepCount* count, uint16_t PIXEL_BUFFER[], uint16_t frameCount, uint16_t COUNT_BUFF[], uint16_t lastFrame); -void initialize_mem(uint16_t PIXEL_BUFFER[], uint16_t frameCount); +void median_buffer_init(uint16_t *median_filters_buff[]); +void initialize_count(); +void pc_new_frame(uint16_t frame_count, uint16_t pix_buff[], uint16_t *median_filters_buff[]); +void start_count(uint16_t frame_count, uint16_t pix_buff[], uint16_t *median_filter_buff[], uint16_t count_buffer[]); +bool is_local_max(uint16_t count_buffer[], uint16_t frame_index, uint16_t row, uint16_t col); +void add_frame(uint16_t frame_median[], uint16_t count_buffer[], uint16_t frame_count) +uint16_t getMaxId(uint16_t count_buffer[], uint16_t ind, uint16_t col); +void convert_to_frame(uint16_t count_buffer[], uint16_t ind); +int determine_direction(uint16_t trigger_col, uint16_t offset, uint16_t count_buffer[]); +void doCount(uint16_t count_buffer[], uint16_t frame_count); +void countPeople(uint16_t frame_median[], uint16_t frame_count, uint16_t count_buffer[]); +void initialize_mem(uint16_t pix_buff[], uint16_t frame_count); #endif diff --git a/GccBoardProject1/src/uart.c b/GccBoardProject1/src/uart.c deleted file mode 100644 index 7c756e2..0000000 --- a/GccBoardProject1/src/uart.c +++ /dev/null @@ -1,98 +0,0 @@ -/* - * CFile1.c - * - * Created: 3/24/2017 11:52:40 AM - * Author: mazacar8 - */ - -#include -#include "uart.h" -#include - -size_t readBuffOffset = 0; -uint8_t readBuf[READ_BUFFER_SIZE]; - -void init_uart() { - struct usart_config config_usart; - struct usart_config config_lora; - usart_get_config_defaults(&config_usart); - - config_usart.baudrate = DEBUG_BAUD; - config_usart.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING; - config_usart.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0; - config_usart.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1; - config_usart.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2; - config_usart.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3; - - usart_get_config_defaults(&config_lora); - config_lora.baudrate = LORA_BAUD; - config_lora.mux_setting = EXT3_UART_SERCOM_MUX_SETTING; - config_lora.pinmux_pad0 = EXT3_UART_SERCOM_PINMUX_PAD0; - config_lora.pinmux_pad1 = EXT3_UART_SERCOM_PINMUX_PAD1; - config_lora.pinmux_pad2 = EXT3_UART_SERCOM_PINMUX_PAD2; - config_lora.pinmux_pad3 = EXT3_UART_SERCOM_PINMUX_PAD3; - - while (usart_init(&usart_instance, EDBG_CDC_MODULE, &config_usart) != STATUS_OK) { - } - - while (usart_init(&lora_uart_instance, EXT3_UART_MODULE, &config_lora) != STATUS_OK) { - } - - usart_enable(&usart_instance); - usart_enable(&lora_uart_instance); - uint8_t msg[] = "UART Initialized\r\n"; - usart_write_buffer_wait(&usart_instance,msg,sizeof(msg)-1); -} - -void send_command(uint8_t cmd[],uint16_t len) -{ - usart_write_buffer_wait(&lora_uart_instance, cmd, len); - uint8_t msg[] = "Sent command: "; - uint8_t term[] = "\r\n"; - usart_write_buffer_wait(&usart_instance,msg,sizeof(msg)-1); - usart_write_buffer_wait(&usart_instance,cmd,len); - usart_write_buffer_wait(&usart_instance,term,2); - delay_ms(1000); - read_response(); - delay_ms(2500); - -} - -void send_command_noack(uint8_t cmd[],uint16_t len) -{ - usart_write_buffer_wait(&lora_uart_instance, cmd, len); - delay_ms(2500); -} - -void read_response() -{ - uint16_t c; - uint8_t msg[] = "ReadBuf overflow, emptying buffer\r\n"; - uint8_t noresp[] = "No response\r\n"; - uint8_t msg1[] = "Received from RN2903: "; - bool response_found = false; - - if(usart_read_wait(&lora_uart_instance, &c) == STATUS_OK){ - - readBuf[readBuffOffset++] = c; - while((uint8_t)c != '\n'){ - if(usart_read_wait(&lora_uart_instance, &c) == STATUS_OK){ - if(readBuffOffset == READ_BUFFER_SIZE){ - usart_write_buffer_wait(&usart_instance,msg,sizeof(msg)-1); - } - readBuf[readBuffOffset++] = c; - } - } - - readBuf[readBuffOffset] = 0; - usart_write_buffer_wait(&usart_instance,msg1,sizeof(msg1)-1); - usart_write_buffer_wait(&usart_instance,readBuf,readBuffOffset); - readBuffOffset = 0; - - } - - else{ - usart_write_buffer_wait(&usart_instance,noresp,sizeof(noresp)-1); - } - -} diff --git a/GccBoardProject1/src/uart.h b/GccBoardProject1/src/uart.h deleted file mode 100644 index 52959da..0000000 --- a/GccBoardProject1/src/uart.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * uart.h - * - * Created: 3/24/2017 12:04:32 PM - * Author: mazacar8 - */ - -#include - -#ifndef UART_H_ -#define UART_H_ - -#define MAX_READ_ATTEMPTS 5 -#define READ_BUFFER_SIZE 200 -#define LORA_BAUD 57600 -#define DEBUG_BAUD 9600 - -struct usart_module usart_instance; -struct usart_module lora_uart_instance; - -void init_uart(void); -void send_command(uint8_t cmd[],uint16_t len); -void send_command_noack(uint8_t cmd[],uint16_t len); -void read_response(void); - - - -#endif /* UART_H_ */ \ No newline at end of file