把代码复制到mind+中上传模式的手动编辑里(arduino c)就能用了,有BUG欢迎评论。
使用方法:
A/B键左右切换,触摸键O选定,按等于输出结果,C清除。
#include <MPython.h>
#include <SimpleList.h>
// 变量定义
String mind_s_ShuChuXianShi;
volatile int mind_n_GeShu; // 当前选择的位置
SimpleList<String> mind_l_ShuChu; // 输入列表
volatile bool inputProcessed = false; // 防止重复输入
// 函数声明
void onButtonBPressed();
void pin15TouchCallback();
void onButtonAPressed();
void updateDisplay();
void calculateResult();
int getPriority(char op);
void computeTop(float numbers[], int &numTop, char operators[], int &opTop);
void handleInput(int selection);
// 主程序
void setup() {
mPython.begin();
buttonB.setPressedCallback(onButtonBPressed);
touchPadO.setTouchedCallback(pin15TouchCallback);
buttonA.setPressedCallback(onButtonAPressed);
mPython.setTouchThreshold(70);
mind_s_ShuChuXianShi = "";
mind_n_GeShu = 1;
updateDisplay();
}
void loop() {
// 空循环
}
// 更新屏幕显示
void updateDisplay() {
display.fillScreen(0);
display.setCursorLine(1);
display.printLine(" 1 2 3 4 5 6 7 8 9");
display.setCursorLine(2);
display.printLine(" 0 + - * / ( ) = c");
display.rect(4, 35, 120, 23, false);
// 计算并更新选择框位置
int rectX = (mind_n_GeShu <= 9) ? ((mind_n_GeShu - 1) * 13) + 4 : ((mind_n_GeShu - 10) * 12.1) + 4;
display.rect(rectX, 1 + ((mind_n_GeShu > 9) ? 17 : 0), 14, 14, false);
// 显示输入内容
display.setCursor(9, 38);
for (int i = 0; i < mind_l_ShuChu.size(); i++) {
display.print(mind_l_ShuChu[i]);
}
}
// 按钮 B 事件(向右移动选择框)
void onButtonBPressed() {
mind_n_GeShu = (mind_n_GeShu % 18) + 1; // 增加到18,支持更多按钮
updateDisplay();
}
// 按钮 A 事件(向左移动选择框)
void onButtonAPressed() {
mind_n_GeShu = (mind_n_GeShu - 2 + 18) % 18 + 1;
updateDisplay();
}
// 触摸事件(确定输入)
void pin15TouchCallback() {
// 确保每次触摸只处理一次输入
if (inputProcessed) {
return;
}
inputProcessed = true; // 防止重复输入
if (mind_l_ShuChu.size() >= 16) {
inputProcessed = false;
return; // 超过输入限制,直接返回
}
handleInput(mind_n_GeShu);
updateDisplay();
inputProcessed = false; // 处理完输入后允许下一次输入
}
// 处理输入
void handleInput(int selection) {
String lastInput = (mind_l_ShuChu.size() > 0) ? mind_l_ShuChu[mind_l_ShuChu.size() - 1] : "";
// 禁止首个输入为运算符或右括号
if (mind_l_ShuChu.size() == 0 && (selection >= 11 && selection <= 14 || selection == 16)) {
return;
}
// 禁止连续输入运算符
if ((selection == 11 || selection == 12 || selection == 13 || selection == 14) &&
(lastInput == "+" || lastInput == "-" || lastInput == "*" || lastInput == "/")) {
return;
}
// 处理括号
if (selection == 15) { // 左括号
mind_l_ShuChu.push_back("(");
} else if (selection == 16) { // 右括号
mind_l_ShuChu.push_back(")");
} else if (selection >= 1 && selection <= 9) { // 数字
mind_l_ShuChu.push_back(String(selection));
} else if (selection == 10) { // 数字0
mind_l_ShuChu.push_back("0");
} else if (selection == 11) { // 运算符 +
mind_l_ShuChu.push_back("+");
} else if (selection == 12) { // 运算符 -
mind_l_ShuChu.push_back("-");
} else if (selection == 13) { // 运算符 *
mind_l_ShuChu.push_back("*");
} else if (selection == 14) { // 运算符 /
mind_l_ShuChu.push_back("/");
} else if (selection == 17) { // 等号 =
calculateResult();
} else if (selection == 18) { // 清除 c
mind_l_ShuChu.clear();
}
updateDisplay();
}
// 计算表达式结果
void calculateResult() {
if (mind_l_ShuChu.size() == 0) {
mind_l_ShuChu.push_back("错误: 输入为空");
updateDisplay();
return;
}
const int MAX_SIZE = 16;
float numbers[MAX_SIZE]; // 存放数字
char operators[MAX_SIZE]; // 存放运算符
int numTop = -1, opTop = -1; // 栈指针
String expression = "";
for (int i = 0; i < mind_l_ShuChu.size(); i++) {
expression += mind_l_ShuChu[i];
}
// 解析表达式
for (int i = 0; i < expression.length(); i++) {
char c = expression[i];
if (isdigit(c)) {
// 处理数字
numbers[++numTop] = c - '0';
} else if (c == '(') {
// 处理左括号
operators[++opTop] = c;
} else if (c == ')') {
// 处理右括号
while (opTop >= 0 && operators[opTop] != '(') {
computeTop(numbers, numTop, operators, opTop);
}
opTop--; // 弹出左括号
} else if (c == '+' || c == '-' || c == '*' || c == '/') {
// 处理运算符
while (opTop >= 0 && getPriority(operators[opTop]) >= getPriority(c)) {
computeTop(numbers, numTop, operators, opTop);
}
operators[++opTop] = c;
}
}
// 处理栈中剩余的运算符
while (opTop >= 0) {
computeTop(numbers, numTop, operators, opTop);
}
if (numTop < 0) {
mind_l_ShuChu.clear();
mind_l_ShuChu.push_back("错误");
} else {
mind_l_ShuChu.clear();
mind_l_ShuChu.push_back(String(numbers[numTop], 2)); // 显示 2 位小数
}
updateDisplay();
}
// 获取运算符优先级
int getPriority(char op) {
if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
return 0;
}
// 计算栈顶运算
void computeTop(float numbers[], int &numTop, char operators[], int &opTop) {
if (numTop < 1 || opTop < 0) return;
float b = numbers[numTop--];
float a = numbers[numTop--];
char op = operators[opTop--];
float result = 0;
if (op == '+') result = a + b;
else if (op == '-') result = a - b;
else if (op == '*') result = a * b;
else if (op == '/') {
if (b == 0) {
mind_l_ShuChu.clear();
mind_l_ShuChu.push_back("错误");
return;
}
result = a / b;
}
numbers[++numTop] = result; // 更新栈
}
评论