Skip to content

Commit

Permalink
Add 'pin' argument to read(), remove listento()
Browse files Browse the repository at this point in the history
Instead of having separate listento and read functions, just
pass the pin to read directly.

Signed-off-by: Keith Packard <keithp@keithp.com>
  • Loading branch information
keith-packard committed May 1, 2019
1 parent c2a1f37 commit 9dbbd9d
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 63 deletions.
32 changes: 12 additions & 20 deletions doc/snek.adoc
Expand Up @@ -686,15 +686,15 @@ Digital pin 13:(((talkto)))(((on)))

[source,subs="verbatim,quotes"]
----
> *talkto(13)*
> *talkto(D13)*
> *on()*
----

Let's get a bit fancier and blink it:(((time.sleep)))

[source,subs="verbatim,quotes"]
----
> *talkto(13)*
> *talkto(D13)*
> *while True:*
+ *onfor(.5)*
+ *time.sleep(.5)*
Expand All @@ -703,14 +703,13 @@ Let's get a bit fancier and blink it:(((time.sleep)))
==== Hooking up a digital input

Find a bit of wire to connect from Digital pin 1 to GND and let's
control the LED with this primitive switch:(((listento)))(((off)))
control the LED with this primitive switch:(((read)))(((off)))

[source,subs="verbatim,quotes"]
----
> *listento(1)*
> *talkto(13)*
> *talkto(D13)*
> *while True:*
+ *if read():*
+ *if read(D1):*
+ *on()*
+ *else:*
+ *off()*
Expand All @@ -726,16 +725,12 @@ Analog pin 0 and make the LED track the sensor:(((read)))(((onfor)))

[source,subs="verbatim,quotes"]
----
> *listento(14)*
> *talkto(13)*
> *talkto(D13)*
> *while True:*
+ *onfor(1-read())*
+ *time.sleep(1-read())*
+ *onfor(1-read(A0))*
+ *time.sleep(1-read(A0))*
----

Note that the Arduino analog pin A0 is called pin 14 in snek (A1 is
pin 15, etc).

==== Controlling motors

So far, we've only talked about using one pin at a time. Arduino motor
Expand Down Expand Up @@ -1922,9 +1917,6 @@ Set the current output pins. If _pin_ is a number, this sets both the
Power and Direction pins. If _pin_ is a List or Tuple, then the first
element sets the Power pin and the second sets the Direction pin.(((talkto)))

=== `listento(` _pin_ `)`
Sets the current input pin to _pin_.(((listento)))

=== `setpower(` _power_ `)`

Sets the power level on the current Power pin to _power_. If the Power
Expand Down Expand Up @@ -1954,11 +1946,11 @@ Turns the current Power pin *off*.(((off)))
Turns the current Power pin *on*, delays for _seconds_ and then
turns the current Power pin *off*.(((onfor)))

=== `read()`
=== `read(` _pin_ `)`

Returns the value of the current Input pin. If this is an analog pin,
then `read()` returns a value from `0 to 1` (inclusive). If this a digital pin, then
`read()` returns either `0` or `1`.(((read)))
Returns the value of _pin_. If this is an analog pin, then `read`
returns a value from `0 to 1` (inclusive). If this a digital pin, then
`read` returns either `0` or `1`.(((read)))

=== `stopall()`

Expand Down
6 changes: 2 additions & 4 deletions examples/snek-car.py
Expand Up @@ -92,16 +92,14 @@ def stop():
# Move forward until we approach something
def go_forw():
forw()
listento(pf)
while read() < .25:
while read(pf) < .25:
pass
stop()

# Move backwards until we approach something
def go_back():
back()
listento(pr)
while read() < .25:
while read(pr) < .25:
pass
stop()

Expand Down
3 changes: 1 addition & 2 deletions examples/track-light.py
Expand Up @@ -17,11 +17,10 @@

def track():
talkto(led)
listento(dial)
setpower(0)
on()
while True:
p = read()
p = read(dial)
if p == 1:
break
setpower(p)
Expand Down
3 changes: 1 addition & 2 deletions samd21/snek-altos.builtin
Expand Up @@ -14,14 +14,13 @@
time.sleep, 1
time.monotonic, 0
talkto, 1
listento, 1
setpower, 1
setleft, 0
setright, 0
onfor, 1
on, 0
off, 0
read, 0
read, 1
stopall, 0
eeprom.write, 0
eeprom.show, -1
Expand Down
26 changes: 9 additions & 17 deletions samd21/snek-gpio.c
Expand Up @@ -18,7 +18,6 @@

static uint8_t power_pin;
static uint8_t dir_pin;
static uint8_t input_pin;
static uint16_t power[AO_SNEK_NUM_PIN];
static uint32_t on_pins;

Expand Down Expand Up @@ -245,18 +244,6 @@ snek_builtin_talkto(snek_poly_t a)
return SNEK_NULL;
}

snek_poly_t
snek_builtin_listento(snek_poly_t a)
{
uint8_t p = snek_poly_get_pin(a);
if (!snek_abort) {
set_dir(p, 0);
input_pin = p;
}
return SNEK_NULL;
}


snek_poly_t
snek_builtin_setpower(snek_poly_t a)
{
Expand Down Expand Up @@ -307,13 +294,18 @@ snek_builtin_onfor(snek_poly_t a)
#define analog_reference 1

snek_poly_t
snek_builtin_read(void)
snek_builtin_read(snek_poly_t a)
{
if (has_adc(input_pin)) {
float value = ao_snek_port_get_analog(input_pin) / (float) AO_ADC_MAX;
uint8_t p = snek_poly_get_pin(a);
if (snek_abort)
return SNEK_NULL;
set_dir(p, 0);

if (has_adc(p)) {
float value = ao_snek_port_get_analog(p) / (float) AO_ADC_MAX;
return snek_float_to_poly(value);
} else {
return snek_bool_to_poly(ao_snek_port_get(input_pin));
return snek_bool_to_poly(ao_snek_port_get(p));
}
}

Expand Down
3 changes: 1 addition & 2 deletions snek-duino/snek-duino.builtin
Expand Up @@ -14,14 +14,13 @@
time.sleep, 1
time.monotonic, 0
talkto, 1
listento, 1
setpower, 1
setleft, 0
setright, 0
onfor, 1
on, 0
off, 0
read, 0
read, 1
stopall, 0
eeprom.write, 0
eeprom.show, -1
Expand Down
26 changes: 10 additions & 16 deletions snek-duino/snek-duino.c
Expand Up @@ -264,17 +264,6 @@ snek_builtin_talkto(snek_poly_t a)
return SNEK_NULL;
}

snek_poly_t
snek_builtin_listento(snek_poly_t a)
{
uint8_t p = snek_poly_get_pin(a);
if (!snek_abort) {
set_dir(p, 0);
input_pin = p;
}
return SNEK_NULL;
}

static bool
is_on(uint8_t pin)
{
Expand Down Expand Up @@ -366,11 +355,16 @@ snek_builtin_onfor(snek_poly_t a)
#define analog_reference 1

snek_poly_t
snek_builtin_read(void)
snek_builtin_read(snek_poly_t a)
{
if (input_pin >= 14) {
uint8_t pin = input_pin - 14;
ADMUX = (analog_reference << 6) | (pin & 7);
uint8_t p = snek_poly_get_pin(a);
if (snek_abort)
return SNEK_NULL;
set_dir(p, 0);

if (p >= 14) {
uint8_t pin = p - 14;
ADMUX = (analog_reference << REFS0) | (pin & 7);
ADCSRA |= (1 << ADSC);
while (ADCSRA & (1 << ADSC))
;
Expand All @@ -380,7 +374,7 @@ snek_builtin_read(void)

return snek_float_to_poly(value);
} else {
return snek_bool_to_poly(*pin_reg(input_pin) & bit(input_pin));
return snek_bool_to_poly(*pin_reg(p) & bit(p));
}
}

Expand Down

0 comments on commit 9dbbd9d

Please sign in to comment.