void FreqData_NewValue(uint16_t newFreq) { s_seconds_since_last_reading += IDEAL_SECONDS; if (s_seconds_since_last_reading >= SAMPLE_EVERY_N_SECONDS) { s_currentAverage = calculateNewAverage(newFreq); Ringbuf_Put(&s_frequencyBuffer, (uint8_t*)&newFreq); Ringbuf_Put(&s_averageBuffer, (uint8_t*)&s_currentAverage); s_seconds_since_last_reading = 0U; } }
void FreqData_Init(void) { Ringbuf_Init(&s_frequencyBuffer, (uint8_t*)s_freqData, sizeof(uint16_t), BUFFER_SIZE, true); Ringbuf_Init(&s_averageBuffer, (uint8_t*)s_averageData, sizeof(uint16_t), BUFFER_SIZE, true); // Fill the buffers with ideal frequency values uint8_t i = 0; uint16_t freq = IDEAL_F_MAINS * 1000U; for (i = 0; i < BUFFER_SIZE; ++i) { Ringbuf_Put(&s_frequencyBuffer, (uint8_t*)&freq); } }
/* test the ring buffer */ void testRingAroundBuffer( Test * pTest, RING_BUFFER * test_buffer, uint8_t * data_element, unsigned element_size, unsigned element_count) { uint8_t *test_data; unsigned index; unsigned data_index; unsigned count; unsigned dummy; bool status; ct_test(pTest, Ringbuf_Empty(test_buffer)); /* test the ring around the buffer */ for (index = 0; index < element_count; index++) { for (count = 1; count < 4; count++) { dummy = index * count; for (data_index = 0; data_index < element_size; data_index++) { data_element[data_index] = dummy; } status = Ringbuf_Put(test_buffer, data_element); ct_test(pTest, status == true); } for (count = 1; count < 4; count++) { dummy = index * count; test_data = Ringbuf_Get_Front(test_buffer); ct_test(pTest, test_data); if (test_data) { for (data_index = 0; data_index < element_size; data_index++) { ct_test(pTest, test_data[data_index] == dummy); } } test_data = Ringbuf_Pop_Front(test_buffer); ct_test(pTest, test_data); if (test_data) { for (data_index = 0; data_index < element_size; data_index++) { ct_test(pTest, test_data[data_index] == dummy); } } } } ct_test(pTest, Ringbuf_Empty(test_buffer)); }
/* test the ring buffer */ static void testRingBuf( Test * pTest, uint8_t * data_store, uint8_t * data_element, unsigned element_size, unsigned element_count) { RING_BUFFER test_buffer; volatile uint8_t *test_data; unsigned index; unsigned data_index; bool status; Ringbuf_Init(&test_buffer, data_store, element_size, element_count); ct_test(pTest, Ringbuf_Empty(&test_buffer)); for (data_index = 0; data_index < element_size; data_index++) { data_element[data_index] = data_index; } status = Ringbuf_Put(&test_buffer, data_element); ct_test(pTest, status == true); ct_test(pTest, !Ringbuf_Empty(&test_buffer)); test_data = Ringbuf_Peek(&test_buffer); for (data_index = 0; data_index < element_size; data_index++) { ct_test(pTest, test_data[data_index] == data_element[data_index]); } ct_test(pTest, !Ringbuf_Empty(&test_buffer)); (void) Ringbuf_Pop(&test_buffer, NULL); ct_test(pTest, Ringbuf_Empty(&test_buffer)); /* fill to max */ for (index = 0; index < element_count; index++) { for (data_index = 0; data_index < element_size; data_index++) { data_element[data_index] = index; } status = Ringbuf_Put(&test_buffer, data_element); ct_test(pTest, status == true); ct_test(pTest, !Ringbuf_Empty(&test_buffer)); } /* verify actions on full buffer */ for (index = 0; index < element_count; index++) { for (data_index = 0; data_index < element_size; data_index++) { data_element[data_index] = index; } status = Ringbuf_Put(&test_buffer, data_element); ct_test(pTest, status == false); ct_test(pTest, !Ringbuf_Empty(&test_buffer)); } /* check buffer full */ for (index = 0; index < element_count; index++) { test_data = Ringbuf_Peek(&test_buffer); ct_test(pTest, test_data); if (test_data) { for (data_index = 0; data_index < element_size; data_index++) { ct_test(pTest, test_data[data_index] == index); } } (void) Ringbuf_Pop(&test_buffer, NULL); } ct_test(pTest, Ringbuf_Empty(&test_buffer)); testRingAroundBuffer(pTest, &test_buffer, data_element, element_size, element_count); /* adjust the internal index of Ringbuf to test unsigned wrapping */ test_buffer.head = UINT_MAX - 1; test_buffer.tail = UINT_MAX - 1; testRingAroundBuffer(pTest, &test_buffer, data_element, element_size, element_count); return; }
void testRingBuf( Test * pTest) { RING_BUFFER test_buffer; char data_store[RING_BUFFER_DATA_SIZE * RING_BUFFER_COUNT]; char data[RING_BUFFER_DATA_SIZE]; char *test_data; unsigned index; unsigned data_index; unsigned count; unsigned dummy; bool status; Ringbuf_Init(&test_buffer, data_store, RING_BUFFER_DATA_SIZE, RING_BUFFER_COUNT); ct_test(pTest, Ringbuf_Empty(&test_buffer)); for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { data[data_index] = data_index; } status = Ringbuf_Put(&test_buffer, data); ct_test(pTest, status == true); ct_test(pTest, !Ringbuf_Empty(&test_buffer)); test_data = Ringbuf_Get_Front(&test_buffer); for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { ct_test(pTest, test_data[data_index] == data[data_index]); } ct_test(pTest, !Ringbuf_Empty(&test_buffer)); test_data = Ringbuf_Pop_Front(&test_buffer); for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { ct_test(pTest, test_data[data_index] == data[data_index]); } ct_test(pTest, Ringbuf_Empty(&test_buffer)); /* fill to max */ for (index = 0; index < RING_BUFFER_COUNT; index++) { for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { data[data_index] = index; } status = Ringbuf_Put(&test_buffer, data); ct_test(pTest, status == true); ct_test(pTest, !Ringbuf_Empty(&test_buffer)); } /* verify actions on full buffer */ for (index = 0; index < RING_BUFFER_COUNT; index++) { for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { data[data_index] = index; } status = Ringbuf_Put(&test_buffer, data); ct_test(pTest, status == false); ct_test(pTest, !Ringbuf_Empty(&test_buffer)); } /* check buffer full */ for (index = 0; index < RING_BUFFER_COUNT; index++) { test_data = Ringbuf_Get_Front(&test_buffer); for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { ct_test(pTest, test_data[data_index] == index); } test_data = Ringbuf_Pop_Front(&test_buffer); for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { ct_test(pTest, test_data[data_index] == index); } } ct_test(pTest, Ringbuf_Empty(&test_buffer)); /* test the ring around the buffer */ for (index = 0; index < RING_BUFFER_COUNT; index++) { for (count = 1; count < 4; count++) { dummy = index * count; for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { data[data_index] = dummy; } status = Ringbuf_Put(&test_buffer, data); ct_test(pTest, status == true); } for (count = 1; count < 4; count++) { dummy = index * count; test_data = Ringbuf_Get_Front(&test_buffer); for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { ct_test(pTest, test_data[data_index] == dummy); } test_data = Ringbuf_Pop_Front(&test_buffer); for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { ct_test(pTest, test_data[data_index] == dummy); } } } ct_test(pTest, Ringbuf_Empty(&test_buffer)); return; }