Skip to content

Commit 1c6c434

Browse files
committed
Added a new function to Eadk module: void eadk_display_push_rect_uniform(eadk_rect_t rect, eadk_color_t color);
1 parent 5532d67 commit 1c6c434

File tree

3 files changed

+122
-36
lines changed

3 files changed

+122
-36
lines changed

README.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,41 @@ Returns a floating value of the battery voltage (in Volt, I guess?).
105105
> See [this issue on NumWorks/epsilon's repository](https://github.com/numworks/epsilon/issues/2326)
106106
> DONE: [I just implemented them myself, by SVC calls](https://github.com/Naereen/A-JavaScript-interpreter-for-the-NumWorks-calculator/issues/5)
107107
108-
### ✅ Display
108+
### ✅ Display
109109

110110
#### ❌✅ `void Eadk.display_draw_string(const char* text, uint16_t x, uint16_t y, bool large_font, uint16_t text_color, uint16_t background_color)`
111111

112112
Displays a given `text` string, at a `{x,y}` position, in large/small font (`large_font`?), with the text in `text_color` and the background in `background_color`.
113113

114-
For instance:
115114
```javascript
116115
Eadk.display_draw_string(text, x, y, large, text_color, background_color);
117116
```
118117

119118
> TODO: it's still a bit buggy!
120119
120+
#### `void Eadk.display_push_rect_uniform(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t color)`
121+
122+
Fills a rectangle on the display with a uniform color.
123+
This can also be used to draw a single pixel by setting width and height to 1.
124+
125+
- `x`: X coordinate of the top-left corner,
126+
- `y`: Y coordinate of the top-left corner,
127+
- `width`: Width of the rectangle,
128+
- `height`: Height of the rectangle,
129+
- `color`: Color of the rectangle (e.g., `0xFFFF` for white, `0xF800` for red).
130+
131+
For instance:
132+
```javascript
133+
Eadk.display_push_rect_uniform(0, 0, 10, 20, 0xF800); // Fills a 10x20 red rectangle at (0,0)
134+
Eadk.display_push_rect_uniform(50, 50, 1, 1, 0x07E0); // Draws a single green pixel at (50,50)
135+
```
136+
137+
#### Missing funtions
138+
139+
- `eadk_display_push_rect`: is missing.
140+
- `eadk_display_pull_rect`: is missing.
141+
- `eadk_display_wait_for_vblank`: is missing.
142+
121143
### ✅ Timing
122144

123145
#### `void Eadk.timing_usleep(uint32_t us)`
@@ -164,8 +186,8 @@ For a more complete and length example, see [`src/test.js`](src/test.js).
164186
// Save this to `javascript.py` on your NumWorks, and run it with
165187
// the "JS interpreter" NumWorks application!
166188

167-
console.log("Hello world from JavaScript!");
168-
console.log("Testing Eadk functions:");
189+
console.log("Hello world from JavaScript!\n");
190+
console.log("Testing some Eadk functions:\n");
169191
Eadk.timing_msleep(5000);
170192

171193
const brightness = Eadk.backlight_brightness();

src/javascript/espruino_embedded.c

Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10229,6 +10229,7 @@ _Bool jswrap_battery_is_charging();
1022910229
uint8_t jswrap_battery_level();
1023010230
float jswrap_battery_voltage();
1023110231
void jswrap_display_draw_string(JsVar *args);
10232+
void jswrap_display_push_rect_uniform(JsVar *args);
1023210233
void jswrap_timing_usleep(uint32_t us);
1023310234
void jswrap_timing_msleep(uint32_t ms);
1023410235
uint64_t jswrap_timing_millis();
@@ -10574,11 +10575,12 @@ static const JswSymPtr jswSymbols_Eadk[] = {
1057410575
{ 158, JSWAT_INT32 | JSWAT_EXECUTE_IMMEDIATELY, (void*)jswrap_color_red},
1057510576
{ 168, JSWAT_INT32 | JSWAT_EXECUTE_IMMEDIATELY, (void*)jswrap_color_white},
1057610577
{ 180, JSWAT_VOID | (JSWAT_ARGUMENT_ARRAY << ((((JSWAT_MASK+1)== 1)? 0: ((JSWAT_MASK+1)== 2)? 1: ((JSWAT_MASK+1)== 4)? 2: ((JSWAT_MASK+1)== 8)? 3: ((JSWAT_MASK+1)== 16)? 4: ((JSWAT_MASK+1)== 32)? 5: ((JSWAT_MASK+1)== 64)? 6: ((JSWAT_MASK+1)== 128)? 7: ((JSWAT_MASK+1)== 256)? 8: ((JSWAT_MASK+1)== 512)? 9: ((JSWAT_MASK+1)== 1024)?10: ((JSWAT_MASK+1)== 2048)?11: ((JSWAT_MASK+1)== 4096)?12: ((JSWAT_MASK+1)== 8192)?13: ((JSWAT_MASK+1)==16384)?14: ((JSWAT_MASK+1)==32768)?15:10000 )*1)), (void*)jswrap_display_draw_string},
10577-
{ 200, JSWAT_INT32, (void*)jswrap_random},
10578-
{ 207, JSWAT_INT32, (void*)jswrap_timing_millis},
10579-
{ 221, JSWAT_VOID | (JSWAT_INT32 << ((((JSWAT_MASK+1)== 1)? 0: ((JSWAT_MASK+1)== 2)? 1: ((JSWAT_MASK+1)== 4)? 2: ((JSWAT_MASK+1)== 8)? 3: ((JSWAT_MASK+1)== 16)? 4: ((JSWAT_MASK+1)== 32)? 5: ((JSWAT_MASK+1)== 64)? 6: ((JSWAT_MASK+1)== 128)? 7: ((JSWAT_MASK+1)== 256)? 8: ((JSWAT_MASK+1)== 512)? 9: ((JSWAT_MASK+1)== 1024)?10: ((JSWAT_MASK+1)== 2048)?11: ((JSWAT_MASK+1)== 4096)?12: ((JSWAT_MASK+1)== 8192)?13: ((JSWAT_MASK+1)==16384)?14: ((JSWAT_MASK+1)==32768)?15:10000 )*1)), (void*)jswrap_timing_msleep},
10580-
{ 235, JSWAT_VOID | (JSWAT_INT32 << ((((JSWAT_MASK+1)== 1)? 0: ((JSWAT_MASK+1)== 2)? 1: ((JSWAT_MASK+1)== 4)? 2: ((JSWAT_MASK+1)== 8)? 3: ((JSWAT_MASK+1)== 16)? 4: ((JSWAT_MASK+1)== 32)? 5: ((JSWAT_MASK+1)== 64)? 6: ((JSWAT_MASK+1)== 128)? 7: ((JSWAT_MASK+1)== 256)? 8: ((JSWAT_MASK+1)== 512)? 9: ((JSWAT_MASK+1)== 1024)?10: ((JSWAT_MASK+1)== 2048)?11: ((JSWAT_MASK+1)== 4096)?12: ((JSWAT_MASK+1)== 8192)?13: ((JSWAT_MASK+1)==16384)?14: ((JSWAT_MASK+1)==32768)?15:10000 )*1)), (void*)jswrap_timing_usleep},
10581-
{ 249, JSWAT_BOOL, (void*)jswrap_usb_is_plugged}
10578+
{ 200, JSWAT_VOID | (JSWAT_ARGUMENT_ARRAY << ((((JSWAT_MASK+1)== 1)? 0: ((JSWAT_MASK+1)== 2)? 1: ((JSWAT_MASK+1)== 4)? 2: ((JSWAT_MASK+1)== 8)? 3: ((JSWAT_MASK+1)== 16)? 4: ((JSWAT_MASK+1)== 32)? 5: ((JSWAT_MASK+1)== 64)? 6: ((JSWAT_MASK+1)== 128)? 7: ((JSWAT_MASK+1)== 256)? 8: ((JSWAT_MASK+1)== 512)? 9: ((JSWAT_MASK+1)== 1024)?10: ((JSWAT_MASK+1)== 2048)?11: ((JSWAT_MASK+1)== 4096)?12: ((JSWAT_MASK+1)== 8192)?13: ((JSWAT_MASK+1)==16384)?14: ((JSWAT_MASK+1)==32768)?15:10000 )*1)), (void*)jswrap_display_push_rect_uniform},
10579+
{ 226, JSWAT_INT32, (void*)jswrap_random},
10580+
{ 233, JSWAT_INT32, (void*)jswrap_timing_millis},
10581+
{ 247, JSWAT_VOID | (JSWAT_INT32 << ((((JSWAT_MASK+1)== 1)? 0: ((JSWAT_MASK+1)== 2)? 1: ((JSWAT_MASK+1)== 4)? 2: ((JSWAT_MASK+1)== 8)? 3: ((JSWAT_MASK+1)== 16)? 4: ((JSWAT_MASK+1)== 32)? 5: ((JSWAT_MASK+1)== 64)? 6: ((JSWAT_MASK+1)== 128)? 7: ((JSWAT_MASK+1)== 256)? 8: ((JSWAT_MASK+1)== 512)? 9: ((JSWAT_MASK+1)== 1024)?10: ((JSWAT_MASK+1)== 2048)?11: ((JSWAT_MASK+1)== 4096)?12: ((JSWAT_MASK+1)== 8192)?13: ((JSWAT_MASK+1)==16384)?14: ((JSWAT_MASK+1)==32768)?15:10000 )*1)), (void*)jswrap_timing_msleep},
10582+
{ 261, JSWAT_VOID | (JSWAT_INT32 << ((((JSWAT_MASK+1)== 1)? 0: ((JSWAT_MASK+1)== 2)? 1: ((JSWAT_MASK+1)== 4)? 2: ((JSWAT_MASK+1)== 8)? 3: ((JSWAT_MASK+1)== 16)? 4: ((JSWAT_MASK+1)== 32)? 5: ((JSWAT_MASK+1)== 64)? 6: ((JSWAT_MASK+1)== 128)? 7: ((JSWAT_MASK+1)== 256)? 8: ((JSWAT_MASK+1)== 512)? 9: ((JSWAT_MASK+1)== 1024)?10: ((JSWAT_MASK+1)== 2048)?11: ((JSWAT_MASK+1)== 4096)?12: ((JSWAT_MASK+1)== 8192)?13: ((JSWAT_MASK+1)==16384)?14: ((JSWAT_MASK+1)==32768)?15:10000 )*1)), (void*)jswrap_timing_usleep},
10583+
{ 275, JSWAT_BOOL, (void*)jswrap_usb_is_plugged}
1058210584
};
1058310585
static const unsigned char jswSymbolIndex_Eadk = 0;
1058410586
static const JswSymPtr jswSymbols_global[] = {
@@ -10900,7 +10902,7 @@ static const JswSymPtr jswSymbols_heatshrink[] = {
1090010902
{ 9, JSWAT_JSVAR | (JSWAT_JSVAR << ((((JSWAT_MASK+1)== 1)? 0: ((JSWAT_MASK+1)== 2)? 1: ((JSWAT_MASK+1)== 4)? 2: ((JSWAT_MASK+1)== 8)? 3: ((JSWAT_MASK+1)== 16)? 4: ((JSWAT_MASK+1)== 32)? 5: ((JSWAT_MASK+1)== 64)? 6: ((JSWAT_MASK+1)== 128)? 7: ((JSWAT_MASK+1)== 256)? 8: ((JSWAT_MASK+1)== 512)? 9: ((JSWAT_MASK+1)== 1024)?10: ((JSWAT_MASK+1)== 2048)?11: ((JSWAT_MASK+1)== 4096)?12: ((JSWAT_MASK+1)== 8192)?13: ((JSWAT_MASK+1)==16384)?14: ((JSWAT_MASK+1)==32768)?15:10000 )*1)), (void*)jswrap_heatshrink_decompress}
1090110903
};
1090210904
static const unsigned char jswSymbolIndex_heatshrink = 26;
10903-
static const char jswSymbols_Eadk_str[] = "SCREEN_HEIGHT\0SCREEN_WIDTH\0backlight_brightness\0backlight_set_brightness\0battery_is_charging\0battery_level\0battery_voltage\0color_black\0color_blue\0color_green\0color_red\0color_white\0display_draw_string\0random\0timing_millis\0timing_msleep\0timing_usleep\0usb_is_plugged\0";
10905+
static const char jswSymbols_Eadk_str[] = "SCREEN_HEIGHT\0SCREEN_WIDTH\0backlight_brightness\0backlight_set_brightness\0battery_is_charging\0battery_level\0battery_voltage\0color_black\0color_blue\0color_green\0color_red\0color_white\0display_draw_string\0display_push_rect_uniform\0random\0timing_millis\0timing_msleep\0timing_usleep\0usb_is_plugged\0";
1090410906
static const char jswSymbols_global_str[] = "Array\0ArrayBuffer\0ArrayBufferView\0Boolean\0DataView\0Date\0Eadk\0Error\0Float32Array\0Float64Array\0Function\0HIGH\0Infinity\0Int16Array\0Int32Array\0Int8Array\0InternalError\0JSON\0LOW\0Math\0Modules\0NaN\0Number\0Object\0ReferenceError\0RegExp\0String\0SyntaxError\0TypeError\0Uint16Array\0Uint24Array\0Uint32Array\0Uint8Array\0Uint8ClampedArray\0arguments\0atob\0btoa\0console\0decodeURIComponent\0encodeURIComponent\0eval\0global\0globalThis\0isFinite\0isNaN\0parseFloat\0parseInt\0print\0require\0trace\0";
1090510907
static const char jswSymbols_Array_proto_str[] = "concat\0every\0fill\0filter\0find\0findIndex\0forEach\0includes\0indexOf\0join\0length\0map\0pop\0push\0reduce\0reverse\0shift\0slice\0some\0sort\0splice\0toString\0unshift\0";
1090610908
static const char jswSymbols_Array_str[] = "isArray\0";
@@ -10928,7 +10930,7 @@ static const char jswSymbols_Modules_str[] = "addCached\0getCached\0removeAllCac
1092810930
static const char jswSymbols_Math_str[] = "E\0LN10\0LN2\0LOG10E\0LOG2E\0PI\0SQRT1_2\0SQRT2\0abs\0acos\0asin\0atan\0atan2\0ceil\0clip\0cos\0exp\0floor\0log\0max\0min\0pow\0randInt\0random\0round\0sign\0sin\0sqrt\0tan\0wrap\0";
1092910931
static const char jswSymbols_heatshrink_str[] = "compress\0decompress\0";
1093010932
const JswSymList jswSymbolTables[] = {
10931-
{jswSymbols_Eadk, jswSymbols_Eadk_str, 18},
10933+
{jswSymbols_Eadk, jswSymbols_Eadk_str, 19},
1093210934
{jswSymbols_global, jswSymbols_global_str, 50},
1093310935
{jswSymbols_Array_proto, jswSymbols_Array_proto_str, 23},
1093410936
{jswSymbols_Array, jswSymbols_Array_str, 1},
@@ -12450,6 +12452,7 @@ _Bool jswrap_battery_is_charging();
1245012452
uint8_t jswrap_battery_level();
1245112453
float jswrap_battery_voltage();
1245212454
void jswrap_display_draw_string(JsVar *args);
12455+
void jswrap_display_push_rect_uniform(JsVar *args);
1245312456
void jswrap_timing_usleep(uint32_t us);
1245412457
void jswrap_timing_msleep(uint32_t ms);
1245512458
uint64_t jswrap_timing_millis();
@@ -12495,42 +12498,87 @@ float jswrap_battery_voltage(void) {
1249512498
void jswrap_display_draw_string(JsVar *args) {
1249612499
int argc = jsvGetArrayLength(args);
1249712500
if (argc < 6) {
12498-
jsError("Arg error: too few args (%i)", argc);
1249912501
return;
1250012502
}
12501-
if (!jsvIsString(jsvGetArrayItem(args, 0))) {
12502-
jsError("Arg error: bad arg type [0]");
12503+
JsVar *textJsVar = jsvGetArrayItem(args, 0);
12504+
if (!jsvIsString(textJsVar)) {
12505+
jsError("Eadk.display_draw_string: Arg[0] (text) must be a string.");
1250312506
return;
1250412507
}
12505-
const char* text = jsvAsString(jsvGetArrayItem(args, 0));
12506-
if (!jsvIsInt(jsvGetArrayItem(args, 1))) {
12507-
jsError("Arg error: bad arg type [1]");
12508+
const char* text = jsvAsString(textJsVar);
12509+
JsVar *xJsVar = jsvGetArrayItem(args, 1);
12510+
if (!jsvIsInt(xJsVar)) {
12511+
jsError("Eadk.display_draw_string: Arg[1] (x) must be an integer.");
1250812512
return;
1250912513
}
12510-
uint16_t x = (uint16_t)jsvGetInteger(jsvGetArrayItem(args, 1));
12511-
if (!jsvIsInt(jsvGetArrayItem(args, 2))) {
12512-
jsError("Arg error: bad arg type [2]");
12514+
uint16_t x = (uint16_t)jsvGetInteger(xJsVar);
12515+
JsVar *yJsVar = jsvGetArrayItem(args, 2);
12516+
if (!jsvIsInt(yJsVar)) {
12517+
jsError("Eadk.display_draw_string: Arg[2] (y) must be an integer.");
1251312518
return;
1251412519
}
12515-
uint16_t y = (uint16_t)jsvGetInteger(jsvGetArrayItem(args, 2));
12516-
if (!jsvIsInt(jsvGetArrayItem(args, 3))) {
12517-
jsError("Arg error: bad arg type [3]");
12520+
uint16_t y = (uint16_t)jsvGetInteger(yJsVar);
12521+
JsVar *large_fontJsVar = jsvGetArrayItem(args, 3);
12522+
if (!jsvIsInt(large_fontJsVar)) {
12523+
jsError("Eadk.display_draw_string: Arg[3] (large_font) must be an integer.");
1251812524
return;
1251912525
}
12520-
_Bool large_font = jsvGetBool(jsvGetArrayItem(args, 3));
12521-
if (!jsvIsInt(jsvGetArrayItem(args, 4))) {
12522-
jsError("Arg error: bad arg type [4]");
12526+
_Bool large_font = jsvGetBool(large_fontJsVar);
12527+
JsVar *text_colorJsVar = jsvGetArrayItem(args, 4);
12528+
if (!jsvIsInt(text_colorJsVar)) {
12529+
jsError("Eadk.display_draw_string: Arg[4] (text_color) must be an integer.");
1252312530
return;
1252412531
}
12525-
uint16_t text_color = (uint16_t)jsvGetInteger(jsvGetArrayItem(args, 4));
12526-
if (!jsvIsInt(jsvGetArrayItem(args, 5))) {
12527-
jsError("Arg error: bad arg type [5]");
12532+
uint16_t text_color = (uint16_t)jsvGetInteger(text_colorJsVar);
12533+
JsVar *background_colorJsVar = jsvGetArrayItem(args, 5);
12534+
if (!jsvIsInt(background_colorJsVar)) {
12535+
jsError("Eadk.display_draw_string: Arg[5] (background_color) must be an integer.");
1252812536
return;
1252912537
}
12530-
uint16_t background_color = (uint16_t)jsvGetInteger(jsvGetArrayItem(args, 5));
12538+
uint16_t background_color = (uint16_t)jsvGetInteger(background_colorJsVar);
1253112539
eadk_display_draw_string(text, (eadk_point_t){x, y}, large_font, (eadk_color_t)text_color, (eadk_color_t)background_color);
1253212540
return;
1253312541
}
12542+
void jswrap_display_push_rect_uniform(JsVar *args) {
12543+
int argc = jsvGetArrayLength(args);
12544+
if (argc < 5) {
12545+
jsError("Eadk.display_fill_rect: Too few arguments (%i). Expected 5.", argc);
12546+
return;
12547+
}
12548+
JsVar *xJsVar = jsvGetArrayItem(args, 0);
12549+
if (!jsvIsInt(xJsVar)) {
12550+
jsError("Eadk.display_fill_rect: Arg[0] (x) must be an integer.");
12551+
return;
12552+
}
12553+
uint16_t x = (uint16_t)jsvGetInteger(xJsVar);
12554+
JsVar *yJsVar = jsvGetArrayItem(args, 1);
12555+
if (!jsvIsInt(yJsVar)) {
12556+
jsError("Eadk.display_fill_rect: Arg[1] (y) must be an integer.");
12557+
return;
12558+
}
12559+
uint16_t y = (uint16_t)jsvGetInteger(yJsVar);
12560+
JsVar *widthJsVar = jsvGetArrayItem(args, 2);
12561+
if (!jsvIsInt(widthJsVar)) {
12562+
jsError("Eadk.display_fill_rect: Arg[2] (width) must be an integer.");
12563+
return;
12564+
}
12565+
uint16_t width = (uint16_t)jsvGetInteger(widthJsVar);
12566+
JsVar *heightJsVar = jsvGetArrayItem(args, 3);
12567+
if (!jsvIsInt(heightJsVar)) {
12568+
jsError("Eadk.display_fill_rect: Arg[3] (height) must be an integer.");
12569+
return;
12570+
}
12571+
uint16_t height = (uint16_t)jsvGetInteger(heightJsVar);
12572+
JsVar *colorJsVar = jsvGetArrayItem(args, 4);
12573+
if (!jsvIsInt(colorJsVar)) {
12574+
jsError("Eadk.display_fill_rect: Arg[4] (color) must be an integer.");
12575+
return;
12576+
}
12577+
uint16_t color = (uint16_t)jsvGetInteger(colorJsVar);
12578+
eadk_rect_t rect = { .x = x, .y = y, .width = width, .height = height };
12579+
eadk_display_push_rect_uniform(rect, (eadk_color_t)color);
12580+
return;
12581+
}
1253412582
void jswrap_timing_usleep(uint32_t us) {
1253512583
return eadk_timing_usleep(us);
1253612584
}

src/test.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function msleep(s) {
1414
}
1515

1616
console.log("Hello world from JavaScript!");
17-
console.log("Testing Eadk functions:");
17+
console.log("Testing all Eadk functions:");
1818
msleep(1000);
1919

2020
//
@@ -46,6 +46,22 @@ msleep(1000);
4646
Eadk.display_draw_string("Hello in white on black ?", 0, 16*7, small_text, Eadk.color_white, Eadk.color_black);
4747
msleep(1000);
4848

49+
// Display rect
50+
console.log("Eadk.display_push_rect_uniform(0, 0, 10, 20, 0xF800)...");
51+
msleep(1000);
52+
Eadk.display_push_rect_uniform(190, 190, 10, 20, 0xF800); // Fills a 10x20 red rectangle at (190,190)
53+
msleep(1000);
54+
55+
console.log("Eadk.display_push_rect_uniform for lines...");
56+
msleep(1000);
57+
// Draws a line of single green pixels at (50,50) to (150,150)
58+
for (let line = 50; line < 150; line++) {
59+
Eadk.display_push_rect_uniform(line, line, 1, 1, 0x07E0);
60+
msleep(10);
61+
}
62+
63+
msleep(1000);
64+
4965
// Brightness
5066

5167
const brightness = Eadk.backlight_brightness();
@@ -95,12 +111,12 @@ msleep(1000);
95111
// Timing
96112
//
97113

98-
// Let's test the function Eadk.timing_usleep(2000000)
99-
console.log("Eadk.timing_usleep(2000000)...");
100-
Eadk.timing_usleep(2000000);
114+
// Let's test the function Eadk.timing_usleep(1000000)
115+
console.log("Eadk.timing_usleep(1000000)...");
116+
Eadk.timing_usleep(1000000);
101117

102-
// Let's test the function Eadk.timing_msleep(2000)
103-
console.log("Eadk.timing_msleep(2000)...");
118+
// Let's test the function Eadk.timing_msleep(1000)
119+
console.log("Eadk.timing_msleep(1000)...");
104120
Eadk.timing_msleep(1000);
105121

106122
// Let's test the function Eadk.timing_millis()

0 commit comments

Comments
 (0)