Examples

This page contains a few examples. See more on GitHub.

Speaker

import time
import flipperzero

def play_frequency(frequency: float):
  volume = 0.8

  flipperzero.speaker_start(frequency, volume)

  for _ in range(0, 150):
    volume *= 0.9945679

    flipperzero.speaker_set_volume(volume)

    time.sleep_ms(1)

  flipperzero.speaker_stop()

play_frequency(100.0)
play_frequency(200.0)
play_frequency(300.0)
play_frequency(500.0)
play_frequency(800.0)
play_frequency(1300.0)
play_frequency(2100.0)
play_frequency(3400.0)

For details, see the Speaker section on the Reference page.

Input

import time
import flipperzero

@flipperzero.on_input
def on_input(button, type):
  flipperzero.canvas_clear()
  flipperzero.canvas_set_color(flipperzero.CANVAS_BLACK)
  flipperzero.canvas_set_text(64, 32, '{button} - {type}'.format(button=button, type=type))
  flipperzero.canvas_update()

for _ in range(1,1000):
  time.sleep_ms(10)

For details, see the Input section on the Reference page.

Interrupts

import flipperzero as f0
import time

# init pins
f0.gpio_init_pin(f0.GPIO_PIN_PA7, f0.GPIO_MODE_OUTPUT_PUSH_PULL)
f0.gpio_init_pin(f0.GPIO_PIN_PC0, f0.GPIO_MODE_INTERRUPT_RISE, f0.GPIO_PULL_UP, f0.GPIO_SPEED_VERY_HIGH)
f0.gpio_init_pin(f0.GPIO_PIN_PC1, f0.GPIO_MODE_INTERRUPT_RISE, f0.GPIO_PULL_UP, f0.GPIO_SPEED_VERY_HIGH)

@f0.on_gpio
def on_gpio(pin):
  if pin == f0.GPIO_PIN_PC0:
    f0.gpio_set_pin(f0.GPIO_PIN_PA7, True)
  if pin == f0.GPIO_PIN_PC1:
    f0.gpio_set_pin(f0.GPIO_PIN_PA7, False)

for _ in range(1, 1500):
  time.sleep_ms(10)

# reset pins
f0.gpio_init_pin(f0.GPIO_PIN_PA7, f0.GPIO_MODE_ANALOG)
f0.gpio_init_pin(f0.GPIO_PIN_PC0, f0.GPIO_MODE_ANALOG)
f0.gpio_init_pin(f0.GPIO_PIN_PC1, f0.GPIO_MODE_ANALOG)

This example drives an external LED upon interrupts: A rising edge on C0 sets the pin A7 to high, a rising edge on C1 sets the pin A7 to low. The following schematic circuit diagram shows the hardware setup for this example:

_images/gpio_interrupt_circuit.svg

Hardware setup for the GPIO interrupt example.

For details, see the GPIO section on the Reference page.

ADC

import flipperzero as f0
import time

f0.gpio_init_pin(f0.GPIO_PIN_PC1, f0.GPIO_MODE_ANALOG)

for _ in range(1,1000):
  raw_value = f0.adc_read_pin_value(f0.GPIO_PIN_PC1)
  raw_voltage = f0.adc_read_pin_voltage(f0.GPIO_PIN_PC1)
  
  value = '{value} #'.format(value=raw_value)
  voltage = '{value} mV'.format(value=raw_voltage)

  f0.canvas_clear()

  f0.canvas_set_text(10, 32, value)
  f0.canvas_set_text(70, 32, voltage)

  f0.canvas_update()

  time.sleep_ms(10)

This example uses a voltage divider with the 3.3 V source from pin 9. The switch S1 changes the input voltage on C1 between 0 and about 0.8 V.

_images/adc_circuit.svg

Hardware setup for the ADC example.

For details, see the ADC section on the Reference page.

PWM

import flipperzero as f0
import time

f0.pwm_start(f0.GPIO_PIN_PA7, 4, 50)
time.sleep(3)

f0.pwm_start(f0.GPIO_PIN_PA7, 1, 25)
time.sleep(3)

This example drives an LED connected to pin A7 and GND using a PWM signal with two different frequency and duty cycle settings.

_images/pwm_circuit.svg

Hardware setup for the PWM example.

For details, see the PWM section on the Reference page.

Infrared

import flipperzero as f0
import time

# receive IR signal
signal = f0.infrared_receive()

time.sleep(3)

# transmit received signal
f0.infrared_transmit(signal)

For details, see the Infrared section on the Reference page.