如何使编码器与两个库一起工作? 我首先想说的是,我对C++非常陌生,这就是为什么我在ESP32上使用ARDUINO核心和库,我想为下面要看到的垃圾堆火灾道歉。
只需制作一个带有按钮和编码器的自定义键盘。启动时,选择两种模式之一:blekeyboard或BleMIDI control surface 按钮在两种模式下都工作,但编码器仅在最后声明的模式下工作。(因此在这个脚本顺序中,模式1中的编码器和按钮都工作,而只有按钮在模式2中工作。) 我做错了什么?我能做什么?欢迎对问题或整个脚本提出任何建议 先谢谢你如何使编码器与两个库一起工作? 我首先想说的是,我对C++非常陌生,这就是为什么我在ESP32上使用ARDUINO核心和库,我想为下面要看到的垃圾堆火灾道歉。,c++,arduino,esp32,C++,Arduino,Esp32,只需制作一个带有按钮和编码器的自定义键盘。启动时,选择两种模式之一:blekeyboard或BleMIDI control surface 按钮在两种模式下都工作,但编码器仅在最后声明的模式下工作。(因此在这个脚本顺序中,模式1中的编码器和按钮都工作,而只有按钮在模式2中工作。) 我做错了什么?我能做什么?欢迎对问题或整个脚本提出任何建议 先谢谢你 #include <Arduino.h> #include <BleKeyboard.h> BleKeyboard ble
#include <Arduino.h>
#include <BleKeyboard.h>
BleKeyboard bleKeyboard;
#define ESP32
#include <encoder.h>
#include <Control_Surface.h>
#include <MIDI_Interfaces/BluetoothMIDI_Interface.hpp>
BluetoothMIDI_Interface midi;
const int usermodebutton1 = 2;
const int usermodebutton2 = 0;
int usermode = 0;
// ---------------------- mode 2 MIDI Input Elements ------------------------ //
using namespace MIDI_Notes;
NoteButton csButton1 = {
2,
note(C, 4),
};
CCRotaryEncoder csEnc1 = {
{26, 25}, // pins
MCU::V_POT_1, // MIDI address (CC number + optional channel)
1, // optional multiplier if the control isn't fast enough
};
// -------------------------- mode 1 blekeyboard --------------------------- //
int kbutton1 = 2;
int kbutton1State;
int keyInterval = 400000;
Encoder kencoder1(25, 26);
int encInterval = 5000;
TickType_t currentTime;
TickType_t previousTime;
long enc1_oldPos = -999;
// ============================================================================= //
void setup()
{
pinMode(usermodebutton1, INPUT_PULLUP);
pinMode(usermodebutton2, INPUT_PULLUP);
Serial.begin(115200);
Serial.println("");
Serial.println("select mode:");
// ----------------------------------------------------------------------------- //
while (true)
{
if (digitalRead(usermodebutton1) == LOW)
{
usermode = 1;
Serial.println("mode 1 selected");
break;
}
if (digitalRead(usermodebutton2) == LOW)
{
usermode = 2;
Serial.println("mode 2 selected");
break;
}
delay(1000);
}
// ----------------------------------------------------------------------------- //
if (usermode == 1)
{
Serial.println("setup mode 1");
Serial.println("Starting BLE work...");
bleKeyboard.begin();
pinMode(kbutton1, INPUT_PULLUP);
previousTime = 0;
}
if (usermode == 2)
{
Serial.println("setup mode 2");
Serial.println("Control Surface BLE starting...");
RelativeCCSender::setMode(relativeCCmode::TWOS_COMPLEMENT);
Control_Surface.begin(); // Initialize Control Surface
}
}
// ============================================================================= //
void loop()
{
while (usermode == 1)
{
while (bleKeyboard.isConnected())
{
// mode 1 encoders
long enc1_newPos = kencoder1.read();
currentTime = esp_timer_get_time();
if (enc1_newPos < enc1_oldPos && currentTime - previousTime > encInterval)
{
enc1_oldPos = enc1_newPos;
previousTime = currentTime;
// bleKeyboard.write(KEY_MEDIA_VOLUME_DOWN);
Serial.print("enc1: ");
Serial.println(enc1_newPos);
}
if (enc1_newPos > enc1_oldPos && currentTime - previousTime > encInterval)
{
enc1_oldPos = enc1_newPos;
previousTime = currentTime;
// bleKeyboard.write(KEY_MEDIA_VOLUME_UP);
Serial.print("enc1: ");
Serial.println(enc1_newPos);
}
// mode 1 keys
kbutton1State = digitalRead(kbutton1);
if (kbutton1State == LOW && currentTime - previousTime > keyInterval)
{
previousTime = currentTime;
Serial.println("button 1 pressed");
bleKeyboard.print("1");
}
}
}
while (usermode == 2)
{
Control_Surface.loop(); // Refresh all elements
}
}
#包括
#包括
键盘,键盘;
#定义ESP32
#包括
#包括
#包括
蓝牙midi_接口midi;
const int usermodebutton1=2;
const int usermodebutton2=0;
int usermode=0;
//--------------------------模式2 MIDI输入元素-----------------//
使用名称空间MIDI_注释;
NoteButton csButton1={
2.
注(C,4),
};
CCrotary编码器csEnc1={
{26,25},//引脚
MCU::V_POT_1,//MIDI地址(抄送号码+可选通道)
1,//如果控件速度不够快,则可选乘法器
};
//------------------------------模式1键盘-----------------//
int kbutton1=2;
int-kbutton1State;
int keyInterval=400000;
编码器kencoder1(25,26);
int encInterval=5000;
勾选当前时间类型;
勾选上一次的类型;
长enc1_oldPos=-999;
// ============================================================================= //
无效设置()
{
pinMode(用户模式按钮1,输入\上拉);
pinMode(用户模式按钮2,输入\上拉);
序列号开始(115200);
Serial.println(“”);
Serial.println(“选择模式:”);
// ----------------------------------------------------------------------------- //
while(true)
{
if(数字读取(用户模式按钮1)=低)
{
usermode=1;
Serial.println(“选择模式1”);
打破
}
if(数字读取(用户模式按钮2)=低)
{
usermode=2;
Serial.println(“选择模式2”);
打破
}
延迟(1000);
}
// ----------------------------------------------------------------------------- //
if(usermode==1)
{
Serial.println(“设置模式1”);
Serial.println(“开始可编辑的工作…”);
bleKeyboard.begin();
pinMode(kbutton1,输入\上拉);
前一时间=0;
}
if(usermode==2)
{
Serial.println(“设置模式2”);
Serial.println(“控制表面启动…”);
RelativeCCSender::setMode(relativeCCmode::TWOS_补码);
控件_Surface.begin();//初始化控件表面
}
}
// ============================================================================= //
void循环()
{
while(usermode==1)
{
while(bleKeyboard.isConnected())
{
//模式1编码器
long enc1_newPos=kencoder1.read();
currentTime=esp_timer_get_time();
如果(enc1_newPosEnInterval)
{
enc1_oldPos=enc1_newPos;
previousTime=当前时间;
//bleKeyboard.write(按下媒体和音量键);
序列号。打印(“附件1:”);
串行打印LN(enc1_newPos);
}
如果(enc1_newPos>enc1_oldPos&¤tTime-previousTime>EnInterval)
{
enc1_oldPos=enc1_newPos;
previousTime=当前时间;
//bleKeyboard.write(按媒体音量向上);
序列号。打印(“附件1:”);
串行打印LN(enc1_newPos);
}
//模式1键
kbutton1State=digitalRead(kbutton1);
if(kbutton1State==LOW&¤tTime-previousTime>keyInterval)
{
previousTime=当前时间;
Serial.println(“按下按钮1”);
bleekeyboard.print(“1”);
}
}
}
while(usermode==2)
{
Control_Surface.loop();//刷新所有元素
}
}
首先,您的代码需要进行总体整理。不要在#include或#define部分之后实例化对象
避免这样做:
#include <BleKeyboard.h>
BleKeyboard bleKeyboard;
#include <MIDI_Interfaces/BluetoothMIDI_Interface.hpp>
BluetoothMIDI_Interface midi;
在图书馆的repo中使用的例子中,我最突出的是,你有一个单独的硬件——编码器,但你使用了两个不同的图书馆来访问它。如果不查看这两个库的源代码,我会怀疑有冲突中断的问题。通过添加第二个库与单个硬件交互,您试图解决什么问题?
// Include libraries
#include <lib1.h>
#include <lib2.h>
...
// Macros
#define SOMETHING_FUNNY value // see the Macro name in capitals?
#define SOMETHING_USEFUL anothervalue
...
/*
* Variables Section.
* Also, I usually categorize my variables section by type: floats, integers, chars, etc. If I ever use booleans I pack them in a section called 'flags'. Also, if a boolean is used inside an interrupt it should be volatile
*/
Type var_name1 = initial_value; // the initaliztion is optional
AnotherType var_name2;
...
// Object instantiation
ClassName object1;
AnotherClassName object2 = new AnotherClassName(constructor_parameters);
...
//Setup Function
void setup(){
// Serial Port initialization goes first
Serial.begin(baudrate);
// Initialization routines --> use functions!
}
void loop(){
// Check for states of your FSM and call the respective function
}
// Functions definitions
output type myFunction1(args){
// Routine
}
...
#define ENCODER_DO_NOT_USE_INTERRUPTS