diff --git a/SDPMcu/Core/Src/main.c b/SDPMcu/Core/Src/main.c index 68850d0..5e4fc0f 100644 --- a/SDPMcu/Core/Src/main.c +++ b/SDPMcu/Core/Src/main.c @@ -18,6 +18,7 @@ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" +#include "cJSON.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ @@ -47,13 +48,10 @@ __IO uint32_t BspButtonState = BUTTON_RELEASED; UART_HandleTypeDef huart1; /* USER CODE BEGIN PV */ -uint8_t tx_buffer[27] = "Welcome to BinaryUpdates!\n\r"; -uint8_t rx_indx; -uint8_t rx_data[6]; - -uint8_t rx_buffer[100]; -uint8_t transfer_cplt; - +uint8_t rx_data[1]; // Receive data one byte at a time +uint8_t rx_buffer[202]; // Buffer to accumulate the incoming message +uint8_t rx_index = 0; // Index to track buffer position +int message_received = 0; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ @@ -106,7 +104,7 @@ int main(void) /* Initialize leds */ BSP_LED_Init(LED_GREEN); - /* Initialize USER push-button, will be used to trigger an interrupt each time it's pressed.*/ + /* Initialize USER push-button, will be used to trigger an interrupt each time it's pressed. */ BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI); /* Initialize COM1 port (115200, 8 bits (7-bit data + 1 stop bit), no parity */ @@ -123,7 +121,6 @@ int main(void) /* USER CODE BEGIN BSP */ /* -- Sample board code to send message over COM1 port ---- */ - printf("Welcome to STM32 world !\n\r"); /* -- Sample board code to switch on leds ---- */ BSP_LED_On(LED_GREEN); @@ -134,23 +131,51 @@ int main(void) /* USER CODE BEGIN WHILE */ while (1) { -// HAL_UART_Transmit(&huart1, tx_buffer, 27, 100); -// HAL_Delay(1000); - /* -- Sample board code for User push-button in interrupt mode ---- */ - HAL_UART_Receive_IT(&huart1, rx_data, 6); - + // Start receiving UART data byte by byte + HAL_UART_Receive_IT(&huart1, rx_data, 1); + if (message_received){ + printf("%s\n",rx_buffer); + cJSON *json = cJSON_Parse((char *)rx_buffer); + if (json == NULL) // Check if the parsing failed + { + printf("Error parsing JSON\n"); + } + else + { + // Iterate over the JSON object and print out each key-value pair + cJSON *item = NULL; + cJSON_ArrayForEach(item, json) // Loop through each item in the JSON object + { + // Get the key (the numbered key in your JSON) + const char *key = item->string; + // Check if the value is a string + if (cJSON_IsString(item) && (item->valuestring != NULL)) + { + // Print the key (pin number) and its value (state) + printf("Pin %s: %s\n", key, item->valuestring); + } + } + + + // Always delete the JSON object to avoid memory leaks + cJSON_Delete(json); + } + // Clear the flag for the next message + message_received = 0; + } if (BspButtonState == BUTTON_PRESSED) { /* Update button state */ BspButtonState = BUTTON_RELEASED; /* -- Sample board code to toggle leds ---- */ - BSP_LED_Toggle(LED_GREEN); +// BSP_LED_Toggle(LED_GREEN) /* ..... Perform your action ..... */ } - /* USER CODE END WHILE */ + /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ + } /* USER CODE END 3 */ } @@ -257,21 +282,43 @@ static void MX_GPIO_Init(void) } /* USER CODE BEGIN 4 */ +int _write(int file, char *ptr, int len) { + HAL_UART_Transmit(&huart1, (uint8_t*)ptr, len, HAL_MAX_DELAY); + return len; +} + void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { /* Prevent unused argument(s) compilation warning */ UNUSED(huart); - /* NOTE : This function should not be modified, when the callback is needed, - the HAL_UART_RxCpltCallback can be implemented in the user file. - */ - uint8_t response[]= "Received!\n"; - HAL_UART_Transmit(&huart1,response, sizeof(response)-1, 10); + if (huart->Instance == USART1) // Check if the interrupt is for UART1 + { + // If received character is not newline, accumulate it in the buffer + if (rx_data[0] != '\n') + { + rx_buffer[rx_index++] = rx_data[0]; // Store received byte in buffer + if (rx_index >= sizeof(rx_buffer)) // Prevent buffer overflow + { + rx_index = 0; // Reset index if the buffer is full + } + } + else + { + // When newline character is received, complete the message and reset buffer + rx_buffer[rx_index] = '\0'; // Null-terminate the string + message_received =1; + rx_index = 0; // Reset the buffer index for the next message + } + + // Continue receiving next byte + HAL_UART_Receive_IT(&huart1, rx_data, 1); + } } /* USER CODE END 4 */ /** - * @brief BSP Push Button callback + * @brief BSP Push Button callback * @param Button Specifies the pressed button * @retval None */