Skip to content

Commit 9cd4252

Browse files
committed
math, advanced & characters
1 parent 3b942d1 commit 9cd4252

File tree

1 file changed

+318
-12
lines changed

1 file changed

+318
-12
lines changed

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

Lines changed: 318 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -358,77 +358,383 @@ rightmost_bit = variable & 1
358358
print("Rigthmost bit: ", rightmost_bit)
359359
```
360360

361-
<!-- Section not started yet
361+
362362
## Advanced I/O
363363

364364
### noTone()
365+
366+
Stops generating a tone on a specified pin by deinitializing the PWM (Pulse Width Modulation) associated with the given pin.
367+
368+
**Parameters:**
369+
- `pin`: The pin number to stop generating the tone.
370+
371+
**Returns:**
372+
- Nothing
373+
374+
**Example:**
375+
376+
```python
377+
import machine
378+
379+
def noTone(pin):
380+
pwm = machine.PWM(machine.Pin(pin))
381+
pwm.deinit()
382+
```
383+
365384
### pulseIn()
366-
### pulseInLong()
385+
386+
**Example:**
387+
388+
```python
389+
import machine
390+
391+
def pulseIn(pin, level, timeout=1000000):
392+
pulse_start = machine.time_pulse_us(machine.Pin(pin), level, timeout)
393+
pulse_end = machine.time_pulse_us(machine.Pin(pin), not level, timeout)
394+
return pulse_end - pulse_start
395+
```
396+
367397
### shiftIn()
398+
399+
**Example:**
400+
401+
```python
402+
import machine
403+
404+
def shiftIn(dataPin, clockPin, bitOrder=machine.MSBFIRST):
405+
value = 0
406+
for i in range(8):
407+
value |= machine.Pin(dataPin).value() << (7 - i)
408+
machine.Pin(clockPin).value(1)
409+
machine.Pin(clockPin).value(0)
410+
return value
411+
```
412+
368413
### shiftOut()
414+
415+
**Example:**
416+
417+
```python
418+
import machine
419+
420+
def shiftOut(dataPin, clockPin, bitOrder=machine.MSBFIRST, value):
421+
for i in range(8):
422+
mask = 1 << i if bitOrder == machine.LSBFIRST else 1 << (7 - i)
423+
machine.Pin(dataPin).value(bool(value & mask))
424+
machine.Pin(clockPin).value(1)
425+
machine.Pin(clockPin).value(0)
426+
```
427+
369428
### tone()
370429

430+
**Example:**
431+
432+
```python
433+
import machine
434+
435+
def tone(pin, frequency, duration=None):
436+
pwm = machine.PWM(machine.Pin(pin))
437+
pwm.freq(frequency)
438+
if duration is not None:
439+
machine.sleep(duration)
440+
pwm.deinit()
441+
```
442+
371443
## Math
372444

373445
### abs()
446+
447+
**Example:**
448+
449+
```python
450+
result = abs(-5)
451+
```
452+
374453
### constrain()
454+
455+
**Example:**
456+
457+
```python
458+
def constrain(value, lower, upper):
459+
return max(min(value, upper), lower)
460+
461+
result = constrain(10, 0, 5) # Result will be 5
462+
```
463+
464+
375465
### map()
466+
467+
**Example:**
468+
469+
```python
470+
def map(value, in_min, in_max, out_min, out_max):
471+
return (value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
472+
473+
result = map(50, 0, 100, 0, 255)
474+
```
475+
476+
376477
### max()
478+
479+
**Example:**
480+
481+
```python
482+
result = max(5, 10, 3) # Result will be 10
483+
```
484+
377485
### min()
486+
487+
**Example:**
488+
489+
```python
490+
result = min(5, 10, 3) # Result will be 3
491+
```
492+
378493
### pow()
494+
495+
**Example:**
496+
497+
```python
498+
result = pow(2, 3) # Result will be 8
499+
```
500+
379501
### sq()
502+
503+
**Example:**
504+
505+
```python
506+
result = sq(4) # Result will be 16
507+
```
508+
380509
### sqrt()
381510

511+
**Example:**
512+
513+
```python
514+
import math
515+
516+
result = math.sqrt(16) # Result will be 4.0
517+
```
518+
519+
520+
521+
382522
Trigonometry
383523

384524
### cos()
525+
526+
385527
### sin()
528+
529+
**Example:**
530+
531+
```python
532+
import math
533+
534+
# Specify the angle in radians
535+
angle_in_radians = math.radians(30)
536+
537+
# Calculate the sine of the angle
538+
sine_value = math.sin(angle_in_radians)
539+
540+
# Print the result
541+
print(f"The sine of {angle_in_radians} radians is: {sine_value}")
542+
543+
```
544+
386545
### tan()
387546

388-
Characters
547+
**Example:**
548+
549+
```python
550+
import math
551+
552+
# Specify the angle in radians
553+
angle_in_radians = math.radians(60)
554+
555+
# Calculate the tangent of the angle
556+
tangent_value = math.tan(angle_in_radians)
557+
558+
# Print the result
559+
print(f"The tangent of {angle_in_radians} radians is: {tangent_value}")
560+
```
561+
562+
## Characters
389563

390564
### isAlpha()
565+
566+
`char.isalpha()`
567+
568+
Analyse if a single character is alphabetic.
569+
570+
```python
571+
char = 'a'
572+
if char.isalpha():
573+
print(f"{char} is alphabetic.")
574+
else:
575+
print(f"{char} is not alphabetic.")
576+
```
577+
391578
### isAlphaNumeric()
579+
580+
`char.isDigit()` and `char.isAlpha()`
581+
582+
Checks if char is a number **or** an alphabetic character.
583+
584+
**Example:**
585+
586+
```python
587+
# Function to check if a character is alphanumeric
588+
def is_alphanumeric(char):
589+
return char.isalpha() or char.isdigit()
590+
591+
# Example usage
592+
test_char = 'a'
593+
594+
if is_alphanumeric(test_char):
595+
print(f"{test_char} is alphanumeric.")
596+
else:
597+
print(f"{test_char} is not alphanumeric.")
598+
599+
```
600+
392601
### isAscii()
602+
603+
`ord(char) < 128`
604+
605+
Checks if character is an ASCII character by checking if the decimal number of the character presented is over 128. If it is over, it is not an ASCII character.
606+
607+
```python
608+
char = 'Ö'
609+
610+
if 0 <= ord(char) < 128:
611+
print(f"{char} is an ASCII character.")
612+
else:
613+
print(f"{char} is not an ASCII character.")
614+
```
615+
393616
### isControl()
617+
618+
`ord(char) < 32 or ord(char) == 127`
619+
620+
Checks whether character presented is less than 32 or 127, which represents control characters.
621+
622+
```python
623+
char = '\t' # Example: Tab character
624+
625+
if 0 <= ord(char) < 32 or ord(char) == 127:
626+
print(f"{char} is a control character.")
627+
else:
628+
print(f"{char} is not a control character.")
629+
```
630+
394631
### isDigit()
632+
633+
`char.isDigit()`
634+
635+
636+
```python
637+
char = '5'
638+
if char.isdigit():
639+
print(f"{char} is a digit.")
640+
else:
641+
print(f"{char} is not a digit.")
642+
```
643+
395644
### isGraph()
645+
646+
**Example:**
647+
648+
```python
649+
char = 'A'
650+
651+
if char.isprintable() and not char.isspace():
652+
print(f"{char} is a graph character.")
653+
else:
654+
print(f"{char} is not a graph character.")
655+
```
656+
396657
### isHexadecimalDigit()
658+
<!-- TODO -->
659+
397660
### isLowerCase()
661+
<!-- TODO -->
398662
### isPrintable()
663+
<!-- TODO -->
399664
### isPunct()
665+
<!-- TODO -->
666+
400667
### isSpace()
668+
669+
`char.isspace()`
670+
671+
**Example:**
672+
673+
```python
674+
char = ' '
675+
676+
if char.isspace():
677+
print(f"{char} is a space character.")
678+
else:
679+
print(f"{char} is not a space character.")
680+
```
681+
401682
### isUpperCase()
402-
### isWhitespace()
403683

404-
Random Numbers
684+
`char.isupper()`
685+
686+
**Example:**
687+
688+
```python
689+
char = 'A'
690+
691+
if char.isupper():
692+
print(f"{char} is an uppercase letter.")
693+
else:
694+
print(f"{char} is not an uppercase letter.")
695+
```
696+
697+
698+
## Random Numbers
699+
405700

406701
## random()
702+
<!-- TODO -->
407703
## randomSeed()
704+
<!-- TODO -->
705+
## External Interrupts
408706

409707

410-
External Interrupts
411-
412708
### attachInterrupt()
709+
<!-- TODO -->
413710
### detachInterrupt()
711+
<!-- TODO -->
414712
### digitalPinToInterrupt()
713+
<!-- TODO -->
714+
## Interrupts
415715

416-
Interrupts
417716

418717
### interrupts()
718+
<!-- TODO -->
419719
### noInterrupts()
720+
<!-- TODO -->
721+
## Communication
420722

421-
Communication
422723

423724
### Print
725+
<!-- TODO -->
424726
### Serial
727+
<!-- TODO -->
425728
### SPI
729+
<!-- TODO -->
426730
### Stream
731+
<!-- TODO -->
427732
### Wire
733+
<!-- TODO -->
734+
## USB
428735

429-
USB
430736

431737
### Keyboard
738+
<!-- TODO -->
432739
### Mouse
433-
434-
-->
740+
<!-- TODO -->

0 commit comments

Comments
 (0)