Ardpy examples

연구/Ardpy 2016. 10. 23. 09:41

Ardpy : Arduino controlled by python

1. first example


 The Arduino function can be called from python shell very easily like this.


python3

arduino program

>>> from ardpy import *


>>> ard = Ardpy(0x10) #make  Ardpy object


>>> ard._exec_func(0) # ledon() call

>>> ard._exec_func(1) # ledoff() call

#include <Wire.h>

#include <Ardpy.h>


void ledon() {

   digitalWrite(13, HIGH);

}


void ledoff() {

   digitalWrite(13, LOW);

}


void setup() {

   pinMode(13, OUTPUT);

   Ardpy.add_func(ledon);// function 0

   Ardpy.add_func(ledoff);// funciton 1

   Ardpy.begin(0x10);

}


void loop() {

}


In the Arduino program (right side), the two functions ledon() and ledoff() are registered by Ardpy.add_func(). Firstly registered function has number 0 and secondly registered one has number 1.


 In the python shell, you can simply create Ardpy object and by using the method _exec_func() the arduino functions called. That is, _exec_func(0) executes ledon() Arduino function, and _exec_func(1) executes ledoff().


 The registered Arduino function using Ardpy.add_func() must be void(input)-void(output) function. How to transmit input arguments and how to get returned value are explained in the next example.

2. second example

 Using _set_arg() method in Ardpy class, you can transmit arguments to the Arduino function. In the Arduino side, Ardpy class has methods to receive the arguments from python. (for example get_byte(), get_int(), etc.). Ardpy class of Arduino side also has the method set_ret() that transmits an obtained (or calculated) value to the python side. The following example is for calling ananlogRead() function of the Arduino board using python script.


python3

Arduino program

>>> from ardpy import *


>>> ard = Ardpy(0x10)


>>> ard._set_arg(0)  # set pin as 0(A0)

>>> ret0 = ard._exec_func(0) # aread()


>>> ard._set_arg(3)  # set pin as 3(A3)

>>> ret1 = ard._exec_func(0) # aread()


#include <Wire.h>

#include <Ardpy.h>


void aread() {

   byte pin = Ardpy.get_byte();

   int ret = analogRead(pin);

   Ardpy.set_ret(ret);

}


void setup() {

   Ardpy.add_func(aread);// function 0

   Ardpy.begin(0x10);

}


void loop() {

}


The ADC value (ret variable in aread() function in right hand side) of Arduino is sent to the python by set_ret() method. The return value of _exec_func() python method is the ADC value received from Arduino. Thus variable ret0 and ret1 (left hand side) is the resulting ADC values (ret variable in aread() function) of Arduino. (right hand side)


 The python method _set_arg() can transmit various data types. Refer to API page.



'연구 > Ardpy' 카테고리의 다른 글

python3-smbus 와 아두이노간 통신 동작  (0) 2016.10.25
Ardpy API reference  (0) 2016.10.23
Ardpy setup  (0) 2016.10.23
About Ardpy  (0) 2016.10.23
ardpy 개발 동기  (0) 2016.05.02
Posted by 살레시오
,