Part 2: Flutter Embedded Raspberry Pi: Real-Time GPIO Coding the Switch Service Class
In this tutorial, I guide you step-by-step through a simple custom switch service class. Here are the top sections of the video:
1. Import necessary packages:
o Importing dart:async for asynchronous programming.
o Importing dart_periphery for GPIO handling.
o Importing flutter/foundation.dart for debugging.
2. Define the SwitchService class:
o The class that encapsulates the logic for handling the switch connected to a GPIO pin.
3. Define class properties:
o Properties required for GPIO pin, polling interval, debounce interval, and stream controller for switch state.
o _isPolling to track the polling state.
o _gpio to manage the GPIO pin.
4. Constructor to initialize the SwitchService:
o Initializes the GPIO pin and sets default values for polling and debounce intervals.
5. Stream to expose switch state changes:
o Provides a stream that can be listened to for switch state changes.
6. Method to start polling the switch state:
o Starts a periodic timer to poll the switch state.
o Debounces the read values to avoid false state changes.
7. Method to stop polling the switch state:
o Stops the polling by setting _isPolling to false.
8. Method to clean up resources:
o Disposes of the stream controller and GPIO resources when they are no longer needed.
Dart Periphery: [ Ссылка ]
Project repository: [ Ссылка ]
Timeline
00:00 to 00:17 Intro
00:18 to 00:25 in terminal, flutter pub add dart_periphery and then flutter pub get
00:26 to 00:44 Intro cont.
00:45 to 01:02 Import necessary packages
01:03 to 01:34 Define the SwitchService class and class properties
01:35 to 02:30 Constructor to initialize the SwitchService
02:31 to 02:54 to Stream to expose switch state changes
02:55 to 03:39 Method to start polling the switch state
03:40 to 04:28 Method to stop polling the switch state and the dispose method
04:29 to 04:34 Repo location
04:35 to 04:52 Ending
Dart Periphery: [ Ссылка ]
Project repository: [ Ссылка ]
Timeline
00:00 to 00:17 Intro
00:18 to 00:25 in terminal, flutter pub add dart_periphery and then flutter pub get
00:26 to 00:44 Intro cont.
00:45 to 01:02 Import necessary packages
01:03 to 01:34 Define the SwitchService class and class properties
01:35 to 02:30 Constructor to initialize the SwitchService
02:31 to 02:54 to Stream to expose switch state changes
02:55 to 03:39 Method to start polling the switch state
03:40 to 04:28 Method to stop polling the switch state and the dispose method
04:29 to 04:34 Repo location
04:35 to 04:52 Ending
![](https://i.ytimg.com/vi/NGyjuZOuUDo/maxresdefault.jpg)