I am working on a Qt/C++ application. I want to show state of 3 hall sensors so I read it from micro-controller this way:
uint8_t state = getState(whichMotor);
int numChars = sprintf(sBuff, "%d", state);
HAL_UART_Transmit_DMA(&huart3, sBuff, numChars);
State can be any 1, 5, 4, 6, 2 or 3.
Then in my Qt application I have 3 labels and want to show if corresponding bit is set in the number I receive from UART. I am sure I get the correct number, when I show it as integer its fine. But to break it down into 3 bits into three labels I fail as it just shows 1 1 1 or 0 0 0.
Here is my code on PC:
const void MotorWidget::setHallState(const QString& s)
{
int hallState = s.toInt();
ui->lbValueHallC->setText(hallState & 0b100 > 0 ? "1" : "0");
ui->lbValueHallB->setText(hallState & 0b010 > 0 ? "1" : "0");
ui->lbValueHallA->setText(hallState & 0b001 > 0 ? "1" : "0");
}
For example, if I receive hallState
as 5, label C should show "1", label B should show "0" and label A should show "1"...but as I said I only get 111 or 000 regardless of what I receive.
I suspect this might be big-endian little-endian thing...but I have no idea how to fix it