This commit is contained in:
jusax23 2023-09-24 20:46:10 +02:00
parent ce6e046d4b
commit cd2102cbd4
Signed by: jusax23
GPG key ID: 499E2AA870C1CD41
3 changed files with 74 additions and 11 deletions

71
io.h Normal file
View file

@ -0,0 +1,71 @@
#ifndef jIO_H_
#define jIO_H_
#define esc_freq 250
#define esc_res 12
#define esc_duty (1 << esc_res) / (1000.0 / esc_freq)
#include "structsAndDefines.h"
size_t _io_id_count = 0;
jType _io_types[9] = {jUnused, jUnused, jUnused, jUnused, jUnused,
jUnused, jUnused, jUnused, jUnused};
void iniOut(size_t i, jType type) {
if (type == jServo) {
_io_types[i] = jServo;
ledcSetup(i, 50, 16);
ledcAttachPin(jChannel[i], i);
} else if (type == jhBridge) {
_io_types[i] = jhBridge;
ledcSetup(i, 30000, 8);
ledcAttachPin(jChannel[i], i);
} else if (type == jESC) {
_io_types[i] = jESC;
ledcSetup(i, esc_freq, esc_res);
ledcAttachPin(jChannel[i], i);
} else if (type == jDigital) {
_io_types[i] = jDigital;
pinMode(jChannel[i], OUTPUT);
}
}
bool setoutput(int c, float v) {
if (sizeof(jChannel) <= c) {
return false;
}
if (_io_types[c] == jServo) {
// uint32_t duty = (((v/180.0)*2000)/20000.0*65536.0) + 1634;// convert
// 0-180 degrees to 0-65536
if (v > 180) v = 180.0;
if (v < 0) v = 0.0;
int duty = (((v * 2000.0 / 180.0) + 500) / (20000.0 / pow(2.0, 16.0)));
Serial.println(duty);
ledcWrite(c, duty);
return true;
} else if (_io_types[c] == jhBridge) {
if (v > 100) v = 100.0;
if (v < 0) v = 0.0;
Serial.println((int)(v * 105.0 / 100.0) + 150);
ledcWrite(c, (int)(v * 105.0 / 100.0) + 150);
return true;
} else if (_io_types[c] == jESC) {
if (v > 100) v = 100.0;
if (v < 0) v = 0.0;
float escout =
(v * (esc_duty /* highrt - lower = lower*/) / 100.0) + esc_duty;
ledcWrite(c, escout);
return true;
} else if (_io_types[c] == jDigital) {
if (v > 0) {
digitalWrite(jChannel[c], HIGH);
} else {
digitalWrite(jChannel[c], LOW);
}
return true;
}
return false;
}
#endif

View file

@ -44,6 +44,7 @@ unsigned long delta = 0;
float avg = 10; float avg = 10;
#include "system_general/rc_handler.h" #include "system_general/rc_handler.h"
#include "io.h"
// //

View file

@ -1,19 +1,10 @@
#ifndef _structsAndDefines_h_ #ifndef _structsAndDefines_h_
#define _structsAndDefines_h_ #define _structsAndDefines_h_
enum jChannel { byte jChannel[] = {13, 12, 14, 27, 26, 25, 33, 32, 15};
jChannel_0 = 13,
jChannel_1 = 12,
jChannel_2 = 14,
jChannel_3 = 27,
jChannel_4 = 26,
jChannel_5 = 25,
jChannel_6 = 33,
jChannel_7 = 32,
jChannel_8 = 15,
};
enum jType { enum jType {
jUnused = 0,
jServo = 1, jServo = 1,
jhBridge = 2, jhBridge = 2,
jESC = 3, jESC = 3,