memset after sending string

master
Dmitry Maylarov 7 years ago
parent 6ef14d0149
commit 2c18224621

@ -8,7 +8,8 @@
"files.associations": { "files.associations": {
"task.h": "c", "task.h": "c",
"music.h": "c", "music.h": "c",
"usb_device.h": "c" "usb_device.h": "c",
"main.h": "c"
} }
} }
} }

@ -1,6 +1,8 @@
#ifndef __LCD_H #ifndef __LCD_H
#define __LCD_H #define __LCD_H
#include "lcd.h" #include "lcd.h"
#include <string.h>
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "task.h" #include "task.h"
@ -114,17 +116,17 @@ void LCD_Init(LCD_HandleTypeDef *lcd) {
vTaskDelay(5); vTaskDelay(5);
LCD_SendCommand(lcd, LCD_FUNCTIONSET | LCD_4BITMODE); LCD_SendCommand(lcd, LCD_FUNCTIONSET | LCD_4BITMODE);
vTaskDelay(1); 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); 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); 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); vTaskDelay(5);
// display & cursor home (keep this!) // display & cursor home (keep this!)
LCD_SendCommand(lcd, LCD_RETURNHOME); LCD_SendCommand(lcd, LCD_RETURNHOME);
vTaskDelay(5); vTaskDelay(5);
// display on, right shift, underline off, blink off // 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); vTaskDelay(5);
// clear display (optional here) // clear display (optional here)
LCD_SendCommand(lcd, LCD_CLEARDISPLAY); LCD_SendCommand(lcd, LCD_CLEARDISPLAY);
@ -141,6 +143,7 @@ void LCD_SendString(LCD_HandleTypeDef *lcd, char *str) {
LCD_SendData(lcd, (uint8_t)(*str)); LCD_SendData(lcd, (uint8_t)(*str));
str++; str++;
} }
memset(&str,'\0', sizeof(str));
} }
#endif #endif

@ -71,7 +71,6 @@ UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */ /* USER CODE BEGIN PV */
xSemaphoreHandle buttonPressed;
GameEngine game; GameEngine game;
LCD_HandleTypeDef hlcd = { LCD_HandleTypeDef hlcd = {
&hi2c1, &hi2c1,
@ -97,9 +96,10 @@ Button *plusButton = &buttons[1];
Button *minusButton = &buttons[2]; Button *minusButton = &buttons[2];
Button *bigButton = &buttons[0]; Button *bigButton = &buttons[0];
TimerHandle_t secondsTimerHandle = NULL; TimerHandle_t xSecondsTimerHandle = NULL;
TaskHandle_t xMusicHandle = NULL; TaskHandle_t xMusicHandle = NULL;
TaskHandle_t xLCDUpdaterHandle = NULL; TaskHandle_t xLCDUpdaterHandle = NULL;
xSemaphoreHandle xButtonPressed;
asm( asm(
"tetris:\n\t" "tetris:\n\t"
@ -133,7 +133,7 @@ void PrintTime() {
LCD_MoveCursor(&hlcd, 0, 0); LCD_MoveCursor(&hlcd, 0, 0);
sprintf( sprintf(
lcdBuffer, lcdBuffer,
(game.timerValue > 0 ? " %1ld:%02d " : " -%ld:%02d "), (game.timerValue > 0 ? " %1d:%02d " : " -%d:%02d "),
abs(game.timerValue / 60), abs(game.timerValue / 60),
abs(game.timerValue % 60) abs(game.timerValue % 60)
); );
@ -476,7 +476,7 @@ void vTaskPlayerSetup(void *parameter)
sprintf(lcdBuffer, "Players: %1d ", game.activePlayers); sprintf(lcdBuffer, "Players: %1d ", game.activePlayers);
LCD_SendString(&hlcd, lcdBuffer); LCD_SendString(&hlcd, lcdBuffer);
vTaskDelay(1); vTaskDelay(1);
if (xSemaphoreTake(buttonPressed, portMAX_DELAY) == pdPASS ) if (xSemaphoreTake(xButtonPressed, portMAX_DELAY) == pdPASS )
{ {
if (plusButton->pressed) if (plusButton->pressed)
{ {
@ -512,7 +512,7 @@ void vTaskTimerSetup(void *parameter) {
sprintf(lcdBuffer, "Turn time: %1ld:%02ld ",game.turnTime / 60, game.turnTime % 60); sprintf(lcdBuffer, "Turn time: %1ld:%02ld ",game.turnTime / 60, game.turnTime % 60);
LCD_SendString(&hlcd, lcdBuffer); LCD_SendString(&hlcd, lcdBuffer);
vTaskDelay(1); vTaskDelay(1);
if (xSemaphoreTake(buttonPressed, portMAX_DELAY) == pdPASS ) if (xSemaphoreTake(xButtonPressed, portMAX_DELAY) == pdPASS )
{ {
if (plusButton->pressed) if (plusButton->pressed)
{ {
@ -530,7 +530,7 @@ void vTaskTimerSetup(void *parameter) {
} }
} }
secondsTimerHandle = xTimerCreate("SecondsTimer", xSecondsTimerHandle = xTimerCreate("SecondsTimer",
pdMS_TO_TICKS(1000), //counts 1 sec pdMS_TO_TICKS(1000), //counts 1 sec
pdTRUE, //auto-reload pdTRUE, //auto-reload
NULL, //not assigning ID NULL, //not assigning ID
@ -555,7 +555,7 @@ void vTaskConfig(void *parameter) {
sprintf(lcdBuffer, "Count scores: %3s ", game.countScores ? "yes" : "no"); sprintf(lcdBuffer, "Count scores: %3s ", game.countScores ? "yes" : "no");
LCD_SendString(&hlcd, lcdBuffer); LCD_SendString(&hlcd, lcdBuffer);
vTaskDelay(1); vTaskDelay(1);
if (xSemaphoreTake(buttonPressed, portMAX_DELAY) == pdPASS ) { if (xSemaphoreTake(xButtonPressed, portMAX_DELAY) == pdPASS ) {
if (plusButton->pressed) if (plusButton->pressed)
game.countScores = !game.countScores; game.countScores = !game.countScores;
if (minusButton->pressed) if (minusButton->pressed)
@ -591,12 +591,11 @@ void vTaskTurn(void *parameter) {
TickType_t xLastWakeTime; TickType_t xLastWakeTime;
xLastWakeTime = xTaskGetTickCount(); xLastWakeTime = xTaskGetTickCount();
xTimerReset(secondsTimerHandle, 0); xTimerReset(xSecondsTimerHandle, 0);
LCD_MoveHome(&hlcd); LCD_MoveHome(&hlcd);
if (game.countScores) if (game.countScores)
{ {
memset(&lcdBuffer,'\0', sizeof(lcdBuffer));
LCD_MoveCursor(&hlcd, 2, 1); LCD_MoveCursor(&hlcd, 2, 1);
for (int i = 1; i <= game.activePlayers; i++) for (int i = 1; i <= game.activePlayers; i++)
{ {
@ -606,12 +605,10 @@ void vTaskTurn(void *parameter) {
if (strlen(lcdBuffer) > (LCD_COLS-6)) if (strlen(lcdBuffer) > (LCD_COLS-6))
{ {
LCD_SendString(&hlcd, lcdBuffer); LCD_SendString(&hlcd, lcdBuffer);
memset(&lcdBuffer,'\0', sizeof(lcdBuffer));
LCD_MoveCursor(&hlcd, 3, 1); LCD_MoveCursor(&hlcd, 3, 1);
} }
} }
LCD_SendString(&hlcd, lcdBuffer); LCD_SendString(&hlcd, lcdBuffer);
memset(&lcdBuffer,'\0', sizeof(lcdBuffer));
} }
LCD_MoveCursor(&hlcd, 1, 0); LCD_MoveCursor(&hlcd, 1, 0);
@ -620,7 +617,7 @@ void vTaskTurn(void *parameter) {
while (1) while (1)
{ {
if (xSemaphoreTake(buttonPressed, portMAX_DELAY) == pdPASS ) if (xSemaphoreTake(xButtonPressed, portMAX_DELAY) == pdPASS )
{ {
if (plusButton->pressed) { if (plusButton->pressed) {
xTaskCreate(vTaskTimerSetup, "TaskTimerSetup", configMINIMAL_STACK_SIZE, NULL, 1, NULL); xTaskCreate(vTaskTimerSetup, "TaskTimerSetup", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
@ -638,7 +635,7 @@ void vTaskTurn(void *parameter) {
vTaskDelayUntil(&xLastWakeTime,100); vTaskDelayUntil(&xLastWakeTime,100);
// HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin); // HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
} }
xTimerStop(secondsTimerHandle, 0); xTimerStop(xSecondsTimerHandle, 0);
if (xMusicHandle) { if (xMusicHandle) {
vTaskDelete(xMusicHandle); vTaskDelete(xMusicHandle);
@ -665,7 +662,7 @@ void vTaskTurnEnd(void *parameter) {
LCD_MoveCursor(&hlcd, 2, 0); LCD_MoveCursor(&hlcd, 2, 0);
sprintf(lcdBuffer, "Result: %+4ld", delta); sprintf(lcdBuffer, "Result: %+4ld", delta);
LCD_SendString(&hlcd, lcdBuffer); LCD_SendString(&hlcd, lcdBuffer);
if (xSemaphoreTake(buttonPressed, portMAX_DELAY) == pdPASS ) { if (xSemaphoreTake(xButtonPressed, portMAX_DELAY) == pdPASS ) {
if (bigButton->pressed) { if (bigButton->pressed) {
ChangeScore(delta); ChangeScore(delta);
break; break;
@ -715,7 +712,7 @@ void vTimerCallback(TimerHandle_t xTimer) {
} }
void vTaskButtonPoll(void *parameter) { void vTaskButtonPoll(void *parameter) {
buttonPressed = xSemaphoreCreateBinary(); xButtonPressed = xSemaphoreCreateBinary();
bool state = false; bool state = false;
while (1) { while (1) {
for(int i = 0; i<BUTTON_COUNT; i++) for(int i = 0; i<BUTTON_COUNT; i++)
@ -727,7 +724,7 @@ void vTaskButtonPoll(void *parameter) {
state = HAL_GPIO_ReadPin(buttons[i].port, buttons[i].pin); state = HAL_GPIO_ReadPin(buttons[i].port, buttons[i].pin);
if (state != buttons[i].pressed) if (state != buttons[i].pressed)
buttons[i].pressed = !buttons[i].pressed; buttons[i].pressed = !buttons[i].pressed;
xSemaphoreGive(buttonPressed); xSemaphoreGive(xButtonPressed);
} }
vTaskDelay(50); vTaskDelay(50);
} }

@ -132,9 +132,9 @@ C_INCLUDES = \
# compile gcc flags # compile gcc flags
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -std=gnu11 ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -std=gnu11 -Wformat-overflow=0
CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -std=gnu11 CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -std=gnu11 -Wformat-overflow=0
ifeq ($(DEBUG), 1) ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2 CFLAGS += -g -gdwarf-2

Loading…
Cancel
Save