From 20e98aeb95e4bfb36bd13f275f34d738eb945968 Mon Sep 17 00:00:00 2001 From: Vineet K Date: Sun, 21 Apr 2024 23:32:44 -0400 Subject: [PATCH] add notes circular buffer to piano lcd --- lcd.c | 50 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/lcd.c b/lcd.c index 5501d86..d60cb1e 100644 --- a/lcd.c +++ b/lcd.c @@ -46,9 +46,6 @@ getNote(void) // convert to capital hex else if (c >= 0xa && c <= 0xf) c = c - 0xa + 'A'; - // shift to rest of alphabet - else if (c >= 0x10) - c = c - 0x10 + 'G'; return c; } @@ -209,7 +206,6 @@ void displayNotes() { } - void displaySerialData() { char circularBuffer[16]; static int bufferIndex = 0; @@ -251,10 +247,52 @@ void displaySerialData() { lcd_clear(); } +void displayPiano() { + char circularBuffer[16]; + static int bufferIndex = 0; + lcd_clear(); + lcd_gotoxy(6, 2); + lcd_print("PIANO"); + _delay_ms(1000); + lcd_clear(); + _delay_ms(1000); + while (buttonState == 0) { + char randomValue = getNote(); + + if (bufferIndex == 16) { + // Remove the first value and shift other values down + for (int i = 0; i < 16 - 1; i++) { + circularBuffer[i] = circularBuffer[i + 1]; + } + // Add the new value at the end + circularBuffer[16 - 1] = randomValue; + } else { + // Add the new value to the buffer + circularBuffer[bufferIndex] = randomValue; + bufferIndex++; + } + + lcd_clear(); + lcd_gotoxy(1,1); + for (int i = 0; i <= bufferIndex-1; i++) { + char singleChar[2] = {circularBuffer[bufferIndex- 1 - i], '\0'}; + lcd_gotoxy(i+1,1); + lcd_print(singleChar); // Print the single character + lcd_gotoxy(6, 2); + lcd_print("PIANO"); + } + + _delay_ms(1000); + } + bufferIndex = 0; + lcd_clear(); +} + //******************************************************* int main(void) { lcd_init(); lcd_hideCursor(); + usart_init(); setup(); lcd_clear(); while (1) { @@ -266,9 +304,7 @@ int main(void) { displayNotes(); } else { - lcd_gotoxy(6,2); - lcd_print("PIANO"); - _delay_ms(1000); + displayPiano(); } } return 0;