22*************************************************************************************************
33@file Deneyap_Servo.cpp
44@mainpage Deneyap Servo Arduino library source file
5- @version v1.1.0
5+ @version v1.1.1
66@date October 20, 2022
77@brief Includes functions to drive servo motor for Deneyap Development Boards
88*************************************************************************************************
99*/
1010
1111#include " Deneyap_Servo.h"
1212
13+ #ifndef CORE_DK_VERSION
14+ #define CORE_DK_VERSION 0 // fallback
15+ #endif
1316/* *
1417 * @brief attaches the given pin, channel, freq, resolution
1518 * @param @pin : servo pin
@@ -23,8 +26,14 @@ void Servo::attach(int pin, int channel, int freq, int resolution) {
2326 _channel = channel;
2427 _pin = pin;
2528 if (channel > 15 ) channel = 15 ;
29+ #if CORE_DK_VERSION <= 103013
30+ ledcSetup (_channel, freq, resolution);
31+ ledcAttachPin (pin, channel);
32+ ledcWrite (_channel, FIRSTDUTY);
33+ #else
2634 ledcAttachChannel (pin, freq, resolution, channel);
2735 ledcWriteChannel (_channel, FIRSTDUTY);
36+ #endif
2837}
2938
3039/* *
@@ -36,15 +45,25 @@ void Servo::attach(int pin, int channel, int freq, int resolution) {
3645void ESC::attach (int pin, int channel, int freq, int resolution) {
3746 _channel = channel;
3847 if (channel > 15 ) channel = 15 ;
48+ #if CORE_DK_VERSION <= 103013
49+ ledcSetup (_channel, freq, resolution);
50+ ledcAttachPin (pin, channel);
51+ ledcWrite (_channel, FIRSTDUTY);
52+ #else
3953 ledcAttachChannel (pin, freq, resolution, channel);
4054 ledcWriteChannel (_channel, FIRSTDUTY);
55+ #endif
4156}
4257
4358void Servo::write (int value) {
4459 if (value < SERVOMIN) value = SERVOMIN;
4560 if (value > SERVOMAX) value = SERVOMAX;
4661 int servoValue = (value - SERVOMIN) * (DUTYCYLEMAX - DUTYCYLEMIN) / (SERVOMAX - SERVOMIN) + DUTYCYLEMIN; // mapping to SERVOMIN-SERVOMAX values from DUTYCYLEMIN-DUTYCYLEMAX values
62+ #if CORE_DK_VERSION <= 103013
63+ ledcWrite (_channel, servoValue);
64+ #else
4765 ledcWriteChannel (_channel, servoValue); // _channel select servoValue(duty) to be set for selected channel
66+ #endif
4867}
4968
5069void Servo::writeMicroseconds (int value) {
@@ -58,11 +77,19 @@ void ESC::write(int value) {
5877 if (value < ESCMIN) value = ESCMIN;
5978 if (value > ESCMAX) value = ESCMAX;
6079 int escValue = (value - ESCMIN) * (ESCDUTYCYLEMAX - ESCDUTYCYLEMIN) / (ESCMAX - ESCMIN) + ESCDUTYCYLEMIN; // mapping to SERVOMIN-SERVOMAX values from DUTYCYLEMIN-DUTYCYLEMAX values
80+ #if CORE_DK_VERSION <= 103013
81+ ledcWrite (_channel, escValue);
82+ #else
6183 ledcWriteChannel (_channel, escValue); // _channel select servoValue(duty) to be set for selected channel
84+ #endif
6285}
6386
6487int Servo::read () {
88+ #if CORE_DK_VERSION <= 103013
89+ int dutyCycle = ledcRead (_channel);
90+ #else
6591 int dutyCycle = ledcRead (_pin);
92+ #endif
6693 int newDutyCycle = map (dutyCycle, DUTYCYLEMIN, DUTYCYLEMAX, SERVOMIN, SERVOMAX);
6794 if (newDutyCycle % 45 == 0 ){
6895 return newDutyCycle;
0 commit comments