Skip to content

Commit 12bfb5e

Browse files
authored
Merge pull request #9 from Joe7M/master
Add examples
2 parents 0a74486 + dac78d1 commit 12bfb5e

File tree

4 files changed

+110
-5
lines changed

4 files changed

+110
-5
lines changed

Diff for: ioio/README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ A pin used for digital output. A digital output pin can be used to generate logi
9191

9292
A pin used for PWM (Pulse-Width Modulation) output. A PWM pin produces a logic-level PWM signal. These signals are typically used for simulating analog outputs for controlling the intensity of LEDs, the rotation speed of motors, etc. They are also frequently used for controlling hobby servo motors. PwmOutput instances are obtained by calling IOIO#openPwmOutput. When used for motors and LEDs, a frequency of several KHz is typically used, where there is a trade-off between switching power-loses and smoothness of operation. The pulse width is typically set by specifying the duty cycle, with the setDutyCycle method. A duty cycle of 0 is \"off\", a duty cycle of 1 is \"on\", and every intermediate value produces an intermediate intensity. Please note that any devices consuming more than 20mA of current (e.g. motors) should not by directly connected the the IOIO pins, but rather through an amplification circuit suited for the specific load. When used for hobby servos, the PWM signal is rather used for encoding of the desired angle the motor should go to. By standard, a 100Hz signal is used and the pulse width is varied between 1ms and 2ms (corresponding to both extremes of the shaft angle), using setPulseWidth. The instance is alive since its creation. If the connection with the IOIO drops at any point, the instance transitions to a disconnected state, in which every attempt to use the pin (except close()) will throw a ConnectionLostException. Whenever close() is invoked the instance may no longer be used. Any resources associated with it are freed and can be reused. Typical usage (fading LED):
9393

94-
`io = ioio.openPwmOutput(pin)`
94+
`io = ioio.openPwmOutput(pin, frequency)`
9595

9696
| Name | Description |
9797
|---------|---------------|
@@ -102,10 +102,14 @@ A pin used for PWM (Pulse-Width Modulation) output. A PWM pin produces a logic-l
102102

103103
An interface for controlling a TWI module, in TWI bus-master mode, enabling communication with multiple TWI-enabled slave modules.
104104

105-
`io = ioio.openTwiMaster(pin)`
105+
`io = ioio.openTwiMaster(TWINumber, mode)`
106+
107+
Opens a TWI module number TWINumber in master mode, using its dedicated SDA and SCL pins. The TWI module will run at 100KHz and will use I2C voltage levels if mode = false (pass true for SMBus levels).
106108

107109
| Name | Description |
108110
|---------|---------------|
111+
|void write(Address, Register, DataByte) | Writes a byte of data to the given register of an I2C device with given address.|
112+
|int readwrite(Address, NumReceiveBytes, Register, DataByte) | Writes a byte of data to the given register of an I2C device with given address and reads NumReceiveBytes. NumReceiveBytes can be max 8 bytes long.|
109113

110114
## SpiMaster
111115

Diff for: ioio/samples/bh1750.bas

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
' BH1750 - Ambient light sensor
2+
' =============================
3+
'
4+
' This examample demonstrates how to measure the ambient light luminous flux
5+
' using the BH1750 I2C sensor.
6+
'
7+
' Connect the sensor to the IOIO-OTG board:
8+
'
9+
' ------ ------
10+
' IOIO | |BH1750
11+
' PIN 4|-------|SDA
12+
' PIN 5|-------|SCL
13+
' GND |-------|GND
14+
' 3.3V |-------|VIN
15+
' | |ADDR
16+
'------- ------
17+
18+
' If ADDR is not connected, 0x23 as I2C address will be used.
19+
'
20+
21+
import ioio
22+
23+
const ADDRESS = 0x23
24+
25+
Print "Connect to BH1750"
26+
sensor = ioio.openTwiMaster(0, 0)
27+
ioio.waitForConnect(10)
28+
Print "Connection established"
29+
30+
' Power down
31+
sensor.write(ADDRESS, 0x00)
32+
' Power on
33+
sensor.write(ADDRESS, 0x01)
34+
delay(1000)
35+
36+
' Read one time with low resolution
37+
ValueLowRes = sensor.readwrite(ADDRESS, 2, 0x23) / 1.2
38+
delay(1000)
39+
' Read one time with high resolution
40+
ValueHighRes = sensor.readwrite(ADDRESS, 2, 0x20) / 1.2
41+
42+
print "Low resolution : " + ValueLowRes + " lx"
43+
print "High resolution: " + valueHighRes + " lx"
44+
45+
46+

Diff for: ioio/samples/button.bas

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
' PUSH BUTTON
2+
' ===========
3+
'
4+
' This example demonstrates how to connect a push button
5+
' and read the state of this button. If the button is pressed
6+
' a 0 will be returned otherwise 1
7+
'
8+
' ---------------
9+
' PIN10 o----| Push Button |----o GND
10+
' ---------------
11+
'
12+
' The push button is connected to pin 10 and to GND of the IOIO board.
13+
14+
15+
import ioio
16+
17+
PIN10 = ioio.openDigitalInput(10)
18+
19+
20+
print "Wait for connection to IOIO board"
21+
ioio.waitForConnect(10)
22+
print "Connection established"
23+
print
24+
print "Press button connected to IOIO board or q for quit"
25+
26+
isRunning = true
27+
28+
while(isRunning)
29+
30+
key = inkey()
31+
if(key == "q") then isRunning = false
32+
33+
value = PIN10.read()
34+
35+
locate(6,0): print "Button value is: " + value
36+
37+
delay(50)
38+
39+
wend
40+
41+
print "done"

Diff for: ioio/samples/pwm.bas

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1+
' PWM with a LED
2+
' =============================
3+
'
4+
' This example demonstrates how to use PWN to
5+
' control the brightness of a LED
6+
'
7+
' Connect a LED and a resistor as following:
8+
'
9+
' |\ |
10+
' IOIO | \| -------- IOIO
11+
' PIN 46 ---| |---| R = 1k |--- GND
12+
' | /| --------
13+
' |/ |
14+
115
import ioio
216

3-
led = ioio.openPwmOutput(46, 99)
17+
led = ioio.openPwmOutput(46, 1000)
418

519
print "wait for connect"
620
ioio.waitForConnect(10)
721
print "ready!!!"
822

9-
for DutyCycle = 0 to 1 step 0.1
23+
for DutyCycle = 0 to 1 step 0.01
1024
led.setDutyCycle(DutyCycle)
11-
delay 100
25+
delay 10
1226
next
1327

1428
print "Done"

0 commit comments

Comments
 (0)