Skip to content

Commit ed7235a

Browse files
committed
data types
1 parent 5c5dd1d commit ed7235a

File tree

1 file changed

+180
-10
lines changed

1 file changed

+180
-10
lines changed

content/micropython/01.basics/08.reference/reference.md

Lines changed: 180 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ For example:
5353
- [pow()](#pow)
5454
- [sq()](#sq)
5555
- [sqrt()](#sqrt)
56+
- [Trigonometry](#trigonometry)
5657
- [cos()](#cos)
5758
- [sin()](#sin)
5859
- [tan()](#tan)
@@ -96,9 +97,18 @@ For example:
9697
- [Wire.write()](#wirewrite)
9798
- [Wire.setClock()](#wiresetclock)
9899
- [SoftI2C](#softi2c)
99-
- [USB](#usb)
100-
- [Keyboard](#keyboard)
101-
- [Mouse](#mouse)
100+
- [USB HID](#usb-hid)
101+
- [Data Types](#data-types)
102+
- [size\_t](#size_t)
103+
- [void](#void)
104+
- [Conversion](#conversion)
105+
- [byte()](#byte)
106+
- [char()](#char)
107+
- [float()](#float)
108+
- [int()](#int)
109+
- [Scope](#scope)
110+
- [Local Variables](#local-variables)
111+
- [Global Variables](#global-variables)
102112

103113
## Digital I/O
104114

@@ -599,16 +609,35 @@ import math
599609
result = math.sqrt(16) # Result will be 4.0
600610
```
601611

612+
## Trigonometry
602613

614+
### cos()
603615

616+
`math.cos(angle_in_radians)`
604617

605-
Trigonometry
618+
Calculates the cosine of an angle (in radians). The result will be between -1 and 1.
606619

607-
### cos()
620+
**Example:**
621+
622+
```python
623+
import math
608624

625+
# Specify the angle in radians
626+
angle_in_radians = math.radians(45)
627+
628+
# Calculate the cosine of the angle
629+
cos_value = math.cos(angle_in_radians)
630+
631+
# Print the result
632+
print(f"The cosine of {angle_in_radians} radians is: {cosine_value}")
633+
```
609634

610635
### sin()
611636

637+
`math.sin(angle_in_radians)`
638+
639+
Calculates the sine of an angle (in radians). The result will be between -1 and 1.
640+
612641
**Example:**
613642

614643
```python
@@ -627,6 +656,10 @@ print(f"The sine of {angle_in_radians} radians is: {sine_value}")
627656

628657
### tan()
629658

659+
`math.tan(angle_in_radians)`
660+
661+
Calculates the tangent of an angle (in radians). The result will be between negative infinity and infinity.
662+
630663
**Example:**
631664

632665
```python
@@ -1216,11 +1249,148 @@ MicroPython has a built in class called `SoftI2C` (as in software I2C). Software
12161249
- `softi2c.start()` - create the start condition for initializing communication over I2C (SDA goes to **LOW** while SCL is **HIGH**).
12171250
- `softi2c.stop()` - create the stop condition for ending communication over I2C (SDA goes to **HIGH** while SCL is **HIGH**).
12181251

1219-
## USB
1252+
## USB HID
1253+
1254+
Human Interface Device (HID) is currently **not supported** on any of the Arduino boards.
1255+
1256+
To use HID functions on Arduino boards that support it (most modern boards), please refer to the Arduino / C++ reference:
1257+
- [Mouse](https://www.arduino.cc/reference/en/language/functions/usb/mouse/)
1258+
- [Keyboard](https://www.arduino.cc/reference/en/language/functions/usb/keyboard/)
1259+
1260+
## Data Types
1261+
1262+
When declaring variables in MicroPython / Python, you do not need to specify the data type, this is done automatically.
1263+
1264+
Examples for variable declaration can be seen in the snippet below:
1265+
1266+
```python
1267+
var_array = [1, 2, 3]
1268+
var_bool = True/False
1269+
var_unsigned_byte = 255
1270+
var_signed_byte = -128
1271+
var_char = 'A'
1272+
var_double = 3.14
1273+
var_float = 29.231232
1274+
var_int = 2147483647
1275+
var_long = 2147483647
1276+
var_short = 32767
1277+
var_string = "This is a string"
1278+
var_unsigned_int = 4294967295
1279+
var_unsigned_long = 4294967295
1280+
```
1281+
1282+
### size_t
1283+
1284+
`len(my_array)`
1285+
1286+
There is no direct equivalent to `size_t`, but using the `len()` function, you can return the size of an object.
1287+
1288+
**Example:**
1289+
1290+
```python
1291+
my_array = [1,2,3,4,5]
1292+
1293+
array_size = len(my_array)
1294+
1295+
print("Size of array is: ", array_size)
1296+
```
1297+
1298+
### void
1299+
1300+
`def my_func():`
1301+
1302+
There is no direct equivalent to `void`, but when defining a function without a return statement, the function is of a "void" type.
1303+
1304+
**Example:**
1305+
1306+
```python
1307+
def my_function():
1308+
print("Function executed, nothing returned though!")
1309+
```
1310+
1311+
## Conversion
1312+
1313+
### byte()
1314+
1315+
`bytes(value)`
1316+
1317+
Converts a value to a byte.
1318+
1319+
**Example:**
1320+
1321+
```python
1322+
my_byte = bytes([255])
1323+
print(my_byte) # prints \xff which in hex is 255
1324+
```
1325+
1326+
### char()
1327+
1328+
`chr(value)`
1329+
1330+
Converts a value to a char
1331+
1332+
1333+
**Example:**
1334+
1335+
```python
1336+
value = 65 # 65 is "A" in ASCII code
1337+
char_value = chr(value) # converts a value to char (65 to "A")
1338+
print("Char value:", char_value) # prints "A"
1339+
```
1340+
1341+
### float()
1342+
1343+
`float(value)`
1344+
1345+
**Example 1:**
1346+
1347+
Converts an integer to float.
1348+
1349+
```python
1350+
value = 25
1351+
float_value = float(value)
1352+
print("Float value:", float_value) # prints 25.0
1353+
```
1354+
1355+
**Example 2:**
1356+
1357+
Converts a string to float.
1358+
1359+
```python
1360+
value = "3.14"
1361+
float_value = float(value)
1362+
print("Float value:", float_value) #prints 3.14
1363+
```
1364+
1365+
### int()
1366+
1367+
`int(value)`
1368+
1369+
Converts a value
1370+
1371+
**Example 1:**
1372+
1373+
Converts a float to int. Rounds the number up/down, e.g. `42.232` = `42`.
1374+
1375+
```python
1376+
value = 42.232
1377+
int_value = int(value)
1378+
print("Int value:", int_value)
1379+
```
1380+
1381+
**Example 2:**
1382+
1383+
Converts a string to int.
1384+
1385+
```python
1386+
value = "42"
1387+
int_value = int(value)
1388+
print("Int value:", int_value)
1389+
```
1390+
1391+
## Scope
12201392

1393+
### Local Variables
12211394

1395+
### Global Variables
12221396

1223-
### Keyboard
1224-
<!-- TODO -->
1225-
### Mouse
1226-
<!-- TODO -->

0 commit comments

Comments
 (0)