Changeset 9696b01 in mainline
- Timestamp:
- 2014-11-14T17:25:28Z (10 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 1ab8539
- Parents:
- a047aaa
- Location:
- uspace
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/barber/barber.c
ra047aaa r9696b01 40 40 #include <task.h> 41 41 #include <loc.h> 42 #include <stats.h> 42 43 #include <fibril_synch.h> 43 44 #include <io/pixel.h> … … 53 54 #define FRAMES 30 54 55 56 #define MIN_FPS 1 57 #define MAX_FPS 25 58 59 #define MIN_LOAD (LOAD_UNIT / 4) 60 #define MAX_LOAD (LOAD_UNIT / 3) 61 55 62 #define FRAME_WIDTH 59 56 63 #define FRAME_HEIGHT 192 57 64 58 #define PERIOD 1000000 59 #define COLORS 7 65 #define LED_PERIOD 1000000 66 #define LED_COLORS 7 67 68 typedef struct { 69 link_t link; 70 service_id_t svc_id; 71 async_sess_t *sess; 72 } led_dev_t; 60 73 61 74 static char *winreg = NULL; 62 static fibril_timer_t *timer = NULL; 75 76 static fibril_timer_t *led_timer = NULL; 63 77 static list_t led_devs; 64 65 static canvas_t *frame_canvas; 66 static surface_t *frames[FRAMES]; 67 68 static pixel_t colors[COLORS] = { 78 static unsigned int led_color = 0; 79 80 static pixel_t led_colors[LED_COLORS] = { 69 81 PIXEL(0xff, 0xff, 0x00, 0x00), 70 82 PIXEL(0xff, 0x00, 0xff, 0x00), … … 76 88 }; 77 89 90 static fibril_timer_t *frame_timer = NULL; 91 static canvas_t *frame_canvas; 92 static surface_t *frames[FRAMES]; 93 78 94 static unsigned int frame = 0; 79 static unsigned int color = 0; 80 81 typedef struct { 82 link_t link; 83 service_id_t svc_id; 84 async_sess_t *sess; 85 } led_dev_t; 86 87 static void timer_callback(void *data) 88 { 89 pixel_t next_color = colors[color]; 90 91 color++; 92 if (color >= COLORS) 93 color = 0; 94 95 list_foreach(led_devs, link, led_dev_t, dev) { 96 if (dev->sess) 97 led_dev_color_set(dev->sess, next_color); 98 } 99 100 frame++; 101 if (frame >= FRAMES) 102 frame = 0; 103 104 update_canvas(frame_canvas, frames[frame]); 105 106 fibril_timer_set(timer, PERIOD, timer_callback, NULL); 107 } 108 109 static void loc_callback(void) 110 { 111 category_id_t led_cat; 112 int rc = loc_category_get_id("led", &led_cat, IPC_FLAG_BLOCKING); 113 if (rc != EOK) 114 return; 115 116 service_id_t *svcs; 117 size_t count; 118 rc = loc_category_get_svcs(led_cat, &svcs, &count); 119 if (rc != EOK) 120 return; 121 122 for (size_t i = 0; i < count; i++) { 123 bool known = false; 124 125 /* Determine whether we already know this device. */ 126 list_foreach(led_devs, link, led_dev_t, dev) { 127 if (dev->svc_id == svcs[i]) { 128 known = true; 129 break; 130 } 131 } 132 133 if (!known) { 134 led_dev_t *dev = (led_dev_t *) calloc(1, sizeof(led_dev_t)); 135 if (!dev) 136 continue; 137 138 link_initialize(&dev->link); 139 dev->svc_id = svcs[i]; 140 dev->sess = loc_service_connect(EXCHANGE_SERIALIZE, svcs[i], 0); 141 142 list_append(&dev->link, &led_devs); 143 } 144 } 145 146 // FIXME: Handle LED device removal 147 148 free(svcs); 149 } 95 static unsigned int fps = MIN_FPS; 96 97 static void led_timer_callback(void *); 98 static void frame_timer_callback(void *); 150 99 151 100 static bool decode_frames(void) … … 192 141 } 193 142 143 static void plan_led_timer(void) 144 { 145 fibril_timer_set(led_timer, LED_PERIOD, led_timer_callback, NULL); 146 } 147 148 static load_t get_load(void) 149 { 150 size_t count; 151 load_t *load = stats_get_load(&count); 152 load_t load_val; 153 154 if ((load != NULL) && (count > 0)) { 155 load_val = load[0]; 156 free(load); 157 } else 158 load_val = 0; 159 160 return load_val; 161 } 162 163 static void plan_frame_timer(suseconds_t render_time) 164 { 165 /* 166 * Crank up the FPS unless we lack 167 * behind with the rendering and 168 * unless the load is not above 169 * a lower threshold. 170 */ 171 172 suseconds_t delta = 1000000 / fps; 173 load_t load = get_load(); 174 175 if ((delta >= render_time) && (load < MIN_LOAD)) 176 fps++; 177 178 if (fps > MAX_FPS) 179 fps = MAX_FPS; 180 181 /* 182 * If we lack behind then immediately 183 * go to the lowest FPS. 184 */ 185 186 if (delta < render_time) 187 fps = MIN_FPS; 188 189 /* 190 * Crank down the FPS if the current 191 * load is above an upper threshold. 192 */ 193 194 if (load > MAX_LOAD) 195 fps--; 196 197 if (fps < MIN_FPS) 198 fps = MIN_FPS; 199 200 delta = 1000000 / fps; 201 202 fibril_timer_set(frame_timer, delta, frame_timer_callback, NULL); 203 } 204 205 static void led_timer_callback(void *data) 206 { 207 pixel_t next_led_color = led_colors[led_color]; 208 209 led_color++; 210 if (led_color >= LED_COLORS) 211 led_color = 0; 212 213 list_foreach(led_devs, link, led_dev_t, dev) { 214 if (dev->sess) 215 led_dev_color_set(dev->sess, next_led_color); 216 } 217 218 plan_led_timer(); 219 } 220 221 static void frame_timer_callback(void *data) 222 { 223 struct timeval prev; 224 getuptime(&prev); 225 226 frame++; 227 if (frame >= FRAMES) 228 frame = 0; 229 230 update_canvas(frame_canvas, frames[frame]); 231 232 struct timeval cur; 233 getuptime(&cur); 234 235 plan_frame_timer(tv_sub(&cur, &prev)); 236 } 237 238 static void loc_callback(void) 239 { 240 category_id_t led_cat; 241 int rc = loc_category_get_id("led", &led_cat, IPC_FLAG_BLOCKING); 242 if (rc != EOK) 243 return; 244 245 service_id_t *svcs; 246 size_t count; 247 rc = loc_category_get_svcs(led_cat, &svcs, &count); 248 if (rc != EOK) 249 return; 250 251 for (size_t i = 0; i < count; i++) { 252 bool known = false; 253 254 /* Determine whether we already know this device. */ 255 list_foreach(led_devs, link, led_dev_t, dev) { 256 if (dev->svc_id == svcs[i]) { 257 known = true; 258 break; 259 } 260 } 261 262 if (!known) { 263 led_dev_t *dev = (led_dev_t *) calloc(1, sizeof(led_dev_t)); 264 if (!dev) 265 continue; 266 267 link_initialize(&dev->link); 268 dev->svc_id = svcs[i]; 269 dev->sess = loc_service_connect(EXCHANGE_SERIALIZE, svcs[i], 0); 270 271 list_append(&dev->link, &led_devs); 272 } 273 } 274 275 // FIXME: Handle LED device removal 276 277 free(svcs); 278 } 279 194 280 int main(int argc, char *argv[]) 195 281 { … … 206 292 } 207 293 208 timer = fibril_timer_create(NULL); 209 if (!timer) { 210 printf("Unable to create timer.\n"); 294 led_timer = fibril_timer_create(NULL); 295 if (!led_timer) { 296 printf("Unable to create LED timer.\n"); 297 return 1; 298 } 299 300 frame_timer = fibril_timer_create(NULL); 301 if (!frame_timer) { 302 printf("Unable to create frame timer.\n"); 211 303 return 1; 212 304 } … … 235 327 window_exec(main_window); 236 328 237 fibril_timer_set(timer, PERIOD, timer_callback, NULL); 329 plan_led_timer(); 330 plan_frame_timer(0); 238 331 239 332 task_retval(0); -
uspace/lib/c/generic/stats.c
ra047aaa r9696b01 316 316 void stats_print_load_fragment(load_t upper, unsigned int dec_length) 317 317 { 318 /* Magic value from BSD */319 load_t lower = 65536;320 321 318 /* Print the whole part */ 322 printf("%u.", upper / lower);323 324 load_t rest = (upper % lower) * 10;319 printf("%u.", upper / LOAD_UNIT); 320 321 load_t rest = (upper % LOAD_UNIT) * 10; 325 322 326 323 unsigned int i; 327 324 for (i = 0; i < dec_length; i++) { 328 printf("%u", rest / lower);329 rest = (rest % lower) * 10;325 printf("%u", rest / LOAD_UNIT); 326 rest = (rest % LOAD_UNIT) * 10; 330 327 } 331 328 } -
uspace/lib/c/include/stats.h
ra047aaa r9696b01 43 43 #include <abi/sysinfo.h> 44 44 45 #define LOAD_UNIT 65536 46 45 47 extern stats_cpu_t *stats_get_cpus(size_t *); 46 48 extern stats_physmem_t *stats_get_physmem(void);
Note:
See TracChangeset
for help on using the changeset viewer.