Flutter微控制器通信插件flutter_mcu_api的使用

Flutter微控制器通信插件flutter_mcu_api的使用

特性

  • 简单的身份验证,TS值在拦截器中基于API密钥计算
  • 所有端点及其关系都在端点对象中管理
  • 可以轻松通过map传递查询参数
  • 所有现有的图像变体都作为枚举可用

开始使用

首先,您需要API密钥,您可以在开发者页面获取它们。 请注意并考虑Marvel API的使用条款。

使用方法

获取角色列表

// 准备
McuApi api = McuApi(
    publicApiKey: '您的公共API密钥',
    privateApiKey: '您的私有API密钥'
);

// 获取前20个角色
final ApiResponse<CharacterDataContainer> response = await api.characters.fetch();

// 使用查询参数获取接下来的20个角色
final ApiResponse<CharacterDataContainer> response = await api.characters.fetch(args: { 'offset': 20 });

// 根据ID获取一个角色
final ApiResponse<CharacterDataContainer> response = await api.characters.fetch(id: 4711);

获取角色关联

// 通过角色获取漫画列表
final ApiResponse<CharacterDataContainer> response = await api.characters.comics(4711);

// 从角色获取事件列表
final ApiResponse<CharacterDataContainer> response = await api.characters.events(4711);

获取图像变体

final imageUrl = response.thumbnail?.getImageVariant(ImageSizes.standard_large);

额外信息

建议查看Marvel API的使用指南和交互式文档。


---

```dart
// example/lib/main.dart

import 'package:example/pages/character_page.dart';
import 'package:example/pages/comic_page.dart';
import 'package:example/pages/creator_page.dart';
import 'package:example/pages/events_page.dart';
import 'package:example/pages/series_page.dart';
import 'package:example/pages/stories_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mcu_api/flutter_mcu_api.dart';

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

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

  // 这个小部件是你的应用的根节点。
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MCU Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  [@override](/user/override)
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int currentViewIndex = 0;

  late McuApi _mcuApi;
  // 设置您的API密钥
  final String publicApiKey = const String.fromEnvironment('publicKey');
  final String privateApiKey = const String.fromEnvironment('privateKey');

  _HomePageState();

  [@override](/user/override)
  void initState() {
    super.initState();
    _mcuApi = McuApi(
      publicApiKey: publicApiKey, // 使用您自己的公共密钥
      privateApiKey: privateApiKey, // 使用您自己的私有密钥
    );
  }

  void setIndex(int index) {
    setState(() {
      currentViewIndex = index;
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    final textTheme = Theme.of(context).textTheme;
    return Scaffold(
      appBar: AppBar(
        title: const Text('MCU英雄'),
      ),
      drawer: Drawer(
        child: ListView(
          padding: const EdgeInsets.all(0.0),
          children: [
            DrawerHeader(
              decoration: const BoxDecoration(
                color: Colors.blue,
              ),
              child: Center(
                  child: Text(
                'MCU API Demo',
                style: textTheme.headlineSmall,
              )),
            ),
            ListTile(
              title: const Text('角色'),
              onTap: () {
                setIndex(0);
                Navigator.pop(context);
              },
            ),
            ListTile(
              title: const Text('漫画'),
              onTap: () {
                setIndex(1);
                Navigator.pop(context);
              },
            ),
            ListTile(
              title: const Text('创作者'),
              onTap: () {
                setIndex(2);
                Navigator.pop(context);
              },
            ),
            ListTile(
              title: const Text('事件'),
              onTap: () {
                setIndex(3);
                Navigator.pop(context);
              },
            ),
            ListTile(
              title: const Text('系列'),
              onTap: () {
                setIndex(4);
                Navigator.pop(context);
              },
            ),
            ListTile(
              title: const Text('故事'),
              onTap: () {
                setIndex(5);
                Navigator.pop(context);
              },
            ),
          ],
        ),
      ),
      body: IndexedStack(
        index: currentViewIndex,
        children: [
          CharacterPage(
            mcuApi: _mcuApi,
          ),
          ComicPage(mcuApi: _mcuApi),
          CreatorPage(mcuApi: _mcuApi),
          EventsPage(mcuApi: _mcuApi),
          SeriesPage(mcuApi: _mcuApi),
          StoriesPage(mcuApi: _mcuApi),
        ],
      ),
    );
  }
}

更多关于Flutter微控制器通信插件flutter_mcu_api的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter微控制器通信插件flutter_mcu_api的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


flutter_mcu_api 是一个用于在 Flutter 应用中与微控制器(MCU)进行通信的插件。它提供了一种简单的方式来与 MCU 进行数据交换,通常通过串口通信、蓝牙、Wi-Fi 或其他通信协议。

安装

首先,你需要在 pubspec.yaml 文件中添加 flutter_mcu_api 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_mcu_api: ^版本号

然后运行 flutter pub get 来安装插件。

基本用法

  1. 导入插件

    在你的 Dart 文件中导入 flutter_mcu_api

    import 'package:flutter_mcu_api/flutter_mcu_api.dart';
    
  2. 初始化通信

    根据你的通信方式(如串口、蓝牙等),初始化通信通道。例如,使用串口通信:

    final mcuApi = McuApi();
    await mcuApi.initSerialPort(portName: '/dev/ttyUSB0', baudRate: 9600);
    
  3. 发送数据到 MCU

    使用 sendData 方法发送数据到 MCU:

    await mcuApi.sendData(Uint8List.fromList([0x01, 0x02, 0x03]));
    
  4. 接收来自 MCU 的数据

    使用 onDataReceived 回调来接收来自 MCU 的数据:

    mcuApi.onDataReceived.listen((data) {
      print('Received data: $data');
    });
    
  5. 关闭通信

    在不需要通信时,关闭通信通道:

    await mcuApi.close();
    

示例代码

以下是一个简单的示例,展示了如何使用 flutter_mcu_api 与 MCU 进行通信:

import 'package:flutter/material.dart';
import 'package:flutter_mcu_api/flutter_mcu_api.dart';
import 'dart:typed_data';

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: McuCommunicationDemo(),
    );
  }
}

class McuCommunicationDemo extends StatefulWidget {
  [@override](/user/override)
  _McuCommunicationDemoState createState() => _McuCommunicationDemoState();
}

class _McuCommunicationDemoState extends State<McuCommunicationDemo> {
  final mcuApi = McuApi();

  [@override](/user/override)
  void initState() {
    super.initState();
    _initMcuCommunication();
  }

  Future<void> _initMcuCommunication() async {
    await mcuApi.initSerialPort(portName: '/dev/ttyUSB0', baudRate: 9600);
    mcuApi.onDataReceived.listen((data) {
      print('Received data: $data');
    });
  }

  Future<void> _sendData() async {
    await mcuApi.sendData(Uint8List.fromList([0x01, 0x02, 0x03]));
  }

  [@override](/user/override)
  void dispose() {
    mcuApi.close();
    super.dispose();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('MCU Communication Demo'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: _sendData,
          child: Text('Send Data to MCU'),
        ),
      ),
    );
  }
}
回到顶部