Skip to content

Commit 1e76469

Browse files
committed
Apps: Demo is bit more smooth than before
Also includes: * Using process events to detect yield & child process exits.
1 parent 186e0c0 commit 1e76469

File tree

3 files changed

+79
-40
lines changed

3 files changed

+79
-40
lines changed

docs/images/meghaos_gui0.gif

2.57 MB
Loading

src/apps/gui0.c

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,39 @@ static OSIF_WindowFrameBufferInfo createWindow (const char* const title)
2525
return fbi;
2626
}
2727

28+
static void triangle_sum (int* now, int start, int end, int* incby)
29+
{
30+
int l_now = *now;
31+
32+
l_now += *incby;
33+
34+
if (l_now >= end) {
35+
l_now = end;
36+
} else if (l_now <= start) {
37+
l_now = start;
38+
}
39+
40+
if (l_now == end || l_now == start) {
41+
*incby *= -1;
42+
}
43+
44+
*now = l_now;
45+
}
46+
47+
static void repaint_on_yield (OSIF_ProcessEvent const* const e)
48+
{
49+
(void)e;
50+
cm_window_flush_graphics();
51+
}
52+
2853
void proc_main()
2954
{
3055
cm_window_flush_graphics();
56+
cm_thread_create (&thread0, false);
3157
cm_thread_create (&thread1, false);
3258
cm_thread_create (&thread2, false);
33-
cm_thread_create (&thread0, false);
59+
60+
cm_process_register_event_handler (OSIF_PROCESS_EVENT_PROCCESS_YIELD_REQ, repaint_on_yield);
3461

3562
while (1) {
3663
cm_process_handle_events();
@@ -39,68 +66,54 @@ void proc_main()
3966

4067
void thread0()
4168
{
42-
UINT color = 0x5F9FFF;
43-
INT x = 0;
44-
INT width = 20;
45-
OSIF_WindowFrameBufferInfo fbi = createWindow ("gui0 - Window 3");
46-
graphics_rect (&fbi, 0, 0, fbi.width_px, fbi.height_px, 0x0);
69+
OSIF_WindowFrameBufferInfo fbi = createWindow ("gui0 - Window 1");
70+
int value = 1;
71+
int incby = 1;
4772
while (1) {
48-
if ((UINT)(x + width) >= fbi.width_px) {
49-
x = 0;
50-
graphics_rect (&fbi, 0, 0, fbi.width_px, fbi.height_px, 0x0);
51-
}
52-
graphics_rect (&fbi, (UINT)x, 0, (UINT)width, fbi.height_px, color);
53-
x += width + 3;
73+
triangle_sum (&value, 1, 10, &incby);
5474

55-
cm_window_flush_graphics();
56-
cm_delay (500);
75+
for (unsigned int x = 0; x < fbi.width_px; x++) {
76+
for (unsigned int y = 0; y < fbi.height_px; y++) {
77+
UINT color = ((x) * (UINT)value & 255) << 0;
78+
graphics_putpixel (&fbi, x, y, color);
79+
}
80+
}
81+
cm_delay (50);
5782
}
5883
}
5984

6085
void thread1()
6186
{
62-
OSIF_WindowFrameBufferInfo fbi = createWindow ("gui0 - Window 1");
63-
int modby = 1;
87+
OSIF_WindowFrameBufferInfo fbi = createWindow ("gui0 - Window 2");
88+
int value = 10;
6489
int incby = 1;
6590
while (1) {
66-
if (modby == 10) {
67-
incby = -1;
68-
} else if (modby == 1) {
69-
incby = 1;
70-
}
71-
modby += incby;
91+
triangle_sum (&value, 10, 20, &incby);
7292

7393
for (unsigned int x = 0; x < fbi.width_px; x++) {
7494
for (unsigned int y = 0; y < fbi.height_px; y++) {
75-
UINT color = (20 * (UINT)modby & 255) << 16;
95+
UINT color = ((y) * (UINT)value & 255) << 16;
7696
graphics_putpixel (&fbi, x, y, color);
7797
}
7898
}
79-
cm_window_flush_graphics();
80-
cm_delay (100);
99+
cm_delay (50);
81100
}
82101
}
83102

84103
void thread2()
85104
{
86-
OSIF_WindowFrameBufferInfo fbi = createWindow ("gui0 - Window 2");
87-
int modby = 1;
105+
OSIF_WindowFrameBufferInfo fbi = createWindow ("gui0 - Window 3");
106+
int value = 1;
88107
int incby = 1;
89108
while (1) {
90-
if (modby == 10) {
91-
incby = -1;
92-
} else if (modby == 1) {
93-
incby = 1;
94-
}
95-
modby += incby;
109+
triangle_sum (&value, 1, 10, &incby);
96110

97111
for (unsigned int x = 0; x < fbi.width_px; x++) {
98112
for (unsigned int y = 0; y < fbi.height_px; y++) {
99-
UINT color = ((x + y) * (UINT)modby & 255) << 8;
113+
UINT color = ((x + y) * (UINT)value & 255) << 8;
100114
graphics_putpixel (&fbi, x, y, color);
101115
}
102116
}
103-
cm_window_flush_graphics();
104117
cm_delay (80);
105118
}
106119
}

src/apps/mpdemo.c

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
#include <stdbool.h>
12
#include <types.h>
23
#include <syscall.h>
34
#include <cm.h>
5+
#include <debug.h>
46

57
#define MAX_VGA_COLUMNS 80U
68

@@ -24,6 +26,8 @@ typedef enum DisplayVgaColors
2426
WHITE
2527
} DisplayVgaColors;
2628

29+
bool all_child_exited = false;
30+
2731
static void s_printString (U32 row, U32 col, U32 bgcolor, U32 fgcolor, char* text);
2832
static void s_progressbar (UINT iterPerStep, char* title, UINT row, UINT color);
2933
static void thread0();
@@ -36,7 +40,7 @@ static void s_printString (U32 row, U32 col, U32 bgcolor, U32 fgcolor, char* tex
3640
cm_putstr (text);
3741
}
3842

39-
void s_progressbar (UINT delay_ms, char* title, UINT row, UINT color)
43+
static void s_progressbar (UINT delay_ms, char* title, UINT row, UINT color)
4044
{
4145
s_printString (row, 0, BLACK, DARK_GRAY, title);
4246

@@ -48,14 +52,36 @@ void s_progressbar (UINT delay_ms, char* title, UINT row, UINT color)
4852
}
4953
}
5054

55+
static void count_child_exits (OSIF_ProcessEvent const* const e)
56+
{
57+
(void)e;
58+
static int count = 0;
59+
60+
CM_DBG_INFO ("Child process exited.");
61+
if (++count == 2) {
62+
CM_DBG_INFO ("Every child process exited.");
63+
all_child_exited = true;
64+
}
65+
}
66+
67+
static void wait_for_all_child_exit()
68+
{
69+
while (!all_child_exited) {
70+
cm_process_handle_events();
71+
}
72+
}
73+
5174
void proc_main()
5275
{
5376
cm_thread_create (thread0, false);
54-
cm_thread_create (&thread1, false);
77+
cm_thread_create (thread1, false);
78+
79+
cm_process_register_event_handler (OSIF_PROCESS_EVENT_PROCCESS_CHILD_KILLED,
80+
count_child_exits);
5581

56-
s_progressbar (200, "Process 0:\n", 26, RED);
82+
s_progressbar (5, "Process 0:\n", 26, RED);
5783

58-
s_progressbar (20, "Process 0:\n", 26, RED);
84+
wait_for_all_child_exit();
5985

6086
cm_process_kill(1);
6187
s_printString (37, 0, BLACK, WHITE,
@@ -70,6 +96,6 @@ void thread0()
7096

7197
void thread1()
7298
{
73-
s_progressbar (5, "Thread 1:\n", 34, YELLOW);
99+
s_progressbar (2, "Thread 1:\n", 34, YELLOW);
74100
cm_process_kill(3);
75101
}

0 commit comments

Comments
 (0)