Basic Operations

Import KOI 2 Library

from future import *
from koi2 import KOI2

KOI Serial Initialization

koi = KOI2(tx, rx)

Initializes the serial connection to KOI.

Parameters:

tx: TX Pin, use P2 for Robotbit EDU

rx: RX Pin, use P12 for Robotbit EDU

KOI Switch Mode

koi.setModel(mode)

Selects the AI mode for KOI.

Parameters:

mode:

mode(decimal number)Function

0

None Mode

1

Built-in Road Sign Tracking

2

Built-in Item Tracking

3

Custom Tracking Model

4

Built-in Number Tracking

5

Image Classifier

6

Built-in Alphabets Tracking

7

Face Mask Detection

9

Face Attributes Detection

10

Color Blob Detection

32

Line Trace Detection

256

Code Scanner

KOI Camera Orientation

koi.direction(dir)

Sets the orientation for the camera.

Parameters:

dir: 0 for Front Facing, 2 for Back Facing

KOI Data Update

koi.read_from_uart()

Reads data from the KOI.

KOI Buttons

koi.getBtnState(btn)

Returns 0 for unpressed, 1 for pressed.

Parameters:

btn: A string value, 'A' for button A, 'B' for button B.

KOI Record Voice Clip

koi.recorded('/sd/'+name,duration)

Records a voice clip and save file onto SD Card.

Parameters:

name: File name as a string, must end in '.wav'

duration: Length in seconds, typically 3 seconds

KOI Play Voice Clip

koi.playAudio('/sd/'+name)

Plays a voice clip from SD Card.

Parameters:

name: File name as a string, must end in '.wav'

KOI Save Picture

koi.takePic(location+name)

Saves a picture.

Parameters:

location: Location of file as a string, '/sd/' for saving on SD Card, '/flash/' for saving onto KOI onboard memory

name: File name as a string, must end in '.jpg'

KOI Show Picture

koi.displayPic(location+name,duration)

Displays a picture.

Parameters:

location: Location of file as a string, '/sd/' for saving on SD Card, '/flash/' for saving onto KOI onboard memory

name: File name as a string, must end in '.jpg'

duration: Duration in micro seconds.

KOI Show String

koi.displayText(x,y,duration,color(r,g,b),text)

Shows a string.

Parameters:

x: X coordinates of the text

y: Y coordinates of the text

duration: Duration in micro seconds.

color: Color of the text in RGB

text: The text to be shown

Sample Program

from future import *
from koi2 import KOI2

koi = KOI2('P2', 'P12')
while True:
  koi.read_from_uart()
  if sensor.btnValue('a'):
    koi.displayText(0+40,0,3*1000,(255, 255, 255),'hello')
  if koi.getBtnState('A'):
    koi.takePic('/flash/'+'abc.jpg')
  if koi.getBtnState('B'):
    koi.displayPic('/flash/'+'abc.jpg',3*1000)

Last updated