[Snek] [PATCH 3/4] Add a dispatch function for read()

Mikhail Gusarov dottedmag at dottedmag.net
Sat Feb 22 15:09:03 PST 2020


---
 ports/ev3/sensors.c   |  3 +--
 ports/ev3/sensors.h   |  6 ++++++
 ports/ev3/snek-main.c | 12 ++++++++++++
 3 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/ports/ev3/sensors.c b/ports/ev3/sensors.c
index 5e04b8f..5fdb8c5 100644
--- a/ports/ev3/sensors.c
+++ b/ports/ev3/sensors.c
@@ -445,9 +445,8 @@ light(int fd)
 }
 
 snek_poly_t
-snek_builtin_read(snek_poly_t port)
+snek_ev3_sensors_read(snek_poly_t port)
 {
-	/* TODO: dispatch read() to actuators if a string is passed */
 	int fd = get_sensor(port);
 	if (fd == -1)
 		return SNEK_NULL;
diff --git a/ports/ev3/sensors.h b/ports/ev3/sensors.h
index 2038a2f..078b62a 100644
--- a/ports/ev3/sensors.h
+++ b/ports/ev3/sensors.h
@@ -14,8 +14,14 @@
 #ifndef _SNEK_EV3_SENSORS_H_
 #define _SNEK_EV3_SENSORS_H_
 
+#include "snek.h"
+
 /* Initializes constants for EV3 light sensor */
 void
 snek_ev3_init_colors(void);
 
+/* read() implementation for sensors */
+snek_poly_t
+snek_ev3_sensors_read(snek_poly_t port);
+
 #endif /* _SNEK_EV3_SENSORS_H_ */
diff --git a/ports/ev3/snek-main.c b/ports/ev3/snek-main.c
index 0d984bd..2d09baf 100644
--- a/ports/ev3/snek-main.c
+++ b/ports/ev3/snek-main.c
@@ -121,3 +121,15 @@ main(int argc, char **argv)
 		printf("\n");
 	return ret ? 0 : 1;
 }
+
+snek_poly_t
+snek_builtin_read(snek_poly_t port)
+{
+	switch (snek_poly_type(port)) {
+	case snek_float:
+		return snek_ev3_sensors_read(port);
+	default:
+		snek_error_type_1(port);
+		return SNEK_NULL;
+	}
+}
-- 
2.24.0



More information about the Snek mailing list