Add V2 implementation (not tested)

This commit is contained in:
jonny 2025-11-17 10:51:02 +01:00
parent 8c10217a89
commit cf3ebbc7e0
4 changed files with 11576 additions and 9126 deletions

View File

@ -23,6 +23,7 @@
/* USER CODE BEGIN Includes */
#include "display.h"
#include "fonts.h"
#include <stdbool.h>
/* USER CODE END Includes */
@ -41,6 +42,17 @@
#define BUTTON_GPIO_PORT GPIOA
#define BUTTON_GPIO_PIN GPIO_PIN_0
#define TOUCH_EVENT_IS_PRESSED 8
#define TOUCH_EVENT_NOT_PRESSED 4
#define TOUCH_EVENT_JUST_PRESSED 0
// calibrate touch display
#define CAL_TOUCH_TOP_LEFT_X 0
#define CAL_TOUCH_TOP_LEFT_Y 0
#define CAL_TOUCH_BOT_RIGHT_X 200
#define CAL_TOUCH_BOT_RIGHT_Y 200
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
@ -87,6 +99,26 @@ uint32_t time_msPassedSince(uint32_t timestampOld)
static inline int32_t clamp_i16(int32_t value, int32_t min, int32_t max)
{
if (value < min) return min;
if (value > max) return max;
return value;
}
// function for drawing a button either pressed or released
void drawButton(bool isPressed){
if (isPressed){
Display_FillRect( 75, 130, 100, 100, LCD_COLOR_RED );
Display_DrawRect( 74, 129, 102, 102, LCD_COLOR_RED );
}
else {
Display_FillRect( 75, 130, 100, 100, LCD_COLOR_BLACK );
Display_DrawRect( 74, 129, 102, 102, LCD_COLOR_RED );
}
}
/* USER CODE END 0 */
@ -126,10 +158,18 @@ int main(void)
Display_Init();
// local variables
uint32_t timestamp_lastBlinked = HAL_GetTick();
uint32_t timestamp_lastButtonPolled = timestamp_lastBlinked;
uint32_t timestamp_lastHelloPrinted = timestamp_lastBlinked;
uint8_t printCount = 0;
uint32_t timestamp_lastCounted = HAL_GetTick();
uint32_t timestamp_lastTouchRead = HAL_GetTick();
uint8_t i = 100;
uint8_t DataRx[4];
uint8_t Eventflag = TOUCH_EVENT_NOT_PRESSED, EventflagLast = TOUCH_EVENT_NOT_PRESSED;
uint16_t xRaw, yRaw;
int32_t xTouch, yTouch;
bool buttonIsPressed;
drawButton(false);
/* USER CODE END 2 */
@ -137,44 +177,72 @@ int main(void)
/* USER CODE BEGIN WHILE */
while (1)
{
uint8_t DataRx[4];
//...
uint8_t Eventflag;
uint16_t xTouch, yTouch;
Eventflag = (uint8_t)(DataRx[0] >> 4);
yTouch = (uint16_t)((DataRx[0] & 0b00001111) << 8) | (uint16_t)DataRx[1];
xTouch = (uint16_t)((DataRx[2] & 0b00001111) << 8) | (uint16_t)DataRx[3];
// blink blue led (toggle state every 500ms)
if (time_msPassedSince(timestamp_lastBlinked) >= 500) {
//HAL_GPIO_TogglePin(LED_BLUE_GPIO_PORT, LED_BLUE_GPIO_PIN);
HAL_GPIO_TogglePin(LED_BLUE_GPIO_PORT, LED_BLUE_GPIO_PIN);
//printf("toggle blue led\n\r");
timestamp_lastBlinked = HAL_GetTick();
// display 100 count down
if (time_msPassedSince(timestamp_lastCounted) >= 100) {
timestamp_lastCounted = HAL_GetTick();
Display_Printf( 0, 10,
LCD_COLOR_WHITE, LCD_COLOR_BLACK,
&FontBig,
"Zahl %d", i
);
if (i == 0) i = 100;
else i--;
}
// poll button every 25ms and set red-led accordingly
if (time_msPassedSince(timestamp_lastButtonPolled) >= 25) {
GPIO_PinState buttonState = HAL_GPIO_ReadPin(BUTTON_GPIO_PORT, BUTTON_GPIO_PIN);
//printf("buttonState = %d\n\r", buttonState);
HAL_GPIO_WritePin(LED_RED_GPIO_PORT, LED_RED_GPIO_PIN, buttonState);
timestamp_lastButtonPolled = HAL_GetTick();
}
// print "Hallo Welt" via UART every 1000ms
if (time_msPassedSince(timestamp_lastHelloPrinted) >= 1000) {
printf("Hallo Welt %d\n\r", printCount++);
timestamp_lastHelloPrinted = HAL_GetTick();
}
// every 20ms: read touch, print values, convert values, draw pixel, handle button
if (time_msPassedSince(timestamp_lastTouchRead) >= 20) { // run this block every 20ms
timestamp_lastTouchRead = HAL_GetTick();
// read touch register
HAL_I2C_Mem_Read(I2C3, 112, 3, I2C_MEMADD_SIZE_8BIT, DataRx, 4, 1000);
// interpret received data
EventflagLast = Eventflag;
Eventflag = (uint8_t)(DataRx[0] >> 4);
yRaw = (uint16_t)((DataRx[0] & 0b00001111) << 8) | (uint16_t)DataRx[1];
xRaw = (uint16_t)((DataRx[2] & 0b00001111) << 8) | (uint16_t)DataRx[3];
// show pos values on display
if (Eventflag == TOUCH_EVENT_IS_PRESSED) printf("is pressed \n");
Display_Printf( 0, 30,
LCD_COLOR_WHITE, LCD_COLOR_BLACK,
&FontBig,
"x=%d y=%d", xRaw, yRaw
);
//scale to display size
xTouch = 239 * ((int32_t)xRaw - CAL_TOUCH_TOP_LEFT_X) / ((int32_t)CAL_TOUCH_BOT_RIGHT_X - CAL_TOUCH_TOP_LEFT_X);
yTouch = 239 * ((int32_t)yRaw - CAL_TOUCH_TOP_LEFT_Y) / ((int32_t)CAL_TOUCH_BOT_RIGHT_Y - CAL_TOUCH_TOP_LEFT_Y);
xTouch = clamp_i16(xTouch, 0, 239);
yTouch = clamp_i16(yTouch, 0, 239);
// draw position when pressed
if (Eventflag == TOUCH_EVENT_IS_PRESSED){
Display_DrawPixel( (uint16_t)xTouch, (uint16_t)yTouch, LCD_COLOR_WHITE );
}
// determine button pressed condition
bool isInsideButtonArea = xTouch > 75 && xTouch < (75+100) && yTouch > 130 && yTouch < (130+100);
bool touchIsPressed = Eventflag == TOUCH_EVENT_IS_PRESSED;
bool buttonIsPressedLast = buttonIsPressed;
buttonIsPressed = isInsideButtonArea && touchIsPressed;
// just got pressed -> draw pressed button
if (buttonIsPressed && !buttonIsPressedLast) drawButton(true);
// just got released -> draw released button
if (!buttonIsPressed && buttonIsPressedLast) drawButton(false);
} // end "run every 20ms"
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
} //end while(1)
/* USER CODE END 3 */
}

View File

@ -1,10 +1,12 @@
../Core/Src/main.c:76:5:_write 1
../Core/Src/main.c:83:10:time_msPassedSince 1
../Core/Src/main.c:97:5:main 4
../Core/Src/main.c:185:6:SystemClock_Config 4
../Core/Src/main.c:242:13:MX_I2C3_Init 4
../Core/Src/main.c:290:13:MX_USART6_UART_Init 2
../Core/Src/main.c:321:13:MX_FMC_Init 2
../Core/Src/main.c:379:13:MX_GPIO_Init 1
../Core/Src/main.c:757:6:HAL_TIM_PeriodElapsedCallback 2
../Core/Src/main.c:775:6:Error_Handler 1
../Core/Src/main.c:81:5:_write 1
../Core/Src/main.c:88:10:time_msPassedSince 1
../Core/Src/main.c:95:23:clamp_i16 3
../Core/Src/main.c:103:6:drawButton 2
../Core/Src/main.c:122:5:main 16
../Core/Src/main.c:249:6:SystemClock_Config 4
../Core/Src/main.c:306:13:MX_I2C3_Init 4
../Core/Src/main.c:354:13:MX_USART6_UART_Init 2
../Core/Src/main.c:385:13:MX_FMC_Init 2
../Core/Src/main.c:443:13:MX_GPIO_Init 1
../Core/Src/main.c:821:6:HAL_TIM_PeriodElapsedCallback 2
../Core/Src/main.c:839:6:Error_Handler 1

View File

@ -1,10 +1,12 @@
../Core/Src/main.c:76:5:_write 24 static
../Core/Src/main.c:83:10:time_msPassedSince 16 static
../Core/Src/main.c:97:5:main 32 static
../Core/Src/main.c:185:6:SystemClock_Config 88 static
../Core/Src/main.c:242:13:MX_I2C3_Init 8 static
../Core/Src/main.c:290:13:MX_USART6_UART_Init 8 static
../Core/Src/main.c:321:13:MX_FMC_Init 40 static
../Core/Src/main.c:379:13:MX_GPIO_Init 64 static
../Core/Src/main.c:757:6:HAL_TIM_PeriodElapsedCallback 16 static
../Core/Src/main.c:775:6:Error_Handler 4 static,ignoring_inline_asm
../Core/Src/main.c:81:5:_write 24 static
../Core/Src/main.c:88:10:time_msPassedSince 16 static
../Core/Src/main.c:95:23:clamp_i16 24 static
../Core/Src/main.c:103:6:drawButton 24 static
../Core/Src/main.c:122:5:main 56 static
../Core/Src/main.c:249:6:SystemClock_Config 88 static
../Core/Src/main.c:306:13:MX_I2C3_Init 8 static
../Core/Src/main.c:354:13:MX_USART6_UART_Init 8 static
../Core/Src/main.c:385:13:MX_FMC_Init 40 static
../Core/Src/main.c:443:13:MX_GPIO_Init 64 static
../Core/Src/main.c:821:6:HAL_TIM_PeriodElapsedCallback 16 static
../Core/Src/main.c:839:6:Error_Handler 4 static,ignoring_inline_asm

File diff suppressed because it is too large Load Diff