> For the complete documentation index, see [llms.txt](https://sharinghub-eng.kittenbot.hk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sharinghub-eng.kittenbot.hk/airelated/ai-camera-koi-2/micropython-programming/basic-operations.md).

# 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="/files/IuSoA481qhwX31Tiqyte" %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://sharinghub-eng.kittenbot.hk/airelated/ai-camera-koi-2/micropython-programming/basic-operations.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
