From cd2102cbd4857a533597db8c1d0edf79ffa3cb0e Mon Sep 17 00:00:00 2001 From: jusax23 Date: Sun, 24 Sep 2023 20:46:10 +0200 Subject: [PATCH] io --- io.h | 71 +++++++++++++++++++++++++++++++++++++++++++++ juRCReceiver.h | 1 + structsAndDefines.h | 13 ++------- 3 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 io.h diff --git a/io.h b/io.h new file mode 100644 index 0000000..011b921 --- /dev/null +++ b/io.h @@ -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 \ No newline at end of file diff --git a/juRCReceiver.h b/juRCReceiver.h index 1413586..db4fafd 100644 --- a/juRCReceiver.h +++ b/juRCReceiver.h @@ -44,6 +44,7 @@ unsigned long delta = 0; float avg = 10; #include "system_general/rc_handler.h" +#include "io.h" // diff --git a/structsAndDefines.h b/structsAndDefines.h index 6b7cf39..201e340 100644 --- a/structsAndDefines.h +++ b/structsAndDefines.h @@ -1,19 +1,10 @@ #ifndef _structsAndDefines_h_ #define _structsAndDefines_h_ -enum jChannel { - 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, -}; +byte jChannel[] = {13, 12, 14, 27, 26, 25, 33, 32, 15}; enum jType { + jUnused = 0, jServo = 1, jhBridge = 2, jESC = 3,