Skip to content

Commit b3d9ebf

Browse files
committed
A bit in advance on the example script, I need to test these additions, then write the documentation.
1 parent aefb807 commit b3d9ebf

File tree

4 files changed

+113
-68
lines changed

4 files changed

+113
-68
lines changed

README.md

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,61 @@ This script should be located in the `javascript.py` file, that you can create,
2323

2424
If you want a demo, use [this `javascript.py` script](https://my.numworks.com/python/lilian-besson-1/javascript), that you can install on your NumWorks calculator, directly from their website (from my user space).
2525

26+
[![TODO: Video documentation showing the icon in my list of applications](screenshot-documentations/video-documentation-icon-of-application.gif)](screenshot-documentations/video-documentation-icon-of-application.gif)
27+
28+
----
29+
2630
## Dependencies
2731

2832
This programs uses [the code generated by the Espruino project](https://github.com/espruino/Espruino/blob/master/README_Building.md#embedding-in-other-applications) (`espruino_embedded.c`, `espruino_embedded.h` etc), a portable JavaScript interpreter for microcontrolers.
2933

34+
### My fork of Espruino
35+
3036
I've started to work on [my fork of Espruino](https://github.com/Naereen/Espruino), in order to solve [this issue](https://github.com/Naereen/A-JavaScript-interpreter-for-the-NumWorks-calculator/issues/2): I wanted to give access to NumWorks's EADK library (see [`eadk.h`](https://github.com/numworks/epsilon/blob/master/eadk/include/eadk/eadk.h)) to the JavaScript files that are executed in this Espruino JS interpreter.
3137
*It's a work in progress!*
3238

39+
To rebuild the two `espruino_embedded.c` and `espruino_embedded.h` files from Espruino's source code, you must run this in its main directory (of my fork, not the main project):
40+
41+
```bash
42+
BOARD=EMBED RELEASE=1 V=1 make
43+
```
44+
45+
----
46+
3347
## Documentation of the `Eadk` module accessible in JavaScript on the NumWorks
3448

3549
Here is a short documentation for each function that I've ported from their interface in [`eadk.h`](https://github.com/numworks/epsilon/blob/master/eadk/include/eadk/eadk.h) to a working version in JavaScript.
3650

37-
### Controlling the screen's brightness
38-
#### `Eadk.backlight_brightness() -> int`
51+
### Eadk predefined colors
52+
`Eadk.color_black`, `Eadk.color_white`, `Eadk.color_red`, `Eadk.color_green`, `Eadk.color_blue` are the five predefined colors.
53+
54+
### Screen width and height
55+
`Eadk.SCREEN_WIDTH` and `Eadk.SCREEN_HEIGHT` are the screen's width and height, respectively.
56+
57+
### ✅ Controlling the screen's brightness
58+
#### `int Eadk.backlight_brightness()`
3959

4060
Returns the screen's brightness, it's a 8 bits integer (`uint8_t` in C), ranging between 0 (min brightness, screen almost shut down) to 240 (for max brightness).
4161

42-
#### `Eadk.set_backlight_brightness(int brightness)`
62+
#### `void Eadk.set_backlight_brightness(int brightness)`
4363

4464
Sets the screen's brightness to this value.
4565
`brightness` **must** be an integer value which fits inside a `uint8_t`, between 0 and 256.
4666

47-
### Eadk predefined colors
48-
`Eadk.color_black`, `Eadk.color_white`, `Eadk.color_red`, `Eadk.color_green`, `Eadk.color_blue` are the five predefined colors.
67+
### Accessing the Battery levels
68+
69+
### `bool eadk_battery_is_charging()`
70+
71+
Indicates whether the battery is charging.
72+
73+
### `uint8_t eadk_battery_level()`
74+
75+
Returns a 8 bits integer giving the battery level.
76+
77+
### `float eadk_battery_voltage()`
78+
79+
Returns a floating value of the battery voltage (in Volt, I guess?).
80+
4981

5082
### How to add new functions to Espruino JavaScript's `Eadk` module?
5183

src/main.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ void ejs_print(const char *str) {
3838
// int main(int argc, char ** argv) {
3939
int main() {
4040

41-
printf("Embedded Espruino v0.0.1\n");
41+
printf("NumWorks's Embedded JavaScript interpreter v0.0.2\n");
42+
eadk_timing_msleep(1000);
43+
printf("Based on Espruino, by Naereen\n");
4244
eadk_timing_msleep(1000);
4345

4446
ejs_create(1000);
@@ -55,7 +57,7 @@ int main() {
5557
// DONE: I wasn't able to compile while depending on external data, but it works if reading from a local 'javascript.py' file.
5658
// const char * code = eadk_external_data;
5759

58-
const char * code = (code_from_file == NULL && file_len <= 0) ? "console.log(\"\\nHi from JavaScript interpreter! sleep(3s)\")\n// eadk.timing_msleep(3000)\ntypeof(NaN)" : (code_from_file + 1);
60+
const char * code = (code_from_file == NULL && file_len <= 0) ? "console.log(\"Hi from JavaScript interpreter! sleep(3s)\");\n// Eadk.timing_msleep(3000);\ntypeof(NaN);" : (code_from_file + 1);
5961

6062
printf("Executing code...\n");
6163
eadk_timing_msleep(1000);

src/test.js

Lines changed: 72 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,84 @@
11
// This is NOT a Python script
22
// This is a JavaScript file!
33

4-
console.log("Hello world from JavaScript! Testing.\n");
5-
console.log("Hello world from JavaScript! Testing.\n");
6-
console.log("Hello world from JavaScript! Testing.\n");
7-
Eadk.timing_msleep(1000);
4+
// A bit hacky experimental: just a for loop, to sleep
5+
function msleep(s) {
6+
if (Eadk.timing_msleep instanceof Function) {
7+
Eadk.timing_msleep(s);
8+
} else {
9+
for (let j = 1; j <= s; j++) {
10+
// Just a comment here
11+
}
12+
}
13+
}
814

9-
const brightness = Eadk.backlight_brightness();
10-
console.log("Eadk.backlight_brightness() =", Eadk.backlight_brightness());
11-
Eadk.timing_msleep(1000);
15+
console.log("Hello world from JavaScript!");
16+
console.log("Testing Eadk functions:");
17+
msleep(5000);
1218

13-
Eadk.set_backlight_brightness(0);
19+
const brightness = Eadk.backlight_brightness();
1420
console.log("Eadk.backlight_brightness() =", Eadk.backlight_brightness());
15-
Eadk.timing_msleep(1000);
21+
msleep(2000);
1622

17-
Eadk.set_backlight_brightness(brightness);
18-
console.log("Eadk.backlight_brightness() =", Eadk.backlight_brightness());
19-
Eadk.timing_msleep(1000);
23+
// Iterate some times from full brightness to zero brightness
24+
const number_of_dwarfs = 13;
25+
for (let dwarf = 1; dwarf <= number_of_dwarfs; dwarf++) {
2026

21-
for (let j = 1; j <= 1; j++) {
22-
// Just a comment here
23-
for (let i = 1; i <= 2; i++) {
24-
Eadk.display_draw_string("Hello", 10 * i, 10 * i + 20 * j, 1); // 1 pour grand texte
27+
// Let's go into the dark
28+
for (let b = brightness; b >= 0; b=b-16) {
29+
Eadk.set_backlight_brightness(b);
30+
console.log("Eadk.backlight_brightness() =", Eadk.backlight_brightness());
31+
msleep(50);
2532
}
26-
for (let i = 3; i <= 4; i++) {
27-
Eadk.display_draw_string("Hello", 10 * i, 10 * i + 20 * j, 0); // 0 pour petit texte
28-
}
29-
for (let i = 5; i <= 6; i++) {
30-
Eadk.display_draw_string("Hello", 10 * i, 10 * i + 20 * j, 0);
33+
34+
// And back into the light!
35+
for (let b = 0; b <= brightness; b=b+16) {
36+
Eadk.set_backlight_brightness(b);
37+
console.log("Eadk.backlight_brightness() =", Eadk.backlight_brightness());
38+
msleep(50);
3139
}
40+
}
3241

33-
let i = 7;
34-
Eadk.display_draw_string("Hello", 10 * i, 10 * i + 20 * j, 0, Eadk.color_red);
35-
i++;
36-
Eadk.display_draw_string("Hello", 10 * i, 10 * i + 20 * j, 0, Eadk.color_green);
37-
i++;
38-
Eadk.display_draw_string("Hello", 10 * i, 10 * i + 20 * j, 0, Eadk.color_blue);
39-
i++;
42+
msleep(1000);
4043

41-
Eadk.timing_msleep(1000);
42-
}
44+
// Let's test the constants SCREEN_WIDTH, SCREEN_HEIGHT
45+
console.log("Eadk.SCREEN_WIDTH =", Eadk.SCREEN_WIDTH);
46+
msleep(1000);
47+
console.log("Eadk.SCREEN_HEIGHT =", Eadk.SCREEN_HEIGHT);
48+
msleep(1000);
49+
50+
// Let's test the battery API
51+
console.log("Eadk.battery_is_charging() =", Eadk.battery_is_charging());
52+
msleep(1000);
53+
console.log("Eadk.battery_level() =", Eadk.battery_level());
54+
msleep(1000);
55+
console.log("Eadk.battery_voltage() =", Eadk.battery_voltage());
56+
msleep(1000);
57+
58+
59+
// Let's test the colors and display_draw_string
60+
// First display on white background, and big text
61+
const big_text = 1; // 1 for big text
62+
Eadk.display_draw_string("Hello in red on white ?", 20, 10, big_text, Eadk.color_red, Eadk.color_white);
63+
msleep(1000);
64+
Eadk.display_draw_string("Hello in green on white ?", 40, 10, big_text, Eadk.color_green, Eadk.color_white);
65+
msleep(1000);
66+
Eadk.display_draw_string("Hello in blue on white ?", 60, 10, big_text, Eadk.color_blue, Eadk.color_white);
67+
msleep(1000);
68+
Eadk.display_draw_string("Hello in black on white ?", 60, 10, big_text, Eadk.color_black, Eadk.color_white);
69+
msleep(1000);
70+
71+
// Then display on black background, and small text
72+
const small_text = 0; // 0 for small text
73+
Eadk.display_draw_string("Hello in red on black ?", 20, 10, small_text, Eadk.color_red, Eadk.color_black);
74+
msleep(1000);
75+
Eadk.display_draw_string("Hello in green on black ?", 40, 10, small_text, Eadk.color_green, Eadk.color_black);
76+
msleep(1000);
77+
Eadk.display_draw_string("Hello in blue on black ?", 60, 10, small_text, Eadk.color_blue, Eadk.color_black);
78+
msleep(1000);
79+
Eadk.display_draw_string("Hello in white on black ?", 60, 10, small_text, Eadk.color_white, Eadk.color_black);
80+
msleep(1000);
81+
82+
// Finish for this test script
83+
console.log("End for the tests of Eadk functions!");
84+
msleep(1000);

src/test2.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)