# CodyNick Python Quick Reference Handbook

<h2 style="font-size: 1.35em; font-weight: bold; color:#16a34a;">Start Code</h2>

```python
import CodyNick

cn = CodyNick.CN()
```

`cn` is the connected CodyNick device object. Pass it as the first argument to CodyNick hardware functions.

<h2 style="font-size: 1.35em; font-weight: bold; color:#2563eb;">RGB LED Matrix</h2>

### Set One LED By Number

```python
CodyNick.RGB_Matrix.set(cn, led, color)
```

| Parameter | Description |
|---|---|
| `cn` | Connected CodyNick device |
| `led` | LED number from `0` to `15` |
| `color` | Color code `0..7`, RGB hex string, or RGB percentage list |

Accepted `color` formats:

```python
0
"#00FFFF"
[0, 100, 100]
```

Examples:

```python
CodyNick.RGB_Matrix.set(cn, 15, 0)
CodyNick.RGB_Matrix.set(cn, 15, "#00FFFF")
CodyNick.RGB_Matrix.set(cn, 15, [0, 100, 100])
```

### Set One LED By Coordinate

```python
CodyNick.RGB_Matrix.set_xy(cn, x, y, color)
```

| Parameter | Description |
|---|---|
| `cn` | Connected CodyNick device |
| `x` | Horizontal coordinate from `0` to `3` |
| `y` | Vertical coordinate from `0` to `3` |
| `color` | Color code `0..7`, RGB hex string, or RGB percentage list |

Coordinate system:

```text
(0,3)  (1,3)  (2,3)  (3,3)
(0,2)  (1,2)  (2,2)  (3,2)
(0,1)  (1,1)  (2,1)  (3,1)
(0,0)  (1,0)  (2,0)  (3,0)
```

Example:

```python
CodyNick.RGB_Matrix.set_xy(cn, 0, 0, "#FF0000")
```

### Clear All RGB LEDs

```python
CodyNick.RGB_Matrix.clear(cn)
```

Turns off all RGB LEDs.

### Built-In Color Codes

| Code | Name | Hex |
|---:|---|---|
| 0 | Red | `#FF0000` |
| 1 | Cyan | `#00FFFF` |
| 2 | Blue | `#0000FF` |
| 3 | Yellow | `#FFFF00` |
| 4 | Magenta | `#FF00FF` |
| 5 | Green | `#00FF00` |
| 6 | Orange | `#FF8000` |
| 7 | White | `#FFFFFF` |

<h2 style="font-size: 1.35em; font-weight: bold; color:#d97706;">Joystick</h2>

### Check One Direction

```python
CodyNick.Joystick.position(cn, direction)
```

| Parameter | Description |
|---|---|
| `cn` | Connected CodyNick device |
| `direction` | `"up"`, `"down"`, `"left"`, or `"right"` |

Returns `True` or `False`.

Example:

```python
if CodyNick.Joystick.position(cn, "up"):
    print("up")
```

### Check Button Click

```python
CodyNick.Joystick.click(cn)
```

Returns `True` when the joystick button is pressed.

### Read All Current States

```python
CodyNick.Joystick.states(cn)
```

Returns a list such as:

```python
[]
["UP"]
["LEFT"]
["CLICK"]
["UP", "CLICK"]
```

Example:

```python
states = CodyNick.Joystick.states(cn)
```

<h2 style="font-size: 1.35em; font-weight: bold; color:#16a34a;">CJP Sound Maker</h2>

### Play A Note And Continue

```python
CodyNick.CJP_Sound_Maker.play(cn, note, duration_ms)
```

| Parameter | Description |
|---|---|
| `cn` | Connected CodyNick device |
| `note` | Musical note from `B0` to `D#8`, such as `"C4"`, `"F#2"`, or `"Gb2"` |
| `duration_ms` | Duration in milliseconds |

Example:

```python
CodyNick.CJP_Sound_Maker.play(cn, "F#2", 300)
```

### Play A Note And Wait Until Done

```python
CodyNick.CJP_Sound_Maker.play_until_done(cn, note, duration_ms)
```

Same parameters as `play()`, but Python waits until the note is finished.

Example:

```python
CodyNick.CJP_Sound_Maker.play_until_done(cn, "C4", 500)
```

<h2 style="font-size: 1.35em; font-weight: bold; color:#2563eb;">Seven Segment Display</h2>

### Display A Number

```python
CodyNick.Seven_Segment.display(cn, value)
```

| Parameter | Description |
|---|---|
| `cn` | Connected CodyNick device |
| `value` | Number or numeric string to show |

Examples:

```python
CodyNick.Seven_Segment.display(cn, 1234)
CodyNick.Seven_Segment.display(cn, -123)
CodyNick.Seven_Segment.display(cn, 1.234)
```

<h2 style="font-size: 1.35em; font-weight: bold; color:#d97706;">Motion Detection</h2>

### Detect Motion

```python
CodyNick.Motion_Detection.detect(cn)
```

| Parameter | Description |
|---|---|
| `cn` | Connected CodyNick device |

Returns:

```python
True
False
```

Example:

```python
if CodyNick.Motion_Detection.detect(cn):
    print("motion detected")
```

<h2 style="font-size: 1.35em; font-weight: bold; color:#16a34a;">Connection Control</h2>

### Close The Connection

```python
cn.close()
```

Closes the serial connection to the CodyNick device.

<div style="border-left: 5px solid #16a34a; background:#f0fdf4; padding: 12px 16px; border-radius: 8px; margin: 14px 0;">
<strong>Quick summary:</strong><br>
Use `RGB_Matrix` for RGB LEDs, `Joystick` for joystick input, `CJP_Sound_Maker` for notes, `Seven_Segment` for numeric output, and `Motion_Detection` for PIR motion sensing.
</div>