pse-uisim/main.c
2023-12-01 00:43:49 +01:00

191 lines
5.5 KiB
C

#include "main.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_pixels.h>
#include <pthread.h>
#include <unistd.h>
#include "lvgl.h"
#include "PSE_unit.h"
#include "home_screen.h"
#include "PSE_unit.h"
#include "pse_stepper_planer.h"
#define BUFF_SIZE (320 * 10)
#define LCD_WIDTH 320
#define LCD_HEIGHT 240
static lv_disp_draw_buf_t disp_buf;
static lv_color_t buf_1[BUFF_SIZE];
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
TIM_HandleTypeDef htim2;
TIM_HandleTypeDef htim3;
TIM_HandleTypeDef htim4;
TIM_HandleTypeDef htim5;
pse_unit pse_units[PSE_UNITS_NUM];
pse_syringe pse_syringes[PSE_UNITS_NUM];
pse_home_display pse_home_displays[PSE_UNITS_NUM];
pse_stepper_status pse_steppers_status[PSE_UNITS_NUM];
enum steppers_axis{PSE_X_STEPPER, PSE_Y_STEPPER, PSE_Z_STEPPER, PSE_E_STEPPER, PSE_STEPPER_NUM};
pse_stepper_conf pse_stepper_confs[PSE_STEPPER_NUM] = {
[PSE_X_STEPPER] = {
.DIR_GPIO_Port = X_STEPPER_DIR_GPIO_Port,
.DIR_GPIO_Pin = X_STEPPER_DIR_Pin,
.EN_GPIO_Port = X_STEPPER_EN_GPIO_Port,
.EN_GPIO_Pin = X_STEPPER_EN_Pin,
.STEP_GPIO_Port = X_STEPPER_STEP_GPIO_Port,
.STEP_GPIO_Pin = X_STEPPER_STEP_Pin,
.tim = &htim2,
},
[PSE_Y_STEPPER] = {
.DIR_GPIO_Port = Y_STEPPER_DIR_GPIO_Port,
.DIR_GPIO_Pin = Y_STEPPER_DIR_Pin,
.EN_GPIO_Port = Y_STEPPER_EN_GPIO_Port,
.EN_GPIO_Pin = Y_STEPPER_EN_Pin,
.STEP_GPIO_Port = Y_STEPPER_STEP_GPIO_Port,
.STEP_GPIO_Pin = Y_STEPPER_STEP_Pin,
.tim = &htim3,
},
[PSE_Z_STEPPER] = {
.DIR_GPIO_Port = Z_STEPPER_DIR_GPIO_Port,
.DIR_GPIO_Pin = Z_STEPPER_DIR_Pin,
.EN_GPIO_Port = Z_STEPPER_EN_GPIO_Port,
.EN_GPIO_Pin = Z_STEPPER_EN_Pin,
.STEP_GPIO_Port = Z_STEPPER_STEP_GPIO_Port,
.STEP_GPIO_Pin = Z_STEPPER_STEP_Pin,
.tim = &htim4,
},
[PSE_E_STEPPER] = {
.DIR_GPIO_Port = E_STEPPER_DIR_GPIO_Port,
.DIR_GPIO_Pin = E_STEPPER_DIR_Pin,
.EN_GPIO_Port = E_STEPPER_EN_GPIO_Port,
.EN_GPIO_Pin = E_STEPPER_EN_Pin,
.STEP_GPIO_Port = E_STEPPER_STEP_GPIO_Port,
.STEP_GPIO_Pin = E_STEPPER_STEP_Pin,
.tim = &htim5,
},
};
int main(int argc, char** argv){
if(SDL_Init( SDL_INIT_VIDEO ) < 0){
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
exit(-1);
}
window = SDL_CreateWindow("PSE emulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, LCD_WIDTH, LCD_HEIGHT, SDL_WINDOW_SHOWN );
if(window == NULL){
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
}
screenSurface = SDL_GetWindowSurface(window);
lv_init();
lv_disp_draw_buf_init(&disp_buf, buf_1, NULL, BUFF_SIZE);
lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.draw_buf = &disp_buf;
disp_drv.hor_res = LCD_WIDTH;
disp_drv.ver_res = LCD_HEIGHT;
disp_drv.flush_cb = &my_flush_cb;
lv_disp_drv_register(&disp_drv);
lv_disp_t * disp = lv_disp_drv_register(&disp_drv);
lv_theme_t * th = lv_theme_default_init(lv_disp_get_default(), lv_color_hex(0x2BAEE1), lv_color_hex(0x001361), true, LV_FONT_DEFAULT);
lv_disp_set_theme(lv_disp_get_default(), th);
lv_indev_drv_t indev_drv;
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = &touchscreen_read_callback;
lv_indev_t * my_indev = lv_indev_drv_register(&indev_drv);
LV_IMG_DECLARE(logo_mint_resize);
lv_obj_t * logo_mint = lv_img_create(lv_scr_act());
lv_img_set_src(logo_mint, &logo_mint_resize);
lv_obj_align(logo_mint, LV_ALIGN_CENTER, 0, 0);
lv_obj_set_size(logo_mint, 320, 240);
lv_obj_t* cursor_pos = lv_label_create(lv_scr_act());
lv_obj_t* cursor = lv_label_create(lv_scr_act());
lv_label_set_text(cursor, "A");
lv_indev_set_cursor(my_indev, cursor);
// load first workspace from sd
load_units(pse_units, pse_syringes, pse_stepper_confs, pse_steppers_status, pse_home_displays, PSE_UNITS_NUM, 0);
Home_Screen_Gen(pse_units, PSE_UNITS_NUM, false);
pthread_t tickThread;
pthread_create(&tickThread, NULL, tick_thread, NULL);
while(1){
lv_timer_handler();
int x,y;
Uint32 button = SDL_GetMouseState(&x, &y);
lv_label_set_text_fmt(cursor_pos, "%d : %d", x, y);
SDL_Event e;
SDL_PollEvent(&e);
if(e.type == SDL_QUIT)
break;
}
SDL_DestroyWindow(window);
SDL_Quit();
}
void my_flush_cb(lv_disp_drv_t * disp, const lv_area_t * area, lv_color_t * buf){
int32_t x, y;
for(y = area->y1; y <= area->y2; y++) {
for(x = area->x1; x <= area->x2; x++) {
Uint32 * const target_pixel = (Uint32 *) ((Uint8 *) screenSurface->pixels + y * screenSurface->pitch + x * screenSurface->format->BytesPerPixel);
Uint16 col = *(Uint16*)buf;
*target_pixel = SDL_MapRGB(screenSurface->format, (col & 0xF800) >> 8, (col & 0x07E0) >> 3, (col & 0x001F) << 3);
buf++;
}
}
SDL_UpdateWindowSurface(window);
/* IMPORTANT!!!
* Inform LVGL that you are ready with the flushing and buf is not used anymore*/
lv_disp_flush_ready(disp);
}
void touchscreen_read_callback(lv_indev_drv_t * drv, lv_indev_data_t*data){
int x, y;
Uint32 button = SDL_GetMouseState(&x, &y);
if(SDL_BUTTON(button) == 1) {
data->state = LV_INDEV_STATE_PRESSED;
} else {
data->state = LV_INDEV_STATE_RELEASED;
}
data->point.x = x;
data->point.y = y;
}
void * tick_thread (void *args){
while(1) {
usleep(5*1000); /*Sleep for 5 millisecond*/
lv_tick_inc(5); /*Tell LVGL that 5 milliseconds were elapsed*/
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim){
for(int i = 0; i < PSE_UNITS_NUM; i++){
if(htim == pse_units[i].stepper_conf->tim){
pse_stepper_planer_tick(&pse_units[i]);
}
}
}