
jeu pacman
Dependencies: BSP_DISCO_F746NG
main.cpp@6:845cfd545a41, 2022-07-12 (annotated)
- Committer:
- mickaelul
- Date:
- Tue Jul 12 14:11:18 2022 +0000
- Revision:
- 6:845cfd545a41
- Parent:
- 5:071136c3eefa
Programme jeu pacman
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JohnnyK | 5:071136c3eefa | 1 | /** |
JohnnyK | 5:071136c3eefa | 2 | * Notebook page https://os.mbed.com/users/JohnnyK/notebook/how-start-with-the-littlevgl/ |
JohnnyK | 5:071136c3eefa | 3 | */ |
JohnnyK | 5:071136c3eefa | 4 | |
JohnnyK | 0:10c4b83c458d | 5 | #include "mbed.h" |
JohnnyK | 0:10c4b83c458d | 6 | #include "lvgl/lvgl.h" |
JohnnyK | 3:4f5dc253eb7b | 7 | #include "hal_stm_lvgl/tft/tft.h" |
JohnnyK | 3:4f5dc253eb7b | 8 | #include "hal_stm_lvgl/touchpad/touchpad.h" |
JohnnyK | 5:071136c3eefa | 9 | #include "lv_demo.h" /*Comment/uncomment will switch between LVGL demo and Hello word example*/ |
JohnnyK | 0:10c4b83c458d | 10 | |
JohnnyK | 3:4f5dc253eb7b | 11 | #define LVGL_TICK 10 //Time tick value for lvgl in ms (1-10msa) |
JohnnyK | 3:4f5dc253eb7b | 12 | #define TICKER_TIME 10ms //modified to miliseconds |
JohnnyK | 0:10c4b83c458d | 13 | |
mickaelul | 6:845cfd545a41 | 14 | |
mickaelul | 6:845cfd545a41 | 15 | //declaration des entrés analogiques qui correspondent aux mouvements du joystick |
mickaelul | 6:845cfd545a41 | 16 | |
mickaelul | 6:845cfd545a41 | 17 | DigitalIn portD13(D13); |
mickaelul | 6:845cfd545a41 | 18 | DigitalIn portD12(D12); |
mickaelul | 6:845cfd545a41 | 19 | DigitalIn portD11(D11); |
mickaelul | 6:845cfd545a41 | 20 | DigitalIn portD10(D10); |
mickaelul | 6:845cfd545a41 | 21 | |
mickaelul | 6:845cfd545a41 | 22 | |
mickaelul | 6:845cfd545a41 | 23 | |
JohnnyK | 3:4f5dc253eb7b | 24 | Ticker ticker; //Ticker for lvgl |
JohnnyK | 3:4f5dc253eb7b | 25 | |
JohnnyK | 5:071136c3eefa | 26 | /* |
JohnnyK | 5:071136c3eefa | 27 | * Callback function for lvgl timing. |
JohnnyK | 5:071136c3eefa | 28 | * Call lv_tick_inc(x) every x milliseconds in a Timer or Task (x should be between 1 and 10). |
JohnnyK | 5:071136c3eefa | 29 | * It is required for the internal timing of LittlevGL. |
JohnnyK | 5:071136c3eefa | 30 | */ |
JohnnyK | 0:10c4b83c458d | 31 | void lv_ticker_func(){ |
JohnnyK | 0:10c4b83c458d | 32 | lv_tick_inc(LVGL_TICK); |
JohnnyK | 0:10c4b83c458d | 33 | } |
JohnnyK | 0:10c4b83c458d | 34 | |
JohnnyK | 5:071136c3eefa | 35 | #ifndef LV_DEMO_H |
JohnnyK | 5:071136c3eefa | 36 | static void event_handler(lv_event_t* event) |
JohnnyK | 3:4f5dc253eb7b | 37 | { |
JohnnyK | 5:071136c3eefa | 38 | lv_event_code_t code = lv_event_get_code(event); |
JohnnyK | 5:071136c3eefa | 39 | if(code == LV_EVENT_CLICKED) { |
JohnnyK | 3:4f5dc253eb7b | 40 | printf("Clicked\n"); |
JohnnyK | 3:4f5dc253eb7b | 41 | } |
JohnnyK | 5:071136c3eefa | 42 | else if(code == LV_EVENT_VALUE_CHANGED) { |
JohnnyK | 3:4f5dc253eb7b | 43 | printf("Toggled\n"); |
JohnnyK | 0:10c4b83c458d | 44 | } |
JohnnyK | 1:627f26953c53 | 45 | } |
JohnnyK | 3:4f5dc253eb7b | 46 | #endif |
mickaelul | 6:845cfd545a41 | 47 | |
mickaelul | 6:845cfd545a41 | 48 | |
mickaelul | 6:845cfd545a41 | 49 | |
mickaelul | 6:845cfd545a41 | 50 | |
mickaelul | 6:845cfd545a41 | 51 | |
mickaelul | 6:845cfd545a41 | 52 | |
mickaelul | 6:845cfd545a41 | 53 | |
mickaelul | 6:845cfd545a41 | 54 | |
mickaelul | 6:845cfd545a41 | 55 | |
JohnnyK | 3:4f5dc253eb7b | 56 | // main() runs in its own thread in the OS |
JohnnyK | 0:10c4b83c458d | 57 | int main() |
JohnnyK | 0:10c4b83c458d | 58 | { |
mickaelul | 6:845cfd545a41 | 59 | |
mickaelul | 6:845cfd545a41 | 60 | //initialisation des variables |
mickaelul | 6:845cfd545a41 | 61 | int mur[480][272]; |
mickaelul | 6:845cfd545a41 | 62 | int i,j; |
mickaelul | 6:845cfd545a41 | 63 | int xpacman=20; |
mickaelul | 6:845cfd545a41 | 64 | int ypacman=40; |
mickaelul | 6:845cfd545a41 | 65 | int xfantom=80; |
mickaelul | 6:845cfd545a41 | 66 | int yfantom=80; |
mickaelul | 6:845cfd545a41 | 67 | |
mickaelul | 6:845cfd545a41 | 68 | |
mickaelul | 6:845cfd545a41 | 69 | |
JohnnyK | 3:4f5dc253eb7b | 70 | printf("LVGL-"); |
JohnnyK | 3:4f5dc253eb7b | 71 | lv_init(); //Initialize the LVGL |
JohnnyK | 3:4f5dc253eb7b | 72 | tft_init(); //Initialize diplay |
JohnnyK | 3:4f5dc253eb7b | 73 | touchpad_init(); //Initialize touchpad |
JohnnyK | 3:4f5dc253eb7b | 74 | ticker.attach(callback(&lv_ticker_func),TICKER_TIME); //Attach callback to ticker |
mickaelul | 6:845cfd545a41 | 75 | |
mickaelul | 6:845cfd545a41 | 76 | //déclaration de l'image de la carte du jeu |
mickaelul | 6:845cfd545a41 | 77 | |
mickaelul | 6:845cfd545a41 | 78 | LV_IMG_DECLARE(mappac3); |
mickaelul | 6:845cfd545a41 | 79 | lv_obj_t * img3 = lv_img_create(lv_scr_act()); |
mickaelul | 6:845cfd545a41 | 80 | lv_img_set_src(img3, &mappac3); |
mickaelul | 6:845cfd545a41 | 81 | lv_obj_align(img3, LV_ALIGN_CENTER, 0, 0); |
mickaelul | 6:845cfd545a41 | 82 | lv_obj_set_size(img3, 480, 272); |
mickaelul | 6:845cfd545a41 | 83 | |
mickaelul | 6:845cfd545a41 | 84 | |
mickaelul | 6:845cfd545a41 | 85 | // declaration de l'image de pacman |
JohnnyK | 3:4f5dc253eb7b | 86 | |
mickaelul | 6:845cfd545a41 | 87 | LV_IMG_DECLARE(pacpacpac); |
mickaelul | 6:845cfd545a41 | 88 | lv_obj_t * img1 = lv_img_create(lv_scr_act()); |
mickaelul | 6:845cfd545a41 | 89 | lv_img_set_src(img1, &pacpacpac); |
mickaelul | 6:845cfd545a41 | 90 | lv_obj_align(img1, LV_ALIGN_CENTER, xpacman, ypacman); |
mickaelul | 6:845cfd545a41 | 91 | lv_obj_set_size(img1, 20, 20); |
mickaelul | 6:845cfd545a41 | 92 | |
mickaelul | 6:845cfd545a41 | 93 | // declaration de l'image du fantome |
mickaelul | 6:845cfd545a41 | 94 | |
mickaelul | 6:845cfd545a41 | 95 | LV_IMG_DECLARE(fantom2); |
mickaelul | 6:845cfd545a41 | 96 | lv_obj_t * img2 = lv_img_create(lv_scr_act()); |
mickaelul | 6:845cfd545a41 | 97 | lv_img_set_src(img2, &fantom2); |
mickaelul | 6:845cfd545a41 | 98 | lv_obj_align(img2, LV_ALIGN_CENTER, 80, 10); |
mickaelul | 6:845cfd545a41 | 99 | lv_obj_set_size(img2, 30, 30); |
mickaelul | 6:845cfd545a41 | 100 | |
mickaelul | 6:845cfd545a41 | 101 | |
mickaelul | 6:845cfd545a41 | 102 | // mise en place d'un pull up interne pour recevoir un signal quand l'interrupteur est ouvert |
JohnnyK | 0:10c4b83c458d | 103 | |
mickaelul | 6:845cfd545a41 | 104 | portD13.mode(PullUp); |
mickaelul | 6:845cfd545a41 | 105 | portD12.mode(PullUp); |
mickaelul | 6:845cfd545a41 | 106 | portD11.mode(PullUp); |
mickaelul | 6:845cfd545a41 | 107 | portD10.mode(PullUp); |
mickaelul | 6:845cfd545a41 | 108 | |
mickaelul | 6:845cfd545a41 | 109 | // mise en place des limites de la carte a jouer |
mickaelul | 6:845cfd545a41 | 110 | |
mickaelul | 6:845cfd545a41 | 111 | for(i=0;i<480;i++) |
mickaelul | 6:845cfd545a41 | 112 | { |
mickaelul | 6:845cfd545a41 | 113 | for(j=0;j<480;j++) |
mickaelul | 6:845cfd545a41 | 114 | { |
mickaelul | 6:845cfd545a41 | 115 | if((i==479)||(i==0)||(j==271)||(j==0)) |
mickaelul | 6:845cfd545a41 | 116 | { |
mickaelul | 6:845cfd545a41 | 117 | mur[i][j]=1; |
mickaelul | 6:845cfd545a41 | 118 | } |
mickaelul | 6:845cfd545a41 | 119 | else |
mickaelul | 6:845cfd545a41 | 120 | { |
mickaelul | 6:845cfd545a41 | 121 | mur[i][j]=0; |
mickaelul | 6:845cfd545a41 | 122 | } |
mickaelul | 6:845cfd545a41 | 123 | } |
mickaelul | 6:845cfd545a41 | 124 | } |
mickaelul | 6:845cfd545a41 | 125 | |
mickaelul | 6:845cfd545a41 | 126 | |
JohnnyK | 0:10c4b83c458d | 127 | |
JohnnyK | 3:4f5dc253eb7b | 128 | while (true){ |
mickaelul | 6:845cfd545a41 | 129 | |
mickaelul | 6:845cfd545a41 | 130 | |
mickaelul | 6:845cfd545a41 | 131 | |
mickaelul | 6:845cfd545a41 | 132 | |
mickaelul | 6:845cfd545a41 | 133 | |
mickaelul | 6:845cfd545a41 | 134 | //on fait changer le sens de l'image pour la faire bouger sauf si elle touche un mur |
mickaelul | 6:845cfd545a41 | 135 | if((portD13== 1)&&(mur[xpacman][ypacman]==0)) |
mickaelul | 6:845cfd545a41 | 136 | { |
mickaelul | 6:845cfd545a41 | 137 | xpacman= xpacman +1; |
mickaelul | 6:845cfd545a41 | 138 | |
mickaelul | 6:845cfd545a41 | 139 | lv_obj_align(img1, LV_ALIGN_CENTER, xpacman, ypacman); |
mickaelul | 6:845cfd545a41 | 140 | wait_us(10000); |
mickaelul | 6:845cfd545a41 | 141 | } |
mickaelul | 6:845cfd545a41 | 142 | |
mickaelul | 6:845cfd545a41 | 143 | if((portD11== 1)&&(mur[xpacman][ypacman]==0)) |
mickaelul | 6:845cfd545a41 | 144 | { |
mickaelul | 6:845cfd545a41 | 145 | xpacman= xpacman -1; |
mickaelul | 6:845cfd545a41 | 146 | |
mickaelul | 6:845cfd545a41 | 147 | lv_obj_align(img1, LV_ALIGN_CENTER, xpacman, ypacman); |
mickaelul | 6:845cfd545a41 | 148 | wait_us(10000); |
mickaelul | 6:845cfd545a41 | 149 | } |
mickaelul | 6:845cfd545a41 | 150 | |
mickaelul | 6:845cfd545a41 | 151 | if((portD12== 1)&&(mur[xpacman][ypacman]==0)) |
mickaelul | 6:845cfd545a41 | 152 | { |
mickaelul | 6:845cfd545a41 | 153 | ypacman= ypacman +1; |
mickaelul | 6:845cfd545a41 | 154 | |
mickaelul | 6:845cfd545a41 | 155 | lv_obj_align(img1, LV_ALIGN_CENTER, xpacman, ypacman); |
mickaelul | 6:845cfd545a41 | 156 | wait_us(10000); |
mickaelul | 6:845cfd545a41 | 157 | } |
mickaelul | 6:845cfd545a41 | 158 | |
mickaelul | 6:845cfd545a41 | 159 | if((portD10 == 1)&&(mur[xpacman][ypacman]==0)) |
mickaelul | 6:845cfd545a41 | 160 | { |
mickaelul | 6:845cfd545a41 | 161 | ypacman= ypacman +1; |
mickaelul | 6:845cfd545a41 | 162 | |
mickaelul | 6:845cfd545a41 | 163 | lv_obj_align(img1, LV_ALIGN_CENTER, xpacman, ypacman); |
mickaelul | 6:845cfd545a41 | 164 | wait_us(10000); |
mickaelul | 6:845cfd545a41 | 165 | } |
mickaelul | 6:845cfd545a41 | 166 | |
mickaelul | 6:845cfd545a41 | 167 | |
mickaelul | 6:845cfd545a41 | 168 | |
JohnnyK | 3:4f5dc253eb7b | 169 | lv_task_handler(); |
mickaelul | 6:845cfd545a41 | 170 | |
mickaelul | 6:845cfd545a41 | 171 | |
JohnnyK | 3:4f5dc253eb7b | 172 | //Call lv_task_handler() periodically every few milliseconds. |
JohnnyK | 3:4f5dc253eb7b | 173 | //It will redraw the screen if required, handle input devices etc. |
JohnnyK | 3:4f5dc253eb7b | 174 | thread_sleep_for(LVGL_TICK); |
JohnnyK | 0:10c4b83c458d | 175 | } |
JohnnyK | 3:4f5dc253eb7b | 176 | } |