raspi_gpio 0.0.1 copy "raspi_gpio: ^0.0.1" to clipboard
raspi_gpio: ^0.0.1 copied to clipboard

PlatformAndroid

A Flutter plugin for controlling Raspberry Pi GPIO pins, supporting LED control, motor control, and button input.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:raspi_gpio/raspi_gpio.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Raspi GPIO Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool _ledState = false;
  bool _motorDirection = true;
  int _motorSpeed = 50;
  bool _buttonState = false;

  @override
  void initState() {
    super.initState();
    _initializeGpio();
    _setupButtonListener();
  }

  Future<void> _initializeGpio() async {
    try {
      await RaspiGpio.initialize();
    } catch (e) {
      debugPrint('Error initializing GPIO: $e');
    }
  }

  void _setupButtonListener() {
    RaspiGpio.listenToButton(17).listen((state) {
      setState(() {
        _buttonState = state;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Raspi GPIO Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  children: [
                    const Text('LED Control'),
                    Switch(
                      value: _ledState,
                      onChanged: (value) async {
                        try {
                          await RaspiGpio.setLedState(18, value);
                          setState(() {
                            _ledState = value;
                          });
                        } catch (e) {
                          debugPrint('Error controlling LED: $e');
                        }
                      },
                    ),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 16),
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  children: [
                    const Text('Motor Control'),
                    Row(
                      children: [
                        const Text('Direction:'),
                        Switch(
                          value: _motorDirection,
                          onChanged: (value) {
                            setState(() {
                              _motorDirection = value;
                            });
                            _updateMotor();
                          },
                        ),
                      ],
                    ),
                    Slider(
                      value: _motorSpeed.toDouble(),
                      min: 0,
                      max: 100,
                      divisions: 100,
                      label: _motorSpeed.toString(),
                      onChanged: (value) {
                        setState(() {
                          _motorSpeed = value.round();
                        });
                        _updateMotor();
                      },
                    ),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 16),
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  children: [
                    const Text('Button State'),
                    Text('Button is ${_buttonState ? "pressed" : "released"}'),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Future<void> _updateMotor() async {
    try {
      await RaspiGpio.controlMotor(
        in1Pin: 22,
        in2Pin: 23,
        enPin: 24,
        speed: _motorSpeed,
        direction: _motorDirection,
      );
    } catch (e) {
      debugPrint('Error controlling motor: $e');
    }
  }
}
2
likes
160
points
24
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin for controlling Raspberry Pi GPIO pins, supporting LED control, motor control, and button input.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on raspi_gpio