Skip to content

Commit

Permalink
SI4844 Arduino Library
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Lima Caratti committed Jul 9, 2020
1 parent 5d40375 commit 5dd7b5d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 28 deletions.
23 changes: 12 additions & 11 deletions SI4844.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void SI4844::getCommandResponse(int response_size, uint8_t *response)
void SI4844::waitInterrupt(void)
{

while (!data_from_si4844)
while (!data_from_device)
;
}

Expand All @@ -154,6 +154,7 @@ void SI4844::setup(uint16_t resetPin, int interruptPin, byte defaultBand)
this->interruptPin = interruptPin;

// Arduino interrupt setup.
// if interruptPin parameter is < 0, it means the interrupt is being controlled by the user of this library
if (interruptPin != -1 ) {
pinMode(interruptPin, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), interrupt_hundler, RISING);
Expand All @@ -162,7 +163,7 @@ void SI4844::setup(uint16_t resetPin, int interruptPin, byte defaultBand)
pinMode(resetPin, OUTPUT);
digitalWrite(resetPin, HIGH);

data_from_si4844 = false;
data_from_device = false;

reset();

Expand Down Expand Up @@ -202,7 +203,7 @@ void SI4844::debugDevice(uint16_t resetPin, uint16_t interruptPin, byte defaultB
// showFunc("So far so good 1");
// delay(1000);

data_from_si4844 = false;
data_from_device = false;
reset();

// showFunc("So far so good 2");
Expand Down Expand Up @@ -232,7 +233,7 @@ void SI4844::reset()
// waitToSend();

setClockLow(); // See *Note on page 5
data_from_si4844 = false;
data_from_device = false;
digitalWrite(resetPin, LOW);
delayMicroseconds(200);
digitalWrite(resetPin, HIGH);
Expand All @@ -250,7 +251,7 @@ void SI4844::reset()
*/
void SI4844::powerDown(void)
{
data_from_si4844 = false;
data_from_device = false;
// Wait until rady to send a command
waitToSend();
Wire.beginTransmission(SI4844_ADDRESS);
Expand Down Expand Up @@ -292,7 +293,7 @@ void SI4844::setBand(byte new_band)
new_band |= 0b10000000;
new_band &= 0b10111111;

data_from_si4844 = false;
data_from_device = false;

// Wait until rady to send a command
waitToSend();
Expand Down Expand Up @@ -602,7 +603,7 @@ si4844_firmware_response *SI4844::getFirmware(void)
for (int i = 0; i < 9; i++)
firmware_response.raw[i] = Wire.read();

data_from_si4844 = false;
data_from_device = false;

return &firmware_response;
}
Expand Down Expand Up @@ -647,7 +648,7 @@ float SI4844::getFrequency(void)
f += (status_response.refined.d2) * 100;
f += (status_response.refined.d1) * 1000;

data_from_si4844 = false;
data_from_device = false;

return (f * multFactor + addFactor);
}
Expand All @@ -662,7 +663,7 @@ float SI4844::getFrequency(void)
*/
bool SI4844::hasStatusChanged(void)
{
return data_from_si4844;
return data_from_device;
}

/**
Expand All @@ -671,7 +672,7 @@ bool SI4844::hasStatusChanged(void)
*/
void SI4844::resetStatus()
{
data_from_si4844 = false;
data_from_device = false;
}

/**
Expand Down Expand Up @@ -709,7 +710,7 @@ void SI4844::setCustomBand(byte bandIndex, uint16_t botton, uint16_t top, byte
setBand(bandIndex);

// Now we can customize the band.
data_from_si4844 = false;
data_from_device = false;
customband.refined.bandindex = bandIndex;
customband.refined.xowait = 0;
customband.refined.xoscen = 1;
Expand Down
39 changes: 33 additions & 6 deletions SI4844.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,12 @@ typedef union {
* mono FM signal and has switched to stereo. The variable below indicates a change of the ATDD status. It will need to
* process some action (for example show on LCD this changes).
*/
volatile static bool data_from_si4844;
volatile static bool data_from_device;

static void interrupt_hundler()
{

data_from_si4844 = true;
data_from_device = true;
}


Expand Down Expand Up @@ -227,6 +227,24 @@ class SI4844
uint8_t bassTreble = 4;

public :
/**
* @ingroup GB
* @brief Set the Data Status From Device
* @details It is a flag that means the device triggered an interrupt.
* @details You can use this function to back the flag status to false. This way you can check when the device triggers the next interrupt.
* @details It is very useful when the user wants to control the interrupt instead of give this control to the library.
* @param value true or false
*/
inline void setStatusInterruptFromDevice( bool value ) { data_from_device = value; };
/**
* @ingroup GB
* @brief Get the Data Status From Device
* @details It returns true when the device has triggered an interrupt.
* @return true or false
*/
inline bool getDataStatusInterruptFromDevice() { return data_from_device; };


void setProperty(uint16_t propertyNumber, uint16_t parameter);
uint16_t getProperty(uint16_t propertyNumber);
void sendCommand(uint8_t cmd, int parameter_size, const uint8_t *parameter);
Expand Down Expand Up @@ -267,18 +285,27 @@ public :
bool hasStatusChanged(void);
void resetStatus(void);


/**
* @ingroup GB
* @brief Gets the current audio volume level
*
* @return uint8_t
* @return Volume level
*/
inline uint8_t getVolume() {return volume; };
uint8_t getVolumeProperty();

// return 0 = "FM mode"; 1 = "AM mode"; 2 = "SW mode".
/**
* @ingroup GB
* @brief Get the Band Mode
* @return char* "FM", "AM" or "SW"
*/
inline char * getBandMode(){ return (char *) bandmode_table[status_response.refined.BANDMODE]; };
// return char * "Off" or stereo "On"

/**
* @ingroup GB
* @brief Get the Stereo Indicator
* @return char* "ON" or "OFF"
*/
inline char * getStereoIndicator(){ return (char *) stereo_indicator_table[status_response.refined.STATION]; };


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@
Test and validation of the SI4844 Arduino Library with ATtiny85.
It is a simple AM/FM radio implementation.
This sketch uses the PinChangeInterrupt Library to deal with interrupt with Attiny devices.
Know more on https://github.com/NicoHood/PinChangeInterrupt
Prototype documentation : https://pu2clr.github.io/SI4735/
PU2CLR Si47XX API documentation: https://pu2clr.github.io/SI4735/extras/apidoc/html/
By Ricardo Lima Caratti, Jan 2020.
*/

#include <PinChangeInterrupt.h>

#include <SI4844.h>
#include <Tiny4kOLED.h>


#define INT_PIN PB3 // Pin 2 is used to control interrupt.
#define INT_PIN PCINT3 // Physical pin 2 is used to control interrupt.

#define RST_PIN PB4 // Pin 3 is used to control the RESET of the device.
#define BAND_SWITC PB1 // Pin 6 is used to switch the band.
#define RST_PIN PB4 // Physical Pin 3 is used to control the RESET of the device.
#define BAND_SWITC PB1 // Physical Pin 6 is used to switch the band.

#define AM 20
#define FM 0
Expand All @@ -27,12 +32,11 @@ float currentFrequency;

SI4844 radio;

// interrupt service routine for PCINT3
ISR(PCINT3_vect)
{
interrupt_hundler(); // See SI4844.h
void handle_interrupt() {
radio.setStatusInterruptFromDevice(true);
}


void setup()
{

Expand All @@ -53,11 +57,9 @@ void setup()
oled.clear();
*/

GIMSK = 1 << PCIE; // turn on pin change interrupts
PCMSK = 1 << PCINT3; // unmask PCINT3 pin change interrupt

sei();
attachPCINT(digitalPinToPCINT(INT_PIN), handle_interrupt, CHANGE);

// -1 means this sketch will handle the interrupt.
radio.setup(RST_PIN, -1, currentBand);

// Comment the line above and uncomment the line bellow to debug. See the function debugDevice dicumentation
Expand Down

0 comments on commit 5dd7b5d

Please sign in to comment.