From 2c182246211ff0aec491e7c13febfffabf93e749 Mon Sep 17 00:00:00 2001 From: Dmitry Maylarov Date: Sun, 14 Jul 2019 14:01:28 +0300 Subject: [PATCH] memset after sending string --- .vscode/uberdevice.code-workspace | 3 ++- Core/Src/lcd.c | 11 +++++++---- Core/Src/main.c | 29 +++++++++++++---------------- Makefile | 4 ++-- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/.vscode/uberdevice.code-workspace b/.vscode/uberdevice.code-workspace index 0643704..b501710 100644 --- a/.vscode/uberdevice.code-workspace +++ b/.vscode/uberdevice.code-workspace @@ -8,7 +8,8 @@ "files.associations": { "task.h": "c", "music.h": "c", - "usb_device.h": "c" + "usb_device.h": "c", + "main.h": "c" } } } \ No newline at end of file diff --git a/Core/Src/lcd.c b/Core/Src/lcd.c index 6ff95f9..d16324d 100644 --- a/Core/Src/lcd.c +++ b/Core/Src/lcd.c @@ -1,6 +1,8 @@ #ifndef __LCD_H #define __LCD_H + #include "lcd.h" +#include #include "FreeRTOS.h" #include "task.h" @@ -114,17 +116,17 @@ void LCD_Init(LCD_HandleTypeDef *lcd) { vTaskDelay(5); LCD_SendCommand(lcd, LCD_FUNCTIONSET | LCD_4BITMODE); vTaskDelay(1); - LCD_SendCommand(lcd, LCD_FUNCTIONSET | LCD_4BITMODE | LCD_SINGLELINE | LCD_5x8DOTS); // 0b00110000); + LCD_SendCommand(lcd, LCD_FUNCTIONSET | LCD_4BITMODE | LCD_SINGLELINE | LCD_5x8DOTS); vTaskDelay(5); - LCD_SendCommand(lcd, LCD_FUNCTIONSET | LCD_4BITMODE | LCD_SINGLELINE | LCD_5x8DOTS); // 0b00110000); + LCD_SendCommand(lcd, LCD_FUNCTIONSET | LCD_4BITMODE | LCD_SINGLELINE | LCD_5x8DOTS); vTaskDelay(5); - LCD_SendCommand(lcd, LCD_FUNCTIONSET | LCD_4BITMODE | LCD_MULTILINE | LCD_5x8DOTS); // 0b00110000); + LCD_SendCommand(lcd, LCD_FUNCTIONSET | LCD_4BITMODE | LCD_MULTILINE | LCD_5x8DOTS); vTaskDelay(5); // display & cursor home (keep this!) LCD_SendCommand(lcd, LCD_RETURNHOME); vTaskDelay(5); // display on, right shift, underline off, blink off - LCD_SendCommand(lcd, LCD_DISPLAYCONTROL|LCD_DISPLAYON|LCD_CURSOROFF|LCD_BLINKOFF); // 0b00001100); + LCD_SendCommand(lcd, LCD_DISPLAYCONTROL|LCD_DISPLAYON|LCD_CURSOROFF|LCD_BLINKOFF); vTaskDelay(5); // clear display (optional here) LCD_SendCommand(lcd, LCD_CLEARDISPLAY); @@ -141,6 +143,7 @@ void LCD_SendString(LCD_HandleTypeDef *lcd, char *str) { LCD_SendData(lcd, (uint8_t)(*str)); str++; } + memset(&str,'\0', sizeof(str)); } #endif \ No newline at end of file diff --git a/Core/Src/main.c b/Core/Src/main.c index eb28d78..9265987 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -71,7 +71,6 @@ UART_HandleTypeDef huart1; /* USER CODE BEGIN PV */ -xSemaphoreHandle buttonPressed; GameEngine game; LCD_HandleTypeDef hlcd = { &hi2c1, @@ -97,9 +96,10 @@ Button *plusButton = &buttons[1]; Button *minusButton = &buttons[2]; Button *bigButton = &buttons[0]; -TimerHandle_t secondsTimerHandle = NULL; +TimerHandle_t xSecondsTimerHandle = NULL; TaskHandle_t xMusicHandle = NULL; TaskHandle_t xLCDUpdaterHandle = NULL; +xSemaphoreHandle xButtonPressed; asm( "tetris:\n\t" @@ -133,7 +133,7 @@ void PrintTime() { LCD_MoveCursor(&hlcd, 0, 0); sprintf( lcdBuffer, - (game.timerValue > 0 ? " %1ld:%02d " : " -%ld:%02d "), + (game.timerValue > 0 ? " %1d:%02d " : " -%d:%02d "), abs(game.timerValue / 60), abs(game.timerValue % 60) ); @@ -476,7 +476,7 @@ void vTaskPlayerSetup(void *parameter) sprintf(lcdBuffer, "Players: %1d ", game.activePlayers); LCD_SendString(&hlcd, lcdBuffer); vTaskDelay(1); - if (xSemaphoreTake(buttonPressed, portMAX_DELAY) == pdPASS ) + if (xSemaphoreTake(xButtonPressed, portMAX_DELAY) == pdPASS ) { if (plusButton->pressed) { @@ -512,7 +512,7 @@ void vTaskTimerSetup(void *parameter) { sprintf(lcdBuffer, "Turn time: %1ld:%02ld ",game.turnTime / 60, game.turnTime % 60); LCD_SendString(&hlcd, lcdBuffer); vTaskDelay(1); - if (xSemaphoreTake(buttonPressed, portMAX_DELAY) == pdPASS ) + if (xSemaphoreTake(xButtonPressed, portMAX_DELAY) == pdPASS ) { if (plusButton->pressed) { @@ -530,7 +530,7 @@ void vTaskTimerSetup(void *parameter) { } } - secondsTimerHandle = xTimerCreate("SecondsTimer", + xSecondsTimerHandle = xTimerCreate("SecondsTimer", pdMS_TO_TICKS(1000), //counts 1 sec pdTRUE, //auto-reload NULL, //not assigning ID @@ -555,7 +555,7 @@ void vTaskConfig(void *parameter) { sprintf(lcdBuffer, "Count scores: %3s ", game.countScores ? "yes" : "no"); LCD_SendString(&hlcd, lcdBuffer); vTaskDelay(1); - if (xSemaphoreTake(buttonPressed, portMAX_DELAY) == pdPASS ) { + if (xSemaphoreTake(xButtonPressed, portMAX_DELAY) == pdPASS ) { if (plusButton->pressed) game.countScores = !game.countScores; if (minusButton->pressed) @@ -591,12 +591,11 @@ void vTaskTurn(void *parameter) { TickType_t xLastWakeTime; xLastWakeTime = xTaskGetTickCount(); - xTimerReset(secondsTimerHandle, 0); + xTimerReset(xSecondsTimerHandle, 0); LCD_MoveHome(&hlcd); if (game.countScores) { - memset(&lcdBuffer,'\0', sizeof(lcdBuffer)); LCD_MoveCursor(&hlcd, 2, 1); for (int i = 1; i <= game.activePlayers; i++) { @@ -606,12 +605,10 @@ void vTaskTurn(void *parameter) { if (strlen(lcdBuffer) > (LCD_COLS-6)) { LCD_SendString(&hlcd, lcdBuffer); - memset(&lcdBuffer,'\0', sizeof(lcdBuffer)); LCD_MoveCursor(&hlcd, 3, 1); } } LCD_SendString(&hlcd, lcdBuffer); - memset(&lcdBuffer,'\0', sizeof(lcdBuffer)); } LCD_MoveCursor(&hlcd, 1, 0); @@ -620,7 +617,7 @@ void vTaskTurn(void *parameter) { while (1) { - if (xSemaphoreTake(buttonPressed, portMAX_DELAY) == pdPASS ) + if (xSemaphoreTake(xButtonPressed, portMAX_DELAY) == pdPASS ) { if (plusButton->pressed) { xTaskCreate(vTaskTimerSetup, "TaskTimerSetup", configMINIMAL_STACK_SIZE, NULL, 1, NULL); @@ -638,7 +635,7 @@ void vTaskTurn(void *parameter) { vTaskDelayUntil(&xLastWakeTime,100); // HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin); } - xTimerStop(secondsTimerHandle, 0); + xTimerStop(xSecondsTimerHandle, 0); if (xMusicHandle) { vTaskDelete(xMusicHandle); @@ -665,7 +662,7 @@ void vTaskTurnEnd(void *parameter) { LCD_MoveCursor(&hlcd, 2, 0); sprintf(lcdBuffer, "Result: %+4ld", delta); LCD_SendString(&hlcd, lcdBuffer); - if (xSemaphoreTake(buttonPressed, portMAX_DELAY) == pdPASS ) { + if (xSemaphoreTake(xButtonPressed, portMAX_DELAY) == pdPASS ) { if (bigButton->pressed) { ChangeScore(delta); break; @@ -715,7 +712,7 @@ void vTimerCallback(TimerHandle_t xTimer) { } void vTaskButtonPoll(void *parameter) { - buttonPressed = xSemaphoreCreateBinary(); + xButtonPressed = xSemaphoreCreateBinary(); bool state = false; while (1) { for(int i = 0; i