Skip to content

Commit 20ee774

Browse files
committed
Added nxt and ev3 touch sensor primitives to the emulator
Instead of always returning 1 or 0 they also use a random 12bit number.
1 parent 7bb496b commit 20ee774

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/Primitives/emulated.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <cmath>
1919
#include <cstdio>
2020
#include <cstring>
21+
#include <random>
2122
#include <thread>
2223

2324
#include "../Memory/mem.h"
@@ -27,7 +28,7 @@
2728
#include "primitives.h"
2829

2930
#define NUM_PRIMITIVES 0
30-
#define NUM_PRIMITIVES_ARDUINO 35
31+
#define NUM_PRIMITIVES_ARDUINO 37
3132

3233
#define ALL_PRIMITIVES (NUM_PRIMITIVES + NUM_PRIMITIVES_ARDUINO)
3334

@@ -558,6 +559,28 @@ def_prim(read_uart_sensor, oneToOneI32) {
558559
return true;
559560
}
560561

562+
std::random_device r;
563+
std::default_random_engine e(r());
564+
std::uniform_int_distribution<int16_t> adc_dist(0, 1<<12); // 12 bit adc
565+
566+
def_prim(nxt_touch_sensor, oneToOneU32) {
567+
const uint32_t port = arg0.uint32;
568+
const int16_t v = adc_dist(e);
569+
pop_args(1);
570+
printf("nxt_touch_sensor(%u) = %d\n", port, v < 2000);
571+
pushUInt32(v < 2000);
572+
return true;
573+
}
574+
575+
def_prim(ev3_touch_sensor, oneToOneU32) {
576+
const uint32_t port = arg0.uint32;
577+
const int16_t v = adc_dist(e);
578+
pop_args(1);
579+
printf("ev3_touch_sensor(%u) = %d\n", port, v > 3000);
580+
pushUInt32(v > 3000);
581+
return true;
582+
}
583+
561584
def_prim(subscribe_interrupt, threeToNoneU32) {
562585
uint8_t pin = arg2.uint32; // GPIOPin
563586
uint8_t tidx = arg1.uint32; // Table Idx pointing to Callback function
@@ -661,6 +684,8 @@ void install_primitives() {
661684
install_primitive(stop_motor);
662685
install_primitive(setup_uart_sensor);
663686
install_primitive(read_uart_sensor);
687+
install_primitive(nxt_touch_sensor);
688+
install_primitive(ev3_touch_sensor);
664689
}
665690

666691
//------------------------------------------------------

0 commit comments

Comments
 (0)