Writing Custom Library's using SPI and Serial

Hi all,
I’m trying to write a library for which I followed the website tutorial, but although it compiles correctly (or without compiler errors) it does not seem to run correctly and I can’t find an answer or more advanced tutorial anywhere. I suspect there is something wrong with the order?

I’ve created a class and with the constructor function in the .cpp I have the following;

#include <Arduino.h>
#include <SPI.h>
#include "SmartStepV01.h"

SPISettings SmartStepSPI(4000000, MSBFIRST, SPI_MODE0);

SmartStep::SmartStep(int sPin, int rPin)
{
  pinMode (_CS_PIN, OUTPUT);
  pinMode (_RD_PIN, INPUT_PULLUP);
  digitalWrite (_CS_PIN, HIGH);
  _CS_PIN = sPin;
  _RD_PIN = rPin;
  SPI.begin();
  delay(20);
  Serial.begin(57600);
  Serial.print("= = = = = =\n");
  Serial.print("  START\n");
  Serial.print("- - - - - -\n");
  delay(250);
}
//.....lots of 'SmartStep::' functions

Within the .h;

#ifndef SMARTSTEPV01_H
#define SMARTSTEPV01_H

class SmartStep
{
  public:
    SmartStep(int sPin, int rPin);
//.....lots of functions declarations

};
#endif

My arduino code then starts;

#include <SmartStepV01.h>


//pins
#define _RD_PIN 31
#define _WT_PIN 32
#define _CS_PIN 35

SmartStep A(_CS_PIN, _RD_PIN);

void setup() {
  pinMode (_WT_PIN, OUTPUT);
  digitalWrite (_WT_PIN, LOW);
  A.initialise(0, 600, 300, 4, 0);
  Serial.print("here");

The code compiles, but i’m expecting to see the;
Serial.print("= = = = = =\n");
Serial.print(" START\n");
Serial.print("- - - - - -\n");
from creating an instance of the smartstep class (A), or at least the ‘here’ printed within the ‘normal’ code. The SPI doesn’t appear to work either. Can anybody help?
I can post the full code if people would like but I think it would confuse the issue, since the function code worked fine until i tried to library-ise it. Thanks

I thought I should add that this is my first time trying to write a library, and although my C isn't too awful, my C++ is very beginner

If the full code will confuse the issue, did you start building the library with say just the 'SmartStep::SmartStep(int sPin, int rPin)' code, and check that the basic building blocks were working before adding "//.....lots of functions declarations" ?

One thing I did spot, you should consider using SPI.beginTransaction() and SPI.endTransaction() when using SPI in the library, so that any specific SPI settings for your devices are not overwritten by another device library using SPI.

Fair point srnet. I took everything else out but it still doesn’t print. Also the lower functions were using the SPI.transfer(value) and as i said it all worked before i moved them into the library. I think/assume it’s a problem with the way I call SPI.begin() and Serial.begin()?
I’ve tried looking at adafruit librarys that use the spi and serial, but they all seem to be much more complicated using pointers. The tutorial on the arduino site lead me to believe I could use standard arduino calls within a library so long as I included the <Arduino.h>
.h is now;

#ifndef SMARTSTEPV01_H
#define SMARTSTEPV01_H

class SmartStep
{
  public:
    SmartStep(int sPin, int rPin);

    

  private:
    //bool isDEF;     //Drive enabled flag
    //bool isSF;      //Stop flag
    //bool isMF;      //Moving flag
    //bool isLK;      //Write lock flag
    uint8_t _CS_PIN;
    uint8_t _RD_PIN;
	//uint8_t spiTransaction_get1(uint8_t);
	//uint16_t spiTransaction_get2(uint8_t);
	//int32_t spiTransaction_get3(uint8_t);
	//void spiTransaction_set1(uint8_t, uint8_t);
	//void spiTransaction_set2(uint8_t, uint16_t);
	//void spiTransaction_set3(uint8_t, int32_t);
};
#endif

the .cpp now;

#include <Arduino.h>
#include <SPI.h>
#include "SmartStepV01.h"

// Debug
#define _ISDEBUG 1

#ifdef _ISDEBUG
SPISettings SmartStepSPI(400000, MSBFIRST, SPI_MODE0);
#else
SPISettings SmartStepSPI(4000000, MSBFIRST, SPI_MODE0);
#endif


SmartStep::SmartStep(int sPin, int rPin)
{
  pinMode (_CS_PIN, OUTPUT);
  pinMode (_RD_PIN, INPUT_PULLUP);
  digitalWrite (_CS_PIN, HIGH);
  _CS_PIN = sPin;
  _RD_PIN = rPin;
  SPI.begin();
  delay(20);
  Serial.begin(57600);
  Serial.print("= = = = = =\n");
  Serial.print("  START\n");
  Serial.print("- - - - - -\n");
  delay(250);
}

main code;

#include <SmartStepV01.h>


//pins
#define _RD_PIN 31
#define _WT_PIN 32
#define _CS_PIN 35

SmartStep A(_CS_PIN, _RD_PIN);

void setup() {
  pinMode (_WT_PIN, OUTPUT);
  digitalWrite (_WT_PIN, LOW);
  //A.initialise(0, 600, 300, 4, 0);
  Serial.print("here");
}

void loop() {
  digitalWrite(_WT_PIN, HIGH);
  while (1) {

  }
}

Just about everything you're trying to do in the SmartStep constructor should not be done there. Plus your '_CS_PIN' and '_RD_PIN' have no defined value when the constructor runs.

Judging from the meager code you posted, it looks to me all the constructor should do is assign its arguments to '_CS_PIN' and '_RD_PIN'. Move everything else to a begin() method that you can call from setup().

Ah, ok, i’ll give it a go that way. Oh and yes you’re right about the spi begin transaction and end, i see what you meant now so that other librarys can use other settings. Thank you!

Perfect!