I am just curious as to why do we have to type cast buf to char, and what does this type cast do? Does it change the data type of buf to char?
uint8_t buf[12];
int16_t val;
float temp_c;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
uint16_t lux;
int steps = 100;
while (1)
{
buf[0] = REG_LUX;
ret = HAL_I2C_Master_Transmit(&hi2c1, BH1750_addr, buf, 1 , HAL_MAX_DELAY);
if (ret != HAL_OK) {
strcpy((char*)buf, "ERROR tx \r\n");
} else {
ret = HAL_I2C_Master_Receive(&hi2c1, BH1750_addr, buf, 2, HAL_MAX_DELAY);
if (ret != HAL_OK) {
strcpy((char*)buf, "ERROR rx \r\n");
} else {
// Combine the two bytes to get the lux value
lux = ((uint16_t)buf[0] << 8) | buf[1];
sprintf((char*)buf, "Lux: %u\r\n", lux);
}
}