diff --git a/kernels/demo/Makefile b/kernels/demo/Makefile new file mode 100644 index 0000000..61835c5 --- /dev/null +++ b/kernels/demo/Makefile @@ -0,0 +1,3 @@ +NAME = demo +SRCS = $(shell find src -name "*.c") +include $(AM_HOME)/Makefile diff --git a/kernels/demo/include/io.h b/kernels/demo/include/io.h new file mode 100644 index 0000000..9ed6b50 --- /dev/null +++ b/kernels/demo/include/io.h @@ -0,0 +1,97 @@ +#ifndef __DRAW_H__ + +#define HAS_GUI + +#include +#include +#include +#include + +#define ANSI_COLOR_RED 31 +#define ANSI_COLOR_GREEN 32 +#define ANSI_COLOR_WHITE 37 +#define ANSI_COLOR_RESET 0 + +#ifdef HAS_GUI +#define TILE_W 4 +#define SCREEN_W 320 +#define SCREEN_H 200 +static inline void set_color(int c) { +} + +static inline void print_char(char c, int y, int x) { + static char last_c = 0xff; + static uint32_t buf[TILE_W * TILE_W]; + if (last_c != c) { + last_c = c; + uint32_t color = 0x0; + if (c != ' ') { // convert different character to different color + uint8_t r = c / 25; + c -= r * 25; + uint8_t g = c / 5; + c -= g * 5; + uint8_t b = c; + r = r * 0x100 / 5; + g = g * 0x100 / 5; + b = b * 0x100 / 5; + color = (r << 16) | (g << 8) | b; + } + int i; + for (i = 0; i < TILE_W * TILE_W; i ++) { + buf[i] = color; + } + } + io_write(AM_GPU_FBDRAW, x * TILE_W, y * TILE_W, buf, TILE_W, TILE_W, false); +} + +static inline void screen_clear() { + static uint32_t buf[SCREEN_W]; + int i; + for (i = 0; i < SCREEN_H; i ++) { + io_write(AM_GPU_FBDRAW, 0, i, buf, SCREEN_W, 1, false); + } +} + +static inline void screen_refresh() { + io_write(AM_GPU_FBDRAW, 0, 0, NULL, 0, 0, true); +} + +static inline int screen_tile_height() { + return io_read(AM_GPU_CONFIG).height / TILE_W; +} + +static inline int screen_tile_width() { + return io_read(AM_GPU_CONFIG).width / TILE_W; +} +#else +static inline void set_color(int c) { + printf("\033[%dm", c); +} + +static inline void print_char(char c, int y, int x) { + printf("\033[%d;%dH%c", y + 1, x + 1, c); +} + +static inline void screen_clear() { + printf("\033[H\033[J"); +} + +static inline void screen_refresh() { +} + +static inline int screen_tile_height() { + return 24; +} + +static inline int screen_tile_width() { + return 80; +} +#endif + +static inline void usleep(int us) { + uint64_t now = io_read(AM_TIMER_UPTIME).us; + uint64_t next = now + us; + while (io_read(AM_TIMER_UPTIME).us < next) ; +} + +#endif diff --git a/kernels/demo/src/aclock/aclock.c b/kernels/demo/src/aclock/aclock.c new file mode 100644 index 0000000..4e74ff5 --- /dev/null +++ b/kernels/demo/src/aclock/aclock.c @@ -0,0 +1,219 @@ +/* + * aclock - ascii clock for vt100 terminals + * failsafe mode: + * > no curses / termcap required + * > no floating point required + * > no ANSI C compiler required + * + * Copyright (c) 2002 Antoni Sawicki + * Version 1.8 (knr-nofloat-vt100); Dublin, June 2002 + * + * Compilation: cc aclock-vt100.c -o aclock + * + * https://github.com/tenox7/aclock/blob/master/sources/aclock-vt100.c + * + * 2021-03-26 Meco Man port to RT-Thread + * + */ + +#include +#include +#include +#include + +static const int minute[60][8][2] = { + { { 39,11 },{ 39,10 },{ 39,9 },{ 39,8 },{ 39,7 },{ 39,6 },{ 39,5 },{ 39,4 } }, + { { 40,11 },{ 40,10 },{ 40,9 },{ 40,8 },{ 41,7 },{ 41,6 },{ 41,5 },{ 41,4 } }, + { { 40,11 },{ 40,10 },{ 41,9 },{ 41,8 },{ 42,7 },{ 42,6 },{ 42,5 },{ 43,4 } }, + { { 40,11 },{ 41,10 },{ 41,9 },{ 42,8 },{ 43,7 },{ 43,6 },{ 44,5 },{ 44,4 } }, + { { 40,11 },{ 41,10 },{ 42,9 },{ 43,8 },{ 44,7 },{ 44,6 },{ 45,5 },{ 46,4 } }, + { { 40,11 },{ 41,10 },{ 42,9 },{ 43,8 },{ 44,7 },{ 45,6 },{ 46,5 },{ 47,5 } }, + { { 41,11 },{ 42,10 },{ 43,9 },{ 44,8 },{ 45,7 },{ 47,7 },{ 48,6 },{ 49,5 } }, + { { 41,11 },{ 42,10 },{ 44,9 },{ 45,9 },{ 46,8 },{ 48,7 },{ 49,6 },{ 50,6 } }, + { { 41,11 },{ 42,10 },{ 44,9 },{ 45,9 },{ 47,8 },{ 48,7 },{ 50,7 },{ 51,6 } }, + { { 41,11 },{ 43,10 },{ 44,10 },{ 46,9 },{ 48,9 },{ 49,8 },{ 51,7 },{ 52,7 } }, + { { 41,11 },{ 43,10 },{ 45,10 },{ 46,9 },{ 48,9 },{ 50,8 },{ 52,8 },{ 53,7 } }, + { { 41,11 },{ 43,11 },{ 45,10 },{ 47,10 },{ 49,9 },{ 50,9 },{ 52,9 },{ 54,8 } }, + { { 41,11 },{ 43,11 },{ 45,11 },{ 47,10 },{ 49,10 },{ 51,10 },{ 53,9 },{ 55,9 } }, + { { 41,11 },{ 43,11 },{ 45,11 },{ 47,11 },{ 49,10 },{ 51,10 },{ 53,10 },{ 55,10 } }, + { { 41,11 },{ 43,11 },{ 45,11 },{ 47,11 },{ 49,11 },{ 51,11 },{ 53,11 },{ 55,11 } }, + { { 42,12 },{ 44,12 },{ 46,12 },{ 48,12 },{ 50,12 },{ 52,12 },{ 54,12 },{ 56,12 } }, + { { 41,12 },{ 43,12 },{ 45,12 },{ 47,12 },{ 49,12 },{ 51,12 },{ 53,12 },{ 55,12 } }, + { { 41,12 },{ 43,12 },{ 45,12 },{ 47,12 },{ 49,13 },{ 51,13 },{ 53,13 },{ 55,13 } }, + { { 41,12 },{ 43,12 },{ 45,12 },{ 47,13 },{ 49,13 },{ 51,13 },{ 53,14 },{ 55,14 } }, + { { 41,12 },{ 43,12 },{ 45,13 },{ 47,13 },{ 49,14 },{ 50,14 },{ 52,14 },{ 54,15 } }, + { { 41,12 },{ 43,13 },{ 45,13 },{ 46,14 },{ 48,14 },{ 50,15 },{ 52,15 },{ 53,16 } }, + { { 41,12 },{ 43,13 },{ 44,13 },{ 46,14 },{ 48,14 },{ 49,15 },{ 51,16 },{ 52,16 } }, + { { 41,12 },{ 42,13 },{ 44,14 },{ 45,14 },{ 47,15 },{ 48,16 },{ 50,16 },{ 51,17 } }, + { { 41,12 },{ 42,13 },{ 44,14 },{ 45,14 },{ 46,15 },{ 48,16 },{ 49,17 },{ 50,17 } }, + { { 41,12 },{ 42,13 },{ 43,14 },{ 44,15 },{ 45,16 },{ 47,16 },{ 48,17 },{ 49,18 } }, + { { 40,12 },{ 41,13 },{ 42,14 },{ 43,15 },{ 44,16 },{ 45,17 },{ 46,18 },{ 47,18 } }, + { { 40,12 },{ 41,13 },{ 42,14 },{ 43,15 },{ 44,16 },{ 44,17 },{ 45,18 },{ 46,19 } }, + { { 40,12 },{ 41,13 },{ 41,14 },{ 42,15 },{ 43,16 },{ 43,17 },{ 44,18 },{ 44,19 } }, + { { 40,12 },{ 40,13 },{ 41,14 },{ 41,15 },{ 42,16 },{ 42,17 },{ 42,18 },{ 43,19 } }, + { { 40,12 },{ 40,13 },{ 40,14 },{ 40,15 },{ 41,16 },{ 41,17 },{ 41,18 },{ 41,19 } }, + { { 39,12 },{ 39,13 },{ 39,14 },{ 39,15 },{ 39,16 },{ 39,17 },{ 39,18 },{ 39,19 } }, + { { 39,12 },{ 39,13 },{ 39,14 },{ 39,15 },{ 38,16 },{ 38,17 },{ 38,18 },{ 38,19 } }, + { { 39,12 },{ 39,13 },{ 38,14 },{ 38,15 },{ 37,16 },{ 37,17 },{ 37,18 },{ 36,19 } }, + { { 39,12 },{ 38,13 },{ 38,14 },{ 37,15 },{ 36,16 },{ 36,17 },{ 35,18 },{ 35,19 } }, + { { 39,12 },{ 38,13 },{ 37,14 },{ 36,15 },{ 35,16 },{ 35,17 },{ 34,18 },{ 33,19 } }, + { { 38,12 },{ 37,13 },{ 36,14 },{ 35,15 },{ 34,16 },{ 33,17 },{ 32,18 },{ 31,18 } }, + { { 38,12 },{ 37,13 },{ 36,14 },{ 35,15 },{ 34,16 },{ 32,16 },{ 31,17 },{ 30,18 } }, + { { 38,12 },{ 37,13 },{ 35,14 },{ 34,14 },{ 33,15 },{ 31,16 },{ 30,17 },{ 29,17 } }, + { { 38,12 },{ 37,13 },{ 35,14 },{ 34,14 },{ 32,15 },{ 31,16 },{ 29,16 },{ 28,17 } }, + { { 38,12 },{ 36,13 },{ 35,13 },{ 33,14 },{ 31,14 },{ 30,15 },{ 28,16 },{ 27,16 } }, + { { 38,12 },{ 36,13 },{ 34,13 },{ 33,14 },{ 31,14 },{ 29,15 },{ 27,15 },{ 26,16 } }, + { { 38,12 },{ 36,12 },{ 34,13 },{ 32,13 },{ 30,14 },{ 29,14 },{ 27,14 },{ 25,15 } }, + { { 38,12 },{ 36,12 },{ 34,12 },{ 32,13 },{ 30,13 },{ 28,13 },{ 26,14 },{ 24,14 } }, + { { 38,12 },{ 36,12 },{ 34,12 },{ 32,12 },{ 30,13 },{ 28,13 },{ 26,13 },{ 24,13 } }, + { { 38,12 },{ 36,12 },{ 34,12 },{ 32,12 },{ 30,12 },{ 28,12 },{ 26,12 },{ 24,12 } }, + { { 38,11 },{ 36,11 },{ 34,11 },{ 32,11 },{ 30,11 },{ 28,11 },{ 26,11 },{ 24,11 } }, + { { 38,11 },{ 36,11 },{ 34,11 },{ 32,11 },{ 30,11 },{ 28,11 },{ 26,11 },{ 24,11 } }, + { { 38,11 },{ 36,11 },{ 34,11 },{ 32,11 },{ 30,10 },{ 28,10 },{ 26,10 },{ 24,10 } }, + { { 38,11 },{ 36,11 },{ 34,11 },{ 32,10 },{ 30,10 },{ 28,10 },{ 26,9 },{ 24,9 } }, + { { 38,11 },{ 36,11 },{ 34,10 },{ 32,10 },{ 30,9 },{ 29,9 },{ 27,9 },{ 25,8 } }, + { { 38,11 },{ 36,11 },{ 34,10 },{ 33,10 },{ 31,9 },{ 29,9 },{ 27,8 },{ 26,8 } }, + { { 38,11 },{ 36,10 },{ 35,10 },{ 33,9 },{ 31,9 },{ 30,8 },{ 28,7 },{ 27,7 } }, + { { 38,11 },{ 37,10 },{ 35,9 },{ 34,9 },{ 32,8 },{ 31,7 },{ 29,7 },{ 28,6 } }, + { { 38,11 },{ 37,10 },{ 35,9 },{ 34,9 },{ 33,8 },{ 31,7 },{ 30,6 },{ 29,6 } }, + { { 38,11 },{ 37,10 },{ 36,9 },{ 35,8 },{ 34,7 },{ 32,7 },{ 31,6 },{ 30,5 } }, + { { 39,11 },{ 38,10 },{ 37,9 },{ 36,8 },{ 35,7 },{ 34,6 },{ 33,5 },{ 32,5 } }, + { { 39,11 },{ 38,10 },{ 37,9 },{ 36,8 },{ 35,7 },{ 35,6 },{ 34,5 },{ 33,4 } }, + { { 39,11 },{ 38,10 },{ 38,9 },{ 37,8 },{ 36,7 },{ 36,6 },{ 35,5 },{ 35,4 } }, + { { 39,11 },{ 39,10 },{ 38,9 },{ 38,8 },{ 37,7 },{ 37,6 },{ 37,5 },{ 36,4 } }, + { { 39,11 },{ 39,10 },{ 39,9 },{ 39,8 },{ 38,7 },{ 38,6 },{ 38,5 },{ 38,4 } } +}; + +static const int hour[60][6][2] = { + { { 39,11 },{ 39,10 },{ 39,9 },{ 39,8 },{ 39,7 },{ 39,6 } }, + { { 40,11 },{ 40,10 },{ 40,9 },{ 40,8 },{ 41,7 },{ 41,6 } }, + { { 40,11 },{ 40,10 },{ 41,9 },{ 41,8 },{ 42,7 },{ 42,6 } }, + { { 40,11 },{ 41,10 },{ 41,9 },{ 42,8 },{ 43,7 },{ 43,6 } }, + { { 40,11 },{ 41,10 },{ 42,9 },{ 43,8 },{ 44,7 },{ 44,6 } }, + { { 40,11 },{ 41,10 },{ 42,9 },{ 43,8 },{ 44,7 },{ 45,6 } }, + { { 41,11 },{ 42,10 },{ 43,9 },{ 44,8 },{ 45,7 },{ 47,7 } }, + { { 41,11 },{ 42,10 },{ 44,9 },{ 45,9 },{ 46,8 },{ 48,7 } }, + { { 41,11 },{ 42,10 },{ 44,9 },{ 45,9 },{ 47,8 },{ 48,7 } }, + { { 41,11 },{ 43,10 },{ 44,10 },{ 46,9 },{ 48,9 },{ 49,8 } }, + { { 41,11 },{ 43,10 },{ 45,10 },{ 46,9 },{ 48,9 },{ 50,8 } }, + { { 41,11 },{ 43,11 },{ 45,10 },{ 47,10 },{ 49,9 },{ 50,9 } }, + { { 41,11 },{ 43,11 },{ 45,11 },{ 47,10 },{ 49,10 },{ 51,10 } }, + { { 41,11 },{ 43,11 },{ 45,11 },{ 47,11 },{ 49,10 },{ 51,10 } }, + { { 41,11 },{ 43,11 },{ 45,11 },{ 47,11 },{ 49,11 },{ 51,11 } }, + { { 42,12 },{ 44,12 },{ 46,12 },{ 48,12 },{ 50,12 },{ 52,12 } }, + { { 41,12 },{ 43,12 },{ 45,12 },{ 47,12 },{ 49,12 },{ 51,12 } }, + { { 41,12 },{ 43,12 },{ 45,12 },{ 47,12 },{ 49,13 },{ 51,13 } }, + { { 41,12 },{ 43,12 },{ 45,12 },{ 47,13 },{ 49,13 },{ 51,13 } }, + { { 41,12 },{ 43,12 },{ 45,13 },{ 47,13 },{ 49,14 },{ 50,14 } }, + { { 41,12 },{ 43,13 },{ 45,13 },{ 46,14 },{ 48,14 },{ 50,15 } }, + { { 41,12 },{ 43,13 },{ 44,13 },{ 46,14 },{ 48,14 },{ 49,15 } }, + { { 41,12 },{ 42,13 },{ 44,14 },{ 45,14 },{ 47,15 },{ 48,16 } }, + { { 41,12 },{ 42,13 },{ 44,14 },{ 45,14 },{ 46,15 },{ 48,16 } }, + { { 41,12 },{ 42,13 },{ 43,14 },{ 44,15 },{ 45,16 },{ 47,16 } }, + { { 40,12 },{ 41,13 },{ 42,14 },{ 43,15 },{ 44,16 },{ 45,17 } }, + { { 40,12 },{ 41,13 },{ 42,14 },{ 43,15 },{ 44,16 },{ 44,17 } }, + { { 40,12 },{ 41,13 },{ 41,14 },{ 42,15 },{ 43,16 },{ 43,17 } }, + { { 40,12 },{ 40,13 },{ 41,14 },{ 41,15 },{ 42,16 },{ 42,17 } }, + { { 40,12 },{ 40,13 },{ 40,14 },{ 40,15 },{ 41,16 },{ 41,17 } }, + { { 39,12 },{ 39,13 },{ 39,14 },{ 39,15 },{ 39,16 },{ 39,17 } }, + { { 39,12 },{ 39,13 },{ 39,14 },{ 39,15 },{ 38,16 },{ 38,17 } }, + { { 39,12 },{ 39,13 },{ 38,14 },{ 38,15 },{ 37,16 },{ 37,17 } }, + { { 39,12 },{ 38,13 },{ 38,14 },{ 37,15 },{ 36,16 },{ 36,17 } }, + { { 39,12 },{ 38,13 },{ 37,14 },{ 36,15 },{ 35,16 },{ 35,17 } }, + { { 38,12 },{ 37,13 },{ 36,14 },{ 35,15 },{ 34,16 },{ 33,17 } }, + { { 38,12 },{ 37,13 },{ 36,14 },{ 35,15 },{ 34,16 },{ 32,16 } }, + { { 38,12 },{ 37,13 },{ 35,14 },{ 34,14 },{ 33,15 },{ 31,16 } }, + { { 38,12 },{ 37,13 },{ 35,14 },{ 34,14 },{ 32,15 },{ 31,16 } }, + { { 38,12 },{ 36,13 },{ 35,13 },{ 33,14 },{ 31,14 },{ 30,15 } }, + { { 38,12 },{ 36,13 },{ 34,13 },{ 33,14 },{ 31,14 },{ 29,15 } }, + { { 38,12 },{ 36,12 },{ 34,13 },{ 32,13 },{ 30,14 },{ 29,14 } }, + { { 38,12 },{ 36,12 },{ 34,12 },{ 32,13 },{ 30,13 },{ 28,13 } }, + { { 38,12 },{ 36,12 },{ 34,12 },{ 32,12 },{ 30,13 },{ 28,13 } }, + { { 38,12 },{ 36,12 },{ 34,12 },{ 32,12 },{ 30,12 },{ 28,12 } }, + { { 38,11 },{ 36,11 },{ 34,11 },{ 32,11 },{ 30,11 },{ 28,11 } }, + { { 38,11 },{ 36,11 },{ 34,11 },{ 32,11 },{ 30,11 },{ 28,11 } }, + { { 38,11 },{ 36,11 },{ 34,11 },{ 32,11 },{ 30,10 },{ 28,10 } }, + { { 38,11 },{ 36,11 },{ 34,11 },{ 32,10 },{ 30,10 },{ 28,10 } }, + { { 38,11 },{ 36,11 },{ 34,10 },{ 32,10 },{ 30,9 },{ 29,9 } }, + { { 38,11 },{ 36,11 },{ 34,10 },{ 33,10 },{ 31,9 },{ 29,9 } }, + { { 38,11 },{ 36,10 },{ 35,10 },{ 33,9 },{ 31,9 },{ 30,8 } }, + { { 38,11 },{ 37,10 },{ 35,9 },{ 34,9 },{ 32,8 },{ 31,7 } }, + { { 38,11 },{ 37,10 },{ 35,9 },{ 34,9 },{ 33,8 },{ 31,7 } }, + { { 38,11 },{ 37,10 },{ 36,9 },{ 35,8 },{ 34,7 },{ 32,7 } }, + { { 39,11 },{ 38,10 },{ 37,9 },{ 36,8 },{ 35,7 },{ 34,6 } }, + { { 39,11 },{ 38,10 },{ 37,9 },{ 36,8 },{ 35,7 },{ 35,6 } }, + { { 39,11 },{ 38,10 },{ 38,9 },{ 37,8 },{ 36,7 },{ 36,6 } }, + { { 39,11 },{ 39,10 },{ 38,9 },{ 38,8 },{ 37,7 },{ 37,6 } }, + { { 39,11 },{ 39,10 },{ 39,9 },{ 39,8 },{ 38,7 },{ 38,6 } } +}; + +static const int circle[60][3] = { + { 62,12,111 }, { 61,13,46 }, { 61,14,46 }, { 60,15,46 }, + { 60,16,46 }, { 59,17,111 }, { 57,18,46 }, { 56,19,46 }, + { 54,20,46 }, { 52,20,46 }, { 51,21,111 }, { 48,22,46 }, + { 46,22,46 }, { 44,22,46 }, { 42,22,46 }, { 40,23,111 }, + { 37,22,46 }, { 35,22,46 }, { 33,22,46 }, { 31,22,46 }, + { 29,21,111 }, { 27,20,46 }, { 25,20,46 }, { 23,19,46 }, + { 22,18,46 }, { 20,17,111 }, { 19,16,46 }, { 19,15,46 }, + { 18,14,46 }, { 18,13,46 }, { 18,12,111 }, { 18,10,46 }, + { 18,9,46 }, { 19,8,46 }, { 19,7,46 }, { 20,6,111 }, + { 22,5,46 }, { 23,4,46 }, { 25,3,46 }, { 27,3,46 }, + { 28,2,111 }, { 31,1,46 }, { 33,1,46 }, { 35,1,46 }, + { 37,1,46 }, { 39,1,111 }, { 42,1,46 }, { 44,1,46 }, + { 46,1,46 }, { 48,1,46 }, { 51,2,111 }, { 52,3,46 }, + { 54,3,46 }, { 56,4,46 }, { 57,5,46 }, { 59,6,111 }, + { 60,7,46 }, { 60,8,46 }, { 61,9,46 }, { 61,10,46 } +}; + + +static void draw_point(int x, int y, int c) { + int xx = x - 16; +#ifdef HAS_GUI + xx /= 2; +#endif + print_char(c, y, xx); +} + +//void draw_text(int x, int y, char *string) { +// rt_kprintf("\033[%d;%dH%s", y, x, string); +//} + +static void draw_circle(void) { + int n; + for(n=0;n<60;n++) + draw_point(circle[n][0], circle[n][1], circle[n][2]); +} + +static void draw_hour(int n) { + int m; + for(m=0;m<6;m++) + draw_point(hour[n][m][0], hour[n][m][1], 'h'); +} + +static void draw_minute(int n) { + int m; + for(m=0;m<8;m++) + draw_point(minute[n][m][0], minute[n][m][1], 'M'); +} + +static void draw_seconds(int n) { + int m; + for(m=0;m<8;m++) + draw_point(minute[n][m][0], minute[n][m][1], '.'); +} + +void aclock(void) { + while(1) { + AM_TIMER_RTC_T rtc = io_read(AM_TIMER_RTC); + screen_clear(); + draw_circle(); + draw_hour(((rtc.hour > 12 ? rtc.hour - 12 : rtc.hour) * 5) + (rtc.minute / 10)); + draw_minute(rtc.minute); + draw_seconds(rtc.second); + screen_refresh(); + //draw_text(35, 6, ".:ACLOCK:."); + //rt_sprintf(digital_time, "[%02d:%02d:%02d]", ltime->tm_hour, ltime->tm_min, ltime->tm_sec); + //draw_text(35, 19, digital_time); + usleep(1000000); + } +} diff --git a/kernels/demo/src/ant/ant.c b/kernels/demo/src/ant/ant.c new file mode 100644 index 0000000..e355b30 --- /dev/null +++ b/kernels/demo/src/ant/ant.c @@ -0,0 +1,70 @@ +/* from http://rosettacode.org/wiki/Langton%27s_ant#C */ + +#include +#include +#include + +static int w = 40, h = 25; + +static unsigned char *pix = NULL; + +static void refresh(int x, int y) { + int i, j, k; + screen_clear(); + for (i = k = 0; i < h; i++) + for (j = 0; j < w; j++, k++) + print_char(pix[k] ? '#' : ' ', i, j); +} + +void ant() { + w = screen_tile_width(); + h = screen_tile_height(); + + int dx = 0, dy = 1, i, k; + int x = w / 2, y = h / 2; + + pix = malloc(w * h); + memset(pix, 0, w * h); + screen_clear(); + + while (1) { + i = (y * w + x); + if (pix[i]) k = dx, dx = -dy, dy = k; + else k = dy, dy = -dx, dx = k; + + pix[i] = !pix[i]; + print_char(pix[i] ? 'o' : ' ', y + 1, x + 1); + + x += dx, y += dy; + + k = 0; + if (x < 0) { + memmove(pix + 1, pix, w * h - 1); + for (i = 0; i < w * h; i += w) pix[i] = 0; + x++, k = 1; + } + else if (x >= w) { + memmove(pix, pix + 1, w * h - 1); + for (i = w-1; i < w * h; i += w) pix[i] = 0; + x--, k = 1; + } + + if (y >= h) { + memmove(pix, pix + w, w * (h - 1)); + memset(pix + w * (h - 1), 0, w); + y--, k = 1; + } + else if (y < 0) { + memmove(pix + w, pix, w * (h - 1)); + memset(pix, 0, w); + y++, k = 1; + } + if (k) refresh(x, y); + set_color(ANSI_COLOR_RED); + print_char('+', y + 1, x + 1); + set_color(ANSI_COLOR_RESET); + screen_refresh(); + + usleep(10000); + } +} diff --git a/kernels/demo/src/cmatrix/LICENSE b/kernels/demo/src/cmatrix/LICENSE new file mode 100644 index 0000000..358821f --- /dev/null +++ b/kernels/demo/src/cmatrix/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Fontaine Hugo a.k.a "Usiten" + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/kernels/demo/src/cmatrix/cmatrix.c b/kernels/demo/src/cmatrix/cmatrix.c new file mode 100644 index 0000000..5f22fef --- /dev/null +++ b/kernels/demo/src/cmatrix/cmatrix.c @@ -0,0 +1,72 @@ +/************************************************************************ + + The Matrix Rain Screen Saver + + This code is converted from QBASIC to C. + + QBasic Code from +http://codegolf.stackexchange.com/questions/17285/make-the-matrix-digital-rain-using-the-shortest-amount-of-code + + ************************************************************************/ + +#include +#include +#include + +#define W 80 +#define H 24 + +static int t[W]; + +static void sub_d(int p, int s, int x, int y) { + uint8_t r = (p % 16) * 16; + uint8_t g = 180 - p; + if (r < 10) { +// set_color(ANSI_COLOR_RESET); + } + else { + if (g > 170) { + set_color(ANSI_COLOR_WHITE); + } + else if (g < 170) { + set_color(ANSI_COLOR_GREEN); + } + } + + if ((y >= 0) && (y < H) && (x < W)) { + char c = (r < 10 ? ' ' : 33 + (x * y) % 94); + print_char(c, y, x); + //)vt_draw_char_at(y, x, c); + } +} + +void cmatrix() { + int i, x, y, k; + screen_clear(); + + x = rand(); + for (i = 0; i < W; i++) { + t[i] = - rand() % 50; + } + + sub_d(1,1,10,10); + + while (1) { + for (k = 1; k < W; k++) { + i = rand() % (W-1); + if (t[i] > 28)t[i] = 0; + t[i] = t[i] + 1; + y = t[i]; + sub_d( 0 , 0, i, y - 6); + sub_d( 2 + x, 0, i, y - 5); + sub_d( 2 + x, 0, i, y - 4); + sub_d( 10 + x, 0, i, y - 3); + sub_d( 10 + x, 0, i, y - 2); + sub_d( 11 + x, 0, i, y - 1); + sub_d( 0 , 2 + x, i, y); + } + + screen_refresh(); + usleep(100000); + } +} diff --git a/kernels/demo/src/donut/donut.c b/kernels/demo/src/donut/donut.c new file mode 100644 index 0000000..fe3d281 --- /dev/null +++ b/kernels/demo/src/donut/donut.c @@ -0,0 +1,82 @@ +/** + * Original author: + * https://twitter.com/a1k0n + * https://www.a1k0n.net/2021/01/13/optimizing-donut.html + * + * Change Logs: + * Date Author Notes + * 2006-09-15 Andy Sloane First version + * 2011-07-20 Andy Sloane Second version + * 2021-01-13 Andy Sloane Third version + * 2021-03-25 Meco Man Port to RT-Thread RTOS + */ + +#include +#include +#include +#include + +#define R(mul,shift,x,y) \ + _=x; \ + x -= mul*y>>shift; \ + y += mul*_>>shift; \ + _ = (3145728-x*x-y*y)>>11; \ + x = x*_>>10; \ + y = y*_>>10; + +static char b[1760]; +static signed char z[1760]; + +void donut(void) { + int sA = 1024, cA = 0, sB = 1024, cB = 0, _; + + while(1) { + screen_clear(); + + memset(b, 32, 1760); // text buffer + memset(z, 127, 1760); // z buffer + int sj = 0, cj = 1024; + for (int j = 0; j < 90; j++) { + int si = 0, ci = 1024; // sine and cosine of angle i + for (int i = 0; i < 324; i++) { + int R1 = 1, R2 = 2048, K2 = 5120*1024; + + int x0 = R1*cj + R2, + x1 = ci*x0 >> 10, + x2 = cA*sj >> 10, + x3 = si*x0 >> 10, + x4 = R1*x2 - (sA*x3 >> 10), + x5 = sA*sj >> 10, + x6 = K2 + R1*1024*x5 + cA*x3, + x7 = cj*si >> 10, + x = 25 + 30*(cB*x1 - sB*x4)/x6, + y = 12 + 15*(cB*x4 + sB*x1)/x6, + N = (((-cA*x7 - cB*((-sA*x7>>10) + x2) - ci*(cj*sB >> 10)) >> 10) - x5) >> 7; + + int o = x + 80 * y; + signed char zz = (x6-K2)>>15; + if (22 > y && y > 0 && x > 0 && 80 > x && zz < z[o]) { + z[o] = zz; + b[o] = ".,-~:;=!*#$@"[N > 0 ? N : 0]; + } + R(5, 8, ci, si) // rotate i + } + R(9, 7, cj, sj) // rotate j + } + int y = 0, x = 0; + for (int k = 0; 1761 > k; k++) { + if (k % 80) { + if (x < 50) print_char(b[k], y, x); + x ++; + } else { + y ++; + x = 1; + } + } + R(5, 7, cA, sA); + R(5, 8, cB, sB); + + screen_refresh(); + usleep(100000); + } +} diff --git a/kernels/demo/src/galton/galton.c b/kernels/demo/src/galton/galton.c new file mode 100644 index 0000000..3522a89 --- /dev/null +++ b/kernels/demo/src/galton/galton.c @@ -0,0 +1,96 @@ +/* from http://rosettacode.org/wiki/Galton_box_animation#C */ + +#include +#include +#include +#include + +#define BALLS 1024 +#define N 8 +#define W (N * 2 + 1) +#define H_MAX 50 +#define H_MIN 30 +static int h = 0; +static int *x = NULL, *y = NULL, cnt = 0; +static char *b = NULL; + +#define B(y, x) b[(y)*W + x] +#define C(y, x) ' ' == b[(y)*W + x] +#define V(i) B(y[i], x[i]) +static int rnd(int a) { return rand() % a; } + +static void show_board() { + int i, j; + for (i = 0; i < h; i++) + for (j = 0; j < W; j++) { + if (B(i, j) == '*') { + set_color(C(i - 1, j) ? ANSI_COLOR_GREEN : ANSI_COLOR_RED); + print_char(B(i, j), i, 2 * j); + set_color(ANSI_COLOR_RESET); + } else { + print_char(B(i, j), i, 2 * j); + } + print_char(' ', i, 2 * j + 1); + } + screen_refresh(); +} + +static void init() { + int i, j; + screen_clear(); + b = malloc(W * h); + memset(b, ' ', W * h); + + x = malloc(sizeof(int) * BALLS * 2); + y = x + BALLS; + + for (i = 0; i < N; i++) + for (j = -i; j <= i; j += 2) + B(2 * i+2, j + W/2) = '*'; +} + +static void move(int idx) { + int xx = x[idx], yy = y[idx], c, kill = 0, sl = 3, o = 0; + + if (yy < 0) return; + if (yy == h - 1) { y[idx] = -1; return; } + + switch(c = B(yy + 1, xx)) { + case ' ': yy++; break; + case '*': sl = 1; + default: if (xx < W - 1 && C(yy, xx + 1) && C(yy + 1, xx + 1)) + if (!rnd(sl++)) o = 1; + if (xx && C(yy, xx - 1) && C(yy + 1, xx - 1)) + if (!rnd(sl++)) o = -1; + if (!o) kill = 1; + xx += o; + } + + c = V(idx); V(idx) = ' '; + idx[y] = yy, idx[x] = xx; + B(yy, xx) = c; + if (kill) idx[y] = -1; +} + +static int run(void) { + static int step = 0; + int i; + for (i = 0; i < cnt; i++) move(i); + if (2 == ++step && cnt < BALLS) { + step = 0; + x[cnt] = W/2; + y[cnt] = 0; + if (V(cnt) != ' ') return 0; + V(cnt) = rnd(80) + 43; + cnt++; + } + return 1; +} + +void galton() { + h = screen_tile_height() - 2; + if (h > H_MAX) h = H_MAX; + if (h < H_MIN) h = H_MIN; + init(); + do { show_board(), usleep(6000); } while (run()); +} diff --git a/kernels/demo/src/hanoi/hanoi.c b/kernels/demo/src/hanoi/hanoi.c new file mode 100644 index 0000000..c5c44ec --- /dev/null +++ b/kernels/demo/src/hanoi/hanoi.c @@ -0,0 +1,60 @@ +/* from http://rosettacode.org/wiki/Towers_of_Hanoi#C */ + +#include +#include +#include +#include + +#define H 7 + +typedef struct { int *x, n; } tower; +static tower *new_tower(int cap) { + int size = sizeof(tower) + sizeof(int) * cap; + tower *t = malloc(size); + memset(t, 0, size); + t->x = (int*)(t + 1); + return t; +} + +static tower *t[3]; + +static void text(int y, int i, int d, const char *s) { + int yy = H - y + 1; + int xx = (H + 1) * (2 * i + 1) - d; + while (d--) { + for (const char *p = s; *p; p ++) { + print_char(*p, yy, xx ++); + } + } +} + +static void add_disk(int i, int d) { + t[i]->x[t[i]->n++] = d; + text(t[i]->n, i, d, "=="); + + usleep(100000); +} + +int remove_disk(int i) { + int d = t[i]->x[--t[i]->n]; + text(t[i]->n + 1, i, d, " "); + return d; +} + +void move(int n, int from, int to, int via) { + if (!n) return; + + move(n - 1, from, via, to); + add_disk(to, remove_disk(from)); + move(n - 1, via, to, from); +} + +void hanoi() { + screen_clear(); + + int c; + for (c = 0; c < 3; c++) t[c] = new_tower(H); + for (c = H; c; c--) add_disk(0, c); + + move(H, 0, 2, 1); +} diff --git a/kernels/demo/src/life/life.c b/kernels/demo/src/life/life.c new file mode 100644 index 0000000..25a30f1 --- /dev/null +++ b/kernels/demo/src/life/life.c @@ -0,0 +1,52 @@ +/* adapted from http://rosettacode.org/wiki/Conway%27s_Game_of_Life */ + +#include +#include +#include + +static int w = 40, h = 25; + +void show(void *u) { + int x,y; + int (*univ)[w] = u; + screen_clear(); + for (y=0;y +#include +#include + +void ant(); +void galton(); +void hanoi(); +void game_of_life(); +void aclock(); +void cmatrix(); +void donut(); + +int main(const char *args) { + ioe_init(); + + switch (args[0]) { + case '1': ant(); break; + case '2': galton(); break; + case '3': hanoi(); break; + case '4': game_of_life(); break; + case '5': aclock(); break; + case '6': cmatrix(); break; + case '7': donut(); break; + default: + printf("Usage: make run mainargs=*\n"); + printf(" 1: ant\n"); + printf(" 2: galton\n"); + printf(" 3: hanoi\n"); + printf(" 4: game of life\n"); + printf(" 5: aclock\n"); + printf(" 6: cmatrix\n"); + printf(" 7: donut\n"); + } + + printf("Press Q to Exit\n"); + while (1) { + AM_INPUT_KEYBRD_T ev = io_read(AM_INPUT_KEYBRD); + if (ev.keydown && ev.keycode == AM_KEY_Q) break; + } + return 0; +}