Skip to content

Commit 6393f5c

Browse files
author
karurochari
committed
Ready for release
1 parent b7bb079 commit 6393f5c

File tree

7 files changed

+62
-23
lines changed

7 files changed

+62
-23
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
> [!WARNING]
2-
> This module is a work in progress. Not functional yet.
3-
41
> [!WARNING]
52
> This module will only work on the for [stable-gluegunfw](https://github.com/KaruroChori/txiki.js/tree/stable-gluegunfw)
63

TODO.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- [ ] ls to search for all available serials
2+
- [ ] reconfiguration of the serial

src/dev/main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
#include <sow.h>
44
using namespace std;
55

6+
using namespace __MODULE__;
7+
68
int main(){
79
cfg_serial cfg;
8-
int fd = __MODULE___configure("/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0",cfg);
10+
int fd = configure("/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0",cfg);
911
cout<<"Hello world!";
1012
close(fd);
1113
return 0;

src/native/module.cpp

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,57 @@
11
#include "module.h"
22
#include "sow.h"
33

4+
using namespace __MODULE__;
5+
46
extern "C" {
57

8+
//Most checks are skipped since the json schema is validated before running this.
9+
//So structure is assumed to be enforced.
610
static void tjs_obj2cfg(JSContext *ctx, JSValue obj, cfg_serial *cfg) {
7-
/*JSValue parity = JS_GetPropertyStr(ctx, obj, "parity");
8-
if (!JS_IsUndefined(parity)){
9-
JS
10-
JSToCString(ctx, &cfg->parity, family);
11+
{
12+
JSValue value = JS_GetPropertyStr(ctx, obj, "parity");
13+
const char* buffer;
14+
if (!JS_IsUndefined(value)){
15+
buffer = JS_ToCString(ctx, value);
16+
if(strcmp("enabled",buffer)==0) cfg->parity = feature_t::ENABLED;
17+
else if(strcmp("disabled",buffer)==0) cfg->parity = feature_t::DISABLED;
18+
JS_FreeCString(ctx, buffer);
19+
}
20+
JS_FreeValue(ctx, value);
21+
}
22+
23+
{
24+
JSValue value = JS_GetPropertyStr(ctx, obj, "bits");
25+
uint32_t buffer;
26+
if (!JS_IsUndefined(value)){
27+
JS_ToUint32(ctx, &buffer, value);
28+
cfg->bits = static_cast<bits_t>((uint8_t)buffer-5);
29+
}
30+
JS_FreeValue(ctx, value);
1131
}
12-
JS_FreeValue(ctx, parity);
1332

14-
JSValue socktype = JS_GetPropertyStr(ctx, obj, "socktype");
15-
if (!JS_IsUndefined(socktype))
16-
JS_ToInt32(ctx, &ai->ai_socktype, socktype);
17-
JS_FreeValue(ctx, socktype);
1833

19-
JSValue protocol = JS_GetPropertyStr(ctx, obj, "protocol");
20-
if (!JS_IsUndefined(protocol))
21-
JS_ToInt32(ctx, &ai->ai_protocol, protocol);
22-
JS_FreeValue(ctx, protocol);
23-
*/
34+
{
35+
JSValue value = JS_GetPropertyStr(ctx, obj, "stop");
36+
uint32_t buffer;
37+
if (!JS_IsUndefined(value)){
38+
JS_ToUint32(ctx, &buffer, value);
39+
cfg->stop = static_cast<stop_t>((uint8_t)buffer-1);
40+
}
41+
JS_FreeValue(ctx, value);
42+
}
43+
44+
{
45+
JSValue value = JS_GetPropertyStr(ctx, obj, "hardware_flow");
46+
const char* buffer;
47+
if (!JS_IsUndefined(value)){
48+
buffer = JS_ToCString(ctx, value);
49+
if(strcmp("enabled",buffer)==0) cfg->hardware_flow = feature_t::ENABLED;
50+
else if(strcmp("disabled",buffer)==0) cfg->hardware_flow = feature_t::DISABLED;
51+
JS_FreeCString(ctx, buffer);
52+
}
53+
JS_FreeValue(ctx, value);
54+
}
2455

2556
{
2657
JSValue value = JS_GetPropertyStr(ctx, obj, "vtime");
@@ -37,7 +68,7 @@ static void tjs_obj2cfg(JSContext *ctx, JSValue obj, cfg_serial *cfg) {
3768
uint32_t buffer;
3869
if (!JS_IsUndefined(value)){
3970
JS_ToUint32(ctx, &buffer, value);
40-
cfg->vtime = (uint8_t)buffer;
71+
cfg->vmin = (uint8_t)buffer;
4172
}
4273
JS_FreeValue(ctx, value);
4374
}
@@ -50,7 +81,7 @@ static void tjs_obj2cfg(JSContext *ctx, JSValue obj, cfg_serial *cfg) {
5081
}
5182

5283
{
53-
JSValue value = JS_GetPropertyStr(ctx, obj, "ispeed");
84+
JSValue value = JS_GetPropertyStr(ctx, obj, "ospeed");
5485
if (!JS_IsUndefined(value))
5586
JS_ToUint32(ctx, &cfg->ospeed, value);
5687
JS_FreeValue(ctx, value);
@@ -73,7 +104,7 @@ static JSValue tjs___MODULE___configure(JSContext *ctx, JSValue this_val, int ar
73104

74105
tjs_obj2cfg(ctx,argv[1],&cfg);
75106

76-
int fd = __MODULE___configure(name,cfg);
107+
int fd = configure(name,cfg);
77108
if(fd==-1) return JS_ThrowPlainError(ctx, "Unable to open file [%s] as serial", name);
78109
return JS_NewInt32(ctx,fd);
79110
}

src/native/sow.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
#include <sys/file.h>
2222

2323

24-
int __MODULE___configure(const char* path, const cfg_serial& cfg){
24+
namespace __MODULE__{
25+
26+
int configure(const char* path, const cfg_serial& cfg){
2527
printf(
2628
"Opening [%s] with cfg {parity:%i,stop:%i,bits:%i,hardware_flow:%i,vtime:%i,vmin:%i,ispeed:%i,ospeed:%i}\n",
2729
path,
@@ -114,3 +116,5 @@ int __MODULE___configure(const char* path, const cfg_serial& cfg){
114116

115117
return serial_port;
116118
}
119+
120+
}

src/native/sow.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <cstdint>
88

9+
namespace __MODULE__{
910

1011
enum class feature_t : uint8_t {DISABLED, ENABLED};
1112
enum class stop_t : uint8_t {ONE_BIT, TWO_BITS};
@@ -26,5 +27,6 @@ struct cfg_serial {
2627
};
2728

2829

29-
int __MODULE___configure(const char* path, const cfg_serial& cfg);
30+
int configure(const char* path, const cfg_serial& cfg);
3031

32+
}

src/ts/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const speeds_t = t.Union([
1010

1111
const serial_cfg_schema = t.Object({
1212
parity: t.Union([t.Literal("disabled"), t.Literal("enabled")], { default: 'disabled' }),
13+
bits: t.Union([t.Literal(5), t.Literal(6), t.Literal(7), t.Literal(8)], { default: 8 }),
1314
stop: t.Union([t.Literal(1), t.Literal(2)], { default: 1 }),
1415
hardware_flow: t.Union([t.Literal("disabled"), t.Literal("enabled")], { default: 'enabled' }),
1516
vtime: t.Integer({ default: 0 }),

0 commit comments

Comments
 (0)