DRV8847 TI Driver Library

Hi all,
I am trying to build an Arduino Library for DRV8847. I want to make it universal for all the Arduinos and also to utilize all its functions easily. Any suggestions or improvement in the attached code is highly appreciated.
Thanks

/* 
   drv8847.cpp
Copyright (c) [2022] [Ali Khan]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
 */


#ifndef drv8847_h
#define drv8847_h

#if (ARDUINO >= 100)

#include "Arduino.h"

#else

#include  "WProgram.h"

#endif

class drv8847 {

public:

drv8847();          // default Constructor
drv8847 (uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t); // Constructor that sets pins nSLEEP MODE nFAULT IN1 IN2 IN3 IN4

// Methods
void begin();                          // Setup hardware
void Bridge_mode_Sel(bool mode1, bool mode2, bool mode3);               // Bridge Mode Selection

void Sleep_mode();                         //Sleep mode

void Motor_coast(bool mot1, bool mot2);  //Motor coast (fast decay)

void Motor_Reverse(bool mot1, bool mot2);            //Reverse direction

void Motor_Forward(bool mot1, bool mot2);          //Forward direction

void Motor_Brake  (bool mot1, bool mot2);        //Motor brake (slow decay)




private:
// private properties and method
bool _check_valid(); //check that Pin configuration is set before doing anything
uint8_t _nSLEEP, _MODE, _nFAULT, _IN1, _IN2, _IN3, _IN4; 
};
#endif```
/* 
   drv8847.cpp

Copyright (c) [2022] [Ali Khan]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "drv8847.h"

drv8847::drv8847();

drv8847::drv8847 (uint8_t nSLEEP , uint8_t MODE, uint8_t nFAULT, uint8_t IN1, uint8_t IN2, uint8_t IN3, uint8_t IN4)
{
_nSLEEP = nSLEEP;   // Constructor that sets pins nSLEEP MODE nFAULT IN1 IN2 IN3 IN4
_MODE   = MODE  ;
_nFAULT = nFAULT;
_IN1    = IN1   ;
_IN2    = IN2   ;
_IN3    = IN3   ;
_IN4    = IN4   ; 
  
}

void drv8847::begin()
{
  pinMode(_nSLEEP,OUTPUT);
    pinMode(_MODE,OUTPUT);
      pinMode(_nFAULT,OUTPUT);
        pinMode(_IN1,OUTPUT);
          pinMode(_IN2,OUTPUT);
            pinMode(_IN3,OUTPUT);
              pinMode(_IN4,OUTPUT);
  
}

void drv8847::Bridge_mode_Sel(bool mode1, bool mode2, bool mode3)
{
  if(mode1 == true)                       // 4 pin Mode
  {
  digitalWrite(_nSLEEP ,HIGH);  
  digitalWrite(_MODE    ,LOW);
    
  }

  else if( mode2 == true)                 // 2 pin Mode
  {
  digitalWrite(_nSLEEP ,HIGH);  
  digitalWrite(_MODE   ,HIGH);
  digitalWrite(_IN3    ,LOW);    
  }

  else if( mode3 == true)                 // parallel Mode
  {
  digitalWrite(_nSLEEP ,HIGH);  
  digitalWrite(_MODE   ,HIGH);
  digitalWrite(_IN3    ,HIGH);    
  }
  
}
/////////////////////////////
void drv8847:: Sleep_mode()
{
  digitalWrite(_nSLEEP,LOW);    
}
/////////////////////////////

void drv8847:: Motor_coast(bool mot1, bool mot2)
{
  if( mot1 == true)
 { 
  digitalWrite(_nSLEEP,HIGH);  
  digitalWrite(_IN1    ,LOW);  
  digitalWrite(_IN2    ,LOW);   
 }

  else if ( mot2 == true)
 {
  digitalWrite(_nSLEEP,HIGH);  
  digitalWrite(_IN3    ,LOW);  
  digitalWrite(_IN4    ,LOW);
 }    
}
/////////////////////////////

void drv8847:: Motor_Reverse(bool mot1, bool mot2)
{
  if( mot1 == true)
 { 
  digitalWrite(_nSLEEP,HIGH);  
  digitalWrite(_IN1    ,LOW);  
  digitalWrite(_IN2   ,HIGH);
 }
     
  else if ( mot2 == true)
 {
  digitalWrite(_nSLEEP,HIGH);  
  digitalWrite(_IN3    ,LOW);  
  digitalWrite(_IN4   ,HIGH);    
 }
}
/////////////////////////////

void drv8847:: Motor_Forward(bool mot1, bool mot2)
{
  if( mot1 == true)
 { 
  digitalWrite(_nSLEEP,HIGH);  
  digitalWrite(_IN1   ,HIGH);  
  digitalWrite(_IN2    ,LOW);  
}

  else if ( mot2 == true)
  
 {
  digitalWrite(_nSLEEP,HIGH);  
  digitalWrite(_IN3   ,HIGH);  
  digitalWrite(_IN4    ,LOW);  
 }

}
/////////////////////////////

void drv8847:: Motor_Brake  (bool mot1, bool mot2)
{
    if( mot1 == true)
 {
  digitalWrite(_nSLEEP,HIGH);  
  digitalWrite(_IN1   ,HIGH);  
  digitalWrite(_IN2   ,HIGH);   
}

    else if ( mot2 == true)
 {
  digitalWrite(_nSLEEP,HIGH);  
  digitalWrite(_IN3   ,HIGH);  
  digitalWrite(_IN4   ,HIGH);   
 }
}
/////////////////////////////

@nickgammon

  • Add an open source license - without a licence, intellectual property is "all rights reserved", meaning nobody can use it.
  • Use a code formatter

And the most important suggestion of all: test your code. You posted it to the forum without even compiling it.

Hi @in0
Thanks for your reply. I missed your email because it was in spam. Thanks for all your suggestions. I thought I compiled it but I will upload it again because I did some modifications. You mentioned about Add an open source license. I don't have any experience with it before. Can you further elaborate on why I need this?

It is explained in detail here:

Hi @in0
I added the license. I guess we are good to go now. Thanks

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.