# Basic Operations

## Import KOI 2 Library

```python
from future import *
from koi2 import KOI2
```

## KOI Serial Initialization

```python
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

```python
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

```python
koi.direction(dir)
```

Sets the orientation for the camera.

Parameters:

dir: 0 for Front Facing, 2 for Back Facing

## KOI Data Update

```python
koi.read_from_uart()
```

Reads data from the KOI.

## KOI Buttons

```python
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)
```

{% file src="<https://686851495-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7Bv9xBdKh3R9w6Vp7asd%2Fuploads%2FAQ7ZApztDBrBingZwy8m%2Fbasicoperations1.py?alt=media&token=5a900dcd-3f5e-4561-8085-4d6ec5137120>" %}
