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;
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
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
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!