diff --git a/doc/snek.adoc b/doc/snek.adoc index f8309ae..f92753d 100644 --- a/doc/snek.adoc +++ b/doc/snek.adoc @@ -686,7 +686,7 @@ Digital pin 13:(((talkto)))(((on))) [source,subs="verbatim,quotes"] ---- -> *talkto(13)* +> *talkto(D13)* > *on()* ---- @@ -694,7 +694,7 @@ 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)* @@ -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()* @@ -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 @@ -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 @@ -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()` diff --git a/examples/snek-car.py b/examples/snek-car.py index d69d4e9..049aa57 100644 --- a/examples/snek-car.py +++ b/examples/snek-car.py @@ -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() diff --git a/examples/track-light.py b/examples/track-light.py index 91fc5cc..87b1e3f 100644 --- a/examples/track-light.py +++ b/examples/track-light.py @@ -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) diff --git a/samd21/snek-altos.builtin b/samd21/snek-altos.builtin index fd1e59f..08d27d5 100644 --- a/samd21/snek-altos.builtin +++ b/samd21/snek-altos.builtin @@ -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 diff --git a/samd21/snek-gpio.c b/samd21/snek-gpio.c index fc53346..e4ce5d1 100644 --- a/samd21/snek-gpio.c +++ b/samd21/snek-gpio.c @@ -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; @@ -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) { @@ -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)); } } diff --git a/snek-duino/snek-duino.builtin b/snek-duino/snek-duino.builtin index 8446d0f..40cbb27 100644 --- a/snek-duino/snek-duino.builtin +++ b/snek-duino/snek-duino.builtin @@ -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 diff --git a/snek-duino/snek-duino.c b/snek-duino/snek-duino.c index e799b51..05c6ef3 100644 --- a/snek-duino/snek-duino.c +++ b/snek-duino/snek-duino.c @@ -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) { @@ -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)) ; @@ -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)); } }