@mikrojs/firmware 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +30 -0
- package/bin/idf.py +7 -0
- package/chips.json +3 -0
- package/cmake.js +9 -0
- package/components/mikrojs/CMakeLists.txt +187 -0
- package/components/mikrojs/Kconfig +55 -0
- package/components/mikrojs/idf_component.yml +6 -0
- package/components/mikrojs/include/mem.h +3 -0
- package/components/mikrojs/include/mik_color.h +3 -0
- package/components/mikrojs/include/mik_http_internal.h +77 -0
- package/components/mikrojs/include/mikrojs.h +5 -0
- package/components/mikrojs/include/mikrojs_esp32.h +65 -0
- package/components/mikrojs/include/private.h +10 -0
- package/components/mikrojs/include/utils.h +3 -0
- package/components/mikrojs/mik_ble.cpp +1588 -0
- package/components/mikrojs/mik_ble_c_shim.c +61 -0
- package/components/mikrojs/mik_ble_c_shim.h +37 -0
- package/components/mikrojs/mik_config.cpp +167 -0
- package/components/mikrojs/mik_deploy.cpp +584 -0
- package/components/mikrojs/mik_http.cpp +916 -0
- package/components/mikrojs/mik_i2c.cpp +364 -0
- package/components/mikrojs/mik_main.cpp +542 -0
- package/components/mikrojs/mik_neopixel.cpp +437 -0
- package/components/mikrojs/mik_nvs_kv.cpp +219 -0
- package/components/mikrojs/mik_pin.cpp +195 -0
- package/components/mikrojs/mik_pwm.cpp +525 -0
- package/components/mikrojs/mik_recovery.cpp +86 -0
- package/components/mikrojs/mik_rtc.cpp +305 -0
- package/components/mikrojs/mik_serial_io.cpp +362 -0
- package/components/mikrojs/mik_sleep.cpp +226 -0
- package/components/mikrojs/mik_sntp.cpp +275 -0
- package/components/mikrojs/mik_spi.cpp +330 -0
- package/components/mikrojs/mik_uart.cpp +497 -0
- package/components/mikrojs/mik_wifi.cpp +1434 -0
- package/components/mikrojs/platform_esp32.cpp +192 -0
- package/components/mikrojs/test/CMakeLists.txt +32 -0
- package/components/mikrojs/test/abort_test.cpp +254 -0
- package/components/mikrojs/test/ble_test.cpp +714 -0
- package/components/mikrojs/test/fs_js_test.cpp +458 -0
- package/components/mikrojs/test/fs_pub_test.cpp +312 -0
- package/components/mikrojs/test/http_test.cpp +475 -0
- package/components/mikrojs/test/i2c_test.cpp +138 -0
- package/components/mikrojs/test/modules_extended_test.cpp +137 -0
- package/components/mikrojs/test/modules_test.cpp +131 -0
- package/components/mikrojs/test/pins_test.cpp +47 -0
- package/components/mikrojs/test/pwm_test.cpp +166 -0
- package/components/mikrojs/test/repl_protocol_test.cpp +405 -0
- package/components/mikrojs/test/rtc_test.cpp +331 -0
- package/components/mikrojs/test/runtime_test.cpp +89 -0
- package/components/mikrojs/test/sleep_test.cpp +222 -0
- package/components/mikrojs/test/sntp_test.cpp +249 -0
- package/components/mikrojs/test/stdio_test.cpp +449 -0
- package/components/mikrojs/test/sys_test.cpp +165 -0
- package/components/mikrojs/test/text_encoding_test.cpp +224 -0
- package/components/mikrojs/test/timers_js_test.cpp +244 -0
- package/components/mikrojs/test/timers_test.cpp +79 -0
- package/components/mikrojs/test/wifi_test.cpp +599 -0
- package/default-app/main/CMakeLists.txt +3 -0
- package/default-app/main/main.cpp +5 -0
- package/discover.js +77 -0
- package/index.d.ts +7 -0
- package/index.js +20 -0
- package/package.json +61 -0
- package/partitions.csv +5 -0
- package/prebuilds/esp32/bootloader/bootloader.bin +0 -0
- package/prebuilds/esp32/flasher_args.json +24 -0
- package/prebuilds/esp32/mikrojs.bin +0 -0
- package/prebuilds/esp32/partition_table/partition-table.bin +0 -0
- package/prebuilds/esp32c3/bootloader/bootloader.bin +0 -0
- package/prebuilds/esp32c3/flasher_args.json +24 -0
- package/prebuilds/esp32c3/mikrojs.bin +0 -0
- package/prebuilds/esp32c3/partition_table/partition-table.bin +0 -0
- package/prebuilds/esp32c6/bootloader/bootloader.bin +0 -0
- package/prebuilds/esp32c6/flasher_args.json +24 -0
- package/prebuilds/esp32c6/mikrojs.bin +0 -0
- package/prebuilds/esp32c6/partition_table/partition-table.bin +0 -0
- package/prebuilds/esp32s3/bootloader/bootloader.bin +0 -0
- package/prebuilds/esp32s3/flasher_args.json +24 -0
- package/prebuilds/esp32s3/mikrojs.bin +0 -0
- package/prebuilds/esp32s3/partition_table/partition-table.bin +0 -0
- package/project.cmake +101 -0
- package/resolve.js +54 -0
- package/sdkconfig.defaults +127 -0
- package/sdkconfig.defaults.esp32 +8 -0
- package/sdkconfig.defaults.esp32c3 +15 -0
- package/sdkconfig.defaults.esp32c6 +26 -0
- package/sdkconfig.defaults.esp32s3 +22 -0
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
#include "driver/gpio.h"
|
|
2
|
+
#include "esp_adc/adc_cali.h"
|
|
3
|
+
#include "esp_adc/adc_cali_scheme.h"
|
|
4
|
+
#include "esp_adc/adc_oneshot.h"
|
|
5
|
+
#include "mikrojs/private.h"
|
|
6
|
+
#include "mikrojs/utils.h"
|
|
7
|
+
|
|
8
|
+
/* ── GPIO helpers ────────────────────────────────────────────────── */
|
|
9
|
+
|
|
10
|
+
static esp_err_t mik_gpio_reset_pin(const int pin) {
|
|
11
|
+
return gpio_reset_pin(static_cast<gpio_num_t>(pin));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
static esp_err_t mik_gpio_set_pin_mode(const int pin, const gpio_mode_t mode) {
|
|
15
|
+
return gpio_set_direction(static_cast<gpio_num_t>(pin), mode);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
static esp_err_t mik_gpio_digital_write(const int pin, const int value) {
|
|
19
|
+
return gpio_set_level(static_cast<gpio_num_t>(pin), value);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
static int mik_gpio_digital_read(const int pin) {
|
|
23
|
+
return gpio_get_level(static_cast<gpio_num_t>(pin));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/* ── ADC helpers ─────────────────────────────────────────────────── */
|
|
27
|
+
|
|
28
|
+
static adc_oneshot_unit_handle_t s_adc_handle = NULL;
|
|
29
|
+
static adc_cali_handle_t s_adc_cali[4] = {}; // one per attenuation level
|
|
30
|
+
|
|
31
|
+
static esp_err_t mik_adc_ensure_init() {
|
|
32
|
+
if (s_adc_handle) return ESP_OK;
|
|
33
|
+
adc_oneshot_unit_init_cfg_t cfg = {.unit_id = ADC_UNIT_1};
|
|
34
|
+
return adc_oneshot_new_unit(&cfg, &s_adc_handle);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static adc_channel_t mik_adc_pin_to_channel(int pin) {
|
|
38
|
+
adc_channel_t channel;
|
|
39
|
+
adc_unit_t unit;
|
|
40
|
+
esp_err_t err = adc_oneshot_io_to_channel(pin, &unit, &channel);
|
|
41
|
+
if (err != ESP_OK || unit != ADC_UNIT_1) return (adc_channel_t)-1;
|
|
42
|
+
return channel;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static int mik_adc_read_raw(int pin, adc_atten_t atten) {
|
|
46
|
+
adc_channel_t ch = mik_adc_pin_to_channel(pin);
|
|
47
|
+
if (ch == (adc_channel_t)-1) return -1;
|
|
48
|
+
|
|
49
|
+
adc_oneshot_chan_cfg_t chan_cfg = {
|
|
50
|
+
.atten = atten,
|
|
51
|
+
.bitwidth = ADC_BITWIDTH_DEFAULT,
|
|
52
|
+
};
|
|
53
|
+
if (adc_oneshot_config_channel(s_adc_handle, ch, &chan_cfg) != ESP_OK) return -1;
|
|
54
|
+
|
|
55
|
+
int raw = 0;
|
|
56
|
+
if (adc_oneshot_read(s_adc_handle, ch, &raw) != ESP_OK) return -1;
|
|
57
|
+
return raw;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
static adc_cali_handle_t mik_adc_get_cali(adc_atten_t atten) {
|
|
61
|
+
int idx = (int)atten;
|
|
62
|
+
if (s_adc_cali[idx]) return s_adc_cali[idx];
|
|
63
|
+
|
|
64
|
+
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
|
|
65
|
+
adc_cali_curve_fitting_config_t cfg = {
|
|
66
|
+
.unit_id = ADC_UNIT_1,
|
|
67
|
+
.atten = atten,
|
|
68
|
+
.bitwidth = ADC_BITWIDTH_DEFAULT,
|
|
69
|
+
};
|
|
70
|
+
if (adc_cali_create_scheme_curve_fitting(&cfg, &s_adc_cali[idx]) != ESP_OK) return NULL;
|
|
71
|
+
#elif ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED
|
|
72
|
+
adc_cali_line_fitting_config_t cfg = {
|
|
73
|
+
.unit_id = ADC_UNIT_1,
|
|
74
|
+
.atten = atten,
|
|
75
|
+
.bitwidth = ADC_BITWIDTH_DEFAULT,
|
|
76
|
+
};
|
|
77
|
+
if (adc_cali_create_scheme_line_fitting(&cfg, &s_adc_cali[idx]) != ESP_OK) return NULL;
|
|
78
|
+
#else
|
|
79
|
+
return NULL;
|
|
80
|
+
#endif
|
|
81
|
+
return s_adc_cali[idx];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
static int mik_adc_read_millivolts(int pin, adc_atten_t atten) {
|
|
85
|
+
int raw = mik_adc_read_raw(pin, atten);
|
|
86
|
+
if (raw < 0) return -1;
|
|
87
|
+
|
|
88
|
+
adc_cali_handle_t cali = mik_adc_get_cali(atten);
|
|
89
|
+
if (!cali) return -1;
|
|
90
|
+
|
|
91
|
+
int mv = 0;
|
|
92
|
+
if (adc_cali_raw_to_voltage(cali, raw, &mv) != ESP_OK) return -1;
|
|
93
|
+
return mv;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/* ── native:pin JS module ────────────────────────────────────────────── */
|
|
97
|
+
|
|
98
|
+
static JSValue mik__pin_mode(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
|
|
99
|
+
int32_t pin, mode;
|
|
100
|
+
if (JS_ToInt32(ctx, &pin, argv[0])) return JS_EXCEPTION;
|
|
101
|
+
if (JS_ToInt32(ctx, &mode, argv[1])) return JS_EXCEPTION;
|
|
102
|
+
|
|
103
|
+
esp_err_t err = mik_gpio_reset_pin(pin);
|
|
104
|
+
if (err != ESP_OK)
|
|
105
|
+
return mik__result_err_named(ctx, "InvalidPin", "failed to reset pin %d: %s", pin,
|
|
106
|
+
esp_err_to_name(err));
|
|
107
|
+
|
|
108
|
+
err = mik_gpio_set_pin_mode(pin, static_cast<gpio_mode_t>(mode));
|
|
109
|
+
if (err != ESP_OK)
|
|
110
|
+
return mik__result_err_named(ctx, "SetModeFailed",
|
|
111
|
+
"failed to set mode on pin %d: %s", pin,
|
|
112
|
+
esp_err_to_name(err));
|
|
113
|
+
|
|
114
|
+
return mik__result_ok_void(ctx);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
static JSValue mik__pin_digital_write(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
|
|
118
|
+
int32_t pin, value;
|
|
119
|
+
if (JS_ToInt32(ctx, &pin, argv[0])) return JS_EXCEPTION;
|
|
120
|
+
if (JS_ToInt32(ctx, &value, argv[1])) return JS_EXCEPTION;
|
|
121
|
+
|
|
122
|
+
esp_err_t err = mik_gpio_digital_write(pin, value);
|
|
123
|
+
if (err != ESP_OK)
|
|
124
|
+
return mik__result_err_named(ctx, "WriteFailed", "failed to write to pin %d: %s", pin,
|
|
125
|
+
esp_err_to_name(err));
|
|
126
|
+
|
|
127
|
+
return mik__result_ok_void(ctx);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
static JSValue mik__pin_digital_read(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
|
|
131
|
+
int32_t pin;
|
|
132
|
+
if (JS_ToInt32(ctx, &pin, argv[0])) return JS_EXCEPTION;
|
|
133
|
+
return JS_NewInt32(ctx, mik_gpio_digital_read(pin));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
static JSValue mik__pin_analog_read(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
|
|
137
|
+
int32_t pin, atten;
|
|
138
|
+
if (JS_ToInt32(ctx, &pin, argv[0])) return JS_EXCEPTION;
|
|
139
|
+
if (JS_ToInt32(ctx, &atten, argv[1])) return JS_EXCEPTION;
|
|
140
|
+
|
|
141
|
+
esp_err_t err = mik_adc_ensure_init();
|
|
142
|
+
if (err != ESP_OK)
|
|
143
|
+
return mik__result_err_named(ctx, "AdcInitFailed",
|
|
144
|
+
"failed to initialize ADC unit: %s", esp_err_to_name(err));
|
|
145
|
+
|
|
146
|
+
int raw = mik_adc_read_raw(pin, static_cast<adc_atten_t>(atten));
|
|
147
|
+
if (raw < 0) return mik__result_err_tag(ctx, "InvalidAdcPin");
|
|
148
|
+
|
|
149
|
+
return mik__result_ok(ctx, JS_NewInt32(ctx, raw));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
static JSValue mik__pin_analog_read_millivolts(JSContext* ctx, JSValue this_val, int argc,
|
|
153
|
+
JSValue* argv) {
|
|
154
|
+
int32_t pin, atten;
|
|
155
|
+
if (JS_ToInt32(ctx, &pin, argv[0])) return JS_EXCEPTION;
|
|
156
|
+
if (JS_ToInt32(ctx, &atten, argv[1])) return JS_EXCEPTION;
|
|
157
|
+
|
|
158
|
+
esp_err_t err = mik_adc_ensure_init();
|
|
159
|
+
if (err != ESP_OK)
|
|
160
|
+
return mik__result_err_named(ctx, "AdcInitFailed",
|
|
161
|
+
"failed to initialize ADC unit: %s", esp_err_to_name(err));
|
|
162
|
+
|
|
163
|
+
int mv = mik_adc_read_millivolts(pin, static_cast<adc_atten_t>(atten));
|
|
164
|
+
if (mv < 0) return mik__result_err_tag(ctx, "InvalidAdcPin");
|
|
165
|
+
|
|
166
|
+
return mik__result_ok(ctx, JS_NewInt32(ctx, mv));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
static int mik__pin_module_init(JSContext* ctx, JSModuleDef* m) {
|
|
170
|
+
JS_SetModuleExport(ctx, m, "pinMode",
|
|
171
|
+
JS_NewCFunction(ctx, mik__pin_mode, "pinMode", 2));
|
|
172
|
+
JS_SetModuleExport(ctx, m, "digitalWrite",
|
|
173
|
+
JS_NewCFunction(ctx, mik__pin_digital_write, "digitalWrite", 2));
|
|
174
|
+
JS_SetModuleExport(ctx, m, "digitalRead",
|
|
175
|
+
JS_NewCFunction(ctx, mik__pin_digital_read, "digitalRead", 1));
|
|
176
|
+
JS_SetModuleExport(ctx, m, "analogRead",
|
|
177
|
+
JS_NewCFunction(ctx, mik__pin_analog_read, "analogRead", 2));
|
|
178
|
+
JS_SetModuleExport(ctx, m, "analogReadMillivolts",
|
|
179
|
+
JS_NewCFunction(ctx, mik__pin_analog_read_millivolts,
|
|
180
|
+
"analogReadMillivolts", 2));
|
|
181
|
+
return 0;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
static JSModuleDef* mik__pin_init(JSContext* ctx) {
|
|
185
|
+
JSModuleDef* m = JS_NewCModule(ctx, "native:pin", mik__pin_module_init);
|
|
186
|
+
if (!m) return nullptr;
|
|
187
|
+
JS_AddModuleExport(ctx, m, "pinMode");
|
|
188
|
+
JS_AddModuleExport(ctx, m, "digitalWrite");
|
|
189
|
+
JS_AddModuleExport(ctx, m, "digitalRead");
|
|
190
|
+
JS_AddModuleExport(ctx, m, "analogRead");
|
|
191
|
+
JS_AddModuleExport(ctx, m, "analogReadMillivolts");
|
|
192
|
+
return m;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
MIK_REGISTER_MODULE(pin, "native:pin", mik__pin_init, nullptr, nullptr)
|