Browse Source

io firmware with encoder support

main
gsd 7 months ago
parent
commit
5acab6eac7
  1. 5
      .vscode/settings.json
  2. 82
      pipboyIO/encoder1.cpp
  3. 4
      pipboyIO/encoder1.h
  4. 35
      pipboyIO/pipboyIO.ino
  5. 5
      pipboyIO/readme

5
.vscode/settings.json

@ -0,0 +1,5 @@
{
"files.associations": {
"cstdint": "cpp"
}
}

82
pipboyIO/encoder1.cpp

@ -0,0 +1,82 @@
#include <cstdint>
#include <binary.h>
#include <esp32-hal-gpio.h>
#include <Arduino.h>
#include "encoder1.h"
#define PinA 27 // контат AB
#define PinC 14 // контат BC
//#define PinS 5 // кнопка
#define encoder1
uint8_t pinA = 0; // состояние контата AB
uint8_t pinC = 0; // состояние контата BC
uint8_t blockA = false; // блокировка прерывания на контакте AB
uint8_t blockC = false; // блокировка прерывания на контакте BC
uint8_t encoderResult = 0; // Результирующая переменная
int16_t encoderCount = 0; // Счетчик щелчков энкодера
int16_t oldencoderCount = 0; // Счетчик старое значение
int16_t getEncoderCount() {
return encoderCount;
}
int16_t getOldencoderCount() {
return oldencoderCount;
}
void setOldencoderCount(int16_t val) {
oldencoderCount = val;
}
void encoderFilter () {
encoderResult = encoderResult & B00001111;
if (encoderResult == B1011) encoderCount ++; // по часовой стрелке
if (encoderResult == B0111) encoderCount --; // против часосовой стрелки
}
void pinACHANGE () { // Изменилось состояние контакта АB
if (blockA == true) return;
pinA = !digitalRead(PinA);
pinC = !digitalRead(PinC);
encoderResult <<= 1;
bitWrite(encoderResult, 0, pinA);
encoderResult <<= 1;
bitWrite(encoderResult, 0, pinC);
encoderFilter();
if (!pinA && !pinC) blockA = false; else blockA = true;
blockC = false;
}
void pinCCHANGE () { // Изменилось состояние контакта BC
if (blockC == true) return;
pinA = !digitalRead(PinA);
pinC = !digitalRead(PinC);
encoderResult <<= 1;
bitWrite(encoderResult, 0, pinA);
encoderResult <<= 1;
bitWrite(encoderResult, 0, pinC);
encoderFilter();
if (!pinA && !pinC) blockC = false; else blockC = true;
blockA = false;
}
void setupEncoder1() {
pinMode (PinA, INPUT_PULLUP); // Как вход и внутренняя подтяжка
pinMode (PinC, INPUT_PULLUP); // Как вход и внутренняя подтяжка
attachInterrupt(digitalPinToInterrupt(PinA), pinACHANGE, CHANGE);
attachInterrupt(digitalPinToInterrupt(PinC), pinCCHANGE, CHANGE);
}
/*void loop() {
if (oldencoderCount != encoderCount) {
Serial.println (encoderCount);
oldencoderCount = encoderCount;
}
}*/

4
pipboyIO/encoder1.h

@ -0,0 +1,4 @@
void setupEncoder1();
int16_t getEncoderCount();
int16_t getOldencoderCount();
void setOldencoderCount(int16_t val);

35
pipboyIO/pipboyIO.ino

@ -1,18 +1,18 @@
#include <Encoder.h>
#include "encoder1.h"
const int input[] = {36, 39, 34 ,35 ,32 ,33 ,25 ,26};
long inputValue[] = {0, 0, 0, 0, 0, 0, 0, 0};
const int numPins = sizeof(input) / sizeof(input[0]);
Encoder enc1(14,27);
Encoder enc2(13,12);
long positions[3] = {0, 0, 0};
#define RXD2 (16)
#define TXD2 (17)
void setup() {
//Serial.begin(115200);
Serial.begin(115200);
//#ifdef encoder1
setupEncoder1();
//#endif
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
analogReadResolution(12);
analogSetAttenuation(ADC_11db);
@ -26,14 +26,6 @@ void values2uart() {
Serial2.print(inputValue[i]);
Serial2.print("\n");
}
for (int i = 0; i < 2; i++) {
Serial2.print("10");
Serial2.print(i);
Serial2.print("-");
Serial2.print(positions[i]);
Serial2.print("\n");
}
}
void loop() {
@ -41,16 +33,13 @@ void loop() {
inputValue[i] = map(analogRead(input[i]), 0, 4096, 0, 25);
}
long newPos1 = enc1.read();
long newPos2 = enc2.read();
if (newPos1 != positions[0]) {
positions[0] = newPos1;
}
if (newPos2 != positions[1]) {
positions[1] = newPos2;
//#ifdef encoder1
// Serial.print(0);
if (getOldencoderCount() != getEncoderCount()) {
Serial.println (getEncoderCount());
setOldencoderCount(getEncoderCount());
}
//#endif
values2uart();
delay(50);

5
pipboyIO/readme

@ -1,4 +1,7 @@
sudo apt install xdotool
export DISPLAY=:0
xdotool key Return
...
...
board esp32 wrover kit (all version)
disable bt+wifi
Loading…
Cancel
Save