编写 C++ 应用
原生应用系统概述
在 Matrix OS 中,原生 (C++) 应用程序将在设备上作为操作系统中的独立线程运行,并可以访问系统 API、设备级低级 API 和任何第三方库。这对性能很有好处,但对可移植性和共享性不太友好。(提示提示)
应用程序代码类似于 Arduino,你将有一个用户定义的 Setup() 函数用于应用程序初始化,一个用户定义的 Loop() 函数作为应用程序代码的无限循环。此外,你还将有一个用户定义的 End() 函数用于应用程序结束,以及一个可以调用以退出应用程序的 Exit() 函数。
可用 API
- Matrix OS C++ API - Matrix OS 提供的 API,它们是最容易使用的,提供最佳的可移植性和内存安全性(据说如此)。
- 设备层 API - 设备层提供的 API。Matrix OS API 很大程度上是对这些 API 的封装,你应该使用 Matrix OS API。对于一些尚未添加到 Matrix OS API 中的特殊设备功能,可能会有一些非标准 API。
- FreeRTOS API - Matrix OS 运行在 FreeRTOS 上,你也可以使用 FreeRTOS API 来创建线程和跨线程通信。
- 你希望使用的任何第三方 C/C++ 库 - 你应该能够将它们包含在应用程序文件夹中并使用它们。
不稳定的 API 和构建系统
由于该项目仍在发展中,我无法保证 API 会保持不变。经常会有很多新用法需要新的 API 和对现有 API 的更改。你的代码会出现问题,但我不知道何时以及如何出现。我会尽可能地保持向后兼容性,但不会试图拯救明显糟糕的设计。
Matrix OS 构建系统可能很快会进行大修,以便更智能地知道要编译什么和不编译什么。你的代码可能需要稍作更改以适应它,但这应该不会很困难,并且会有迁移指南。
示例应用
我们使用 Matrix OS 中的示例应用作为例子来解释应用程序是如何工作的。你可以在 Matrix OS 仓库 找到源代码
头文件
1
2#pragma once
3
4#include "MatrixOS.h"
5#include "applications/Application.h"
6#include "applications/BrightnessControl/BrightnessControl.h"
7
8class ExampleAPP : public Application {
9public:
10static Application_Info info;
11
12void Setup() override;
13void Loop() override;
14void End() override;
15
16
17
18// Wanna make your number and color saves between restarts? Comment out the define below.
19// This macro change the code that will the color variable to a saved variable
20// And replace part of the code to support it
21
22// #define EXAMPLEAPP_SAVED_VAR
23
24#ifndef EXAMPLEAPP_SAVED_VAR
25uint8_t number = 0;
26Color color = Color(0xFFFFFF);
27#else
28CreateSavedVar("Example", number, uint8_t, 0);
29CreateSavedVar("Example", color, Color, Color(0xFFFFFF));
30
31// Namespace (This namespace only applies to this application. So even if two different applications have the same variable name, they won't conflict), variable name (no ""), variable type, default value
32// And then just use the variable as a normal variable. The value will be saved & loaded automatically!
33// However, not all variable type and operator is supported. If that is the case, you have to get the variable via .Get() and .Set()
34// For more, see /os/framework/SavedVariable.h
35#endif
36
37void UIMenu();
38void KeyEventHandler(uint16_t KeyID, KeyInfo* keyInfo);
39void MidiEventHandler(MidiPacket midiPacket);
40};
41
42// Meta data about this application
43inline Application_Info ExampleAPP::info = {
44 .name = "Example",
45 .author = "203 Systems",
46 .color = Color(0xFFFFFF),
47 .version = 1,
48 .visibility = true,
49};
50
51// Register this Application to the OS (Use the class name of your application as the variable)
52REGISTER_APPLICATION(ExampleAPP);
53
详细分解
#include "MatrixOS.h"
这包含了 MatrixOS.h 文件,其中包含了 Matrix OS 提供的所有框架、类型和 API。
#include "applications/Application.h""