 |
PU2CLR BK108X Arduino Library
1.0.1
This is an Arduino Library to control the BK108X device
|
Go to the documentation of this file.
42 void BK108X::
setI2C(uint8_t i2c_addr) {
43 this->deviceAddress = i2c_addr;
54 void BK108X::
i2cInit(
int pin_sdio,
int pin_sclk){
55 this->pin_sdio = pin_sdio;
56 this->pin_sclk = pin_sclk;
65 pinMode(
this->pin_sdio, OUTPUT);
66 pinMode(
this->pin_sclk, OUTPUT);
67 digitalWrite(
this->pin_sdio, HIGH);
68 digitalWrite(
this->pin_sclk, HIGH);
71 digitalWrite(
this->pin_sdio, LOW);
73 digitalWrite(
this->pin_sclk, LOW);
75 digitalWrite(
this->pin_sdio, HIGH);
84 pinMode(pin_sdio, OUTPUT);
85 digitalWrite(
this->pin_sdio, LOW);
88 digitalWrite(
this->pin_sclk, HIGH);
91 digitalWrite(
this->pin_sdio, HIGH);
104 pinMode(pin_sdio, OUTPUT);
105 digitalWrite(
this->pin_sclk, LOW);
106 digitalWrite(
this->pin_sdio, LOW);
107 delayMicroseconds(1);
108 digitalWrite(
this->pin_sclk, HIGH);
109 delayMicroseconds(1);
110 digitalWrite(
this->pin_sclk, LOW);
120 pinMode(pin_sdio, OUTPUT);
122 digitalWrite(
this->pin_sclk, LOW);
123 digitalWrite(
this->pin_sdio, HIGH);
124 delayMicroseconds(1);
125 digitalWrite(
this->pin_sclk, HIGH);
126 delayMicroseconds(1);
127 digitalWrite(
this->pin_sclk, LOW);
139 pinMode(pin_sdio, INPUT);
140 delayMicroseconds(1);
142 digitalWrite(
this->pin_sclk, HIGH);
143 delayMicroseconds(1);
145 ack = digitalRead(
this->pin_sdio);
147 digitalWrite(
this->pin_sclk, LOW);
148 delayMicroseconds(1);
160 pinMode(pin_sdio, OUTPUT);
161 delayMicroseconds(1);
163 for (
int i = 0; i < 8; i++) {
165 digitalWrite(
this->pin_sdio, (
bool)(data &
this->deviceAddress) );
167 delayMicroseconds(1);
168 digitalWrite(
this->pin_sclk, HIGH);
169 delayMicroseconds(1);
170 digitalWrite(
this->pin_sclk, LOW);
184 pinMode(pin_sdio, INPUT);
185 delayMicroseconds(1);
187 for (
int i = 0; i < 8; i++)
189 digitalWrite(
this->pin_sclk, HIGH);
191 delayMicroseconds(1);
192 if ( digitalRead(
this->pin_sdio) )
194 digitalWrite(
this->pin_sclk, LOW);
195 delayMicroseconds(1);
209 word16_to_bytes data;
213 this->i2cWriteByte(
this->deviceAddress);
214 this->i2cReceiveAck();
218 this->i2cWriteByte(reg);
219 this->i2cReceiveAck();
221 this->i2cWriteByte(data.refined.highByte);
222 this->i2cReceiveAck();
223 this->i2cWriteByte(data.refined.lowByte);
224 this->i2cReceiveAck();
237 word16_to_bytes data;
240 this->i2cWriteByte(
this->deviceAddress);
241 this->i2cReceiveAck();
243 reg = (reg << 1) | 1;
245 this->i2cWriteByte(reg);
246 this->i2cReceiveAck();
248 data.refined.highByte =
this->i2cReadByte();
250 data.refined.lowByte =
this->i2cReadByte();
273 uint16_t reg_content;
274 reg_content =
this->readRegister(reg);
275 shadowRegisters[reg] = reg_content;
293 this->writeRegister(reg, value);
294 shadowRegisters[reg] = value;
295 delayMicroseconds(250);
304 return getRegister(
REG00);
314 return getRegister(
REG01);
339 tmp.raw = getRegister(
REG0A);
353 while ( reg0a->refined.STC == 0) {
358 reg03->refined.TUNE = 0;
359 setRegister(
REG03, reg03->raw);
369 reg02->refined.DISABLE = 1;
370 reg02->refined.ENABLE = 0;
371 setRegister(
REG02, reg02->raw);
372 reg02->refined.DISABLE = 0;
373 reg02->refined.ENABLE = 1;
374 setRegister(
REG02, reg02->raw);
388 reg02->refined.DISABLE = 0;
389 reg02->refined.ENABLE = 1;
390 setRegister(
REG02,reg02->raw);
392 setRegister(
REG03, 0x0000);
394 setRegister(
REG04, 0x60D4);
395 setRegister(
REG05, 0x37CF);
396 setRegister(
REG06, 0x086F);
397 setRegister(
REG07, 0x0101);
398 setRegister(
REG08, 0xAC90);
400 setRegister(
REG10, 0x7B11);
401 setRegister(
REG11, 0x004A);
402 setRegister(
REG12, 0x4000);
403 setRegister(
REG13, 0x3E00);
404 setRegister(
REG14, 0xC29A);
405 setRegister(
REG15, 0x79F8);
406 setRegister(
REG16, 0x4012);
408 setRegister(
REG17, 0x0040);
409 setRegister(
REG18, 0x341C);
410 setRegister(
REG19, 0x0080);
411 setRegister(
REG1A, 0x0000);
412 setRegister(
REG1B, 0x4CA2);
414 setRegister(
REG1C, 0x8820);
415 setRegister(
REG1D, 0x0200);
426 reg02->refined.DISABLE = 1;
427 reg02->refined.ENABLE = 0;
428 setRegister(
REG02, reg02->raw);
441 void BK108X::
setup(
int sda_pin,
int sclk_pin,
int rdsInterruptPin,
int seekInterruptPin, uint8_t oscillator_type)
446 if (rdsInterruptPin >= 0)
448 if (seekInterruptPin >= 0)
451 this->oscillatorType = oscillator_type;
467 void BK108X::
setFM(uint16_t minimum_frequency, uint16_t maximum_frequency, uint16_t default_frequency, uint16_t step)
469 this->currentStep = step;
470 this->currentFrequency = default_frequency;
471 this->minimumFrequency = minimum_frequency;
472 this->maximumFrequency = maximum_frequency;
476 setRegister(
REG07, reg07->raw);
478 this->currentFMBand = reg05->refined.BAND = 0;
479 this->currentFMSpace = reg05->refined.SPACE = 2;
480 setRegister(
REG05, reg05->raw);
482 setFrequency(default_frequency);
495 void BK108X::
setAM(uint16_t minimum_frequency, uint16_t maximum_frequency, uint16_t default_frequency, uint16_t step, uint16_t am_space)
497 this->currentStep = step;
498 this->currentFrequency = default_frequency;
499 this->minimumFrequency = minimum_frequency;
500 this->maximumFrequency = maximum_frequency;
502 this->currentMode = reg07->refined.MODE =
MODE_AM;
503 setRegister(
REG07, reg07->raw);
506 if (minimum_frequency < 520 )
507 this->currentAMBand = reg05->refined.BAND = 0;
508 else if (minimum_frequency < 1800)
509 this->currentAMBand = reg05->refined.BAND = 1;
511 this->currentAMBand = reg05->refined.BAND = 2;
513 this->currentAMSpace = reg05->refined.SPACE = am_space;
515 setRegister(
REG05, reg05->raw);
517 this->setFrequency(default_frequency);
529 reg02->refined.SEEK = 0;
530 setRegister(
REG02,reg02->raw);
532 reg03->refined.TUNE = 1;
533 reg03->refined.CHAN = channel;
535 setRegister(
REG03,reg03->raw);
539 this->currentChannel = channel;
552 if (
this->currentMode ==
MODE_FM) {
553 channel = (frequency -
this->fmStartBand[
this->currentFMBand]) /
this->fmSpace[
this->currentFMSpace];
556 channel = (frequency -
this->amStartBand[
this->currentAMBand]) /
this->amSpace[
this->currentAMSpace];
559 this->setChannel(channel);
569 this->currentFrequency +=
this->currentStep;
571 if (
this->currentFrequency >
this->maximumFrequency )
572 this->currentFrequency =
this->minimumFrequency;
574 setFrequency(
this->currentFrequency);
584 this->currentFrequency -=
this->currentStep;
586 if (
this->currentFrequency <
this->minimumFrequency)
587 this->currentFrequency =
this->maximumFrequency;
589 setFrequency(
this->currentFrequency);
599 return this->currentFrequency;
609 return this->currentChannel;
621 return reg0b->refined.READCHAN;
634 return getRealChannel() *
this->amSpace[
this->currentAMSpace] +
this->amStartBand[
this->currentAMBand];
636 return getRealChannel() *
this->fmSpace[
this->currentFMSpace] +
this->fmStartBand[
this->currentFMBand];
676 void BK108X::
seekSoftware(uint8_t seek_mode, uint8_t direction,
void (*showFunc)())
678 long max_time = millis();
683 reg03->refined.TUNE = 0;
684 setRegister(
REG03, reg03->raw);
689 reg02->refined.SKMODE = seek_mode;
690 reg02->refined.SEEKUP = direction;
691 reg02->refined.SEEK = 1;
692 setRegister(
REG02, reg02->raw);
694 if (showFunc != NULL)
696 this->currentFrequency = getRealFrequency();
700 }
while ( (!reg0a->refined.STC && reg0a->refined.SF_BL) && (millis() - max_time) <
MAX_SEEK_TIME);
702 reg02->refined.SEEK = 0;
703 reg03->refined.TUNE = 0;
704 setRegister(
REG02, reg02->raw);
705 setRegister(
REG03, reg03->raw);
707 this->currentFrequency = getRealFrequency();
722 reg03->refined.TUNE = 0;
723 setRegister(
REG03, reg03->raw);
725 reg02->refined.SKMODE = seek_mode;
726 reg02->refined.SEEKUP = direction;
727 reg02->refined.SKAFCRL = 1;
731 reg02->refined.SEEK = 1;
732 setRegister(
REG02, reg02->raw);
734 while (reg0a->refined.STC == 0 )
739 }
while ( reg0a->refined.SF_BL != 0 );
741 reg02->refined.SEEK = 0;
742 setRegister(
REG02, reg02->raw);
745 this->setChannel(
this->getRealChannel());
746 this->currentFrequency = getRealFrequency();
757 reg05->refined.SEEKTH = rssiValue;
758 setRegister(
REG05,reg05->raw);
760 reg06->refined.SKSNR = snrValue;
761 setRegister(
REG06,reg06->raw);
782 if (
this->currentMode ==
MODE_AM )
783 this->currentAMBand = band;
785 this->currentFMBand = band;
787 reg05->refined.BAND = band;
788 setRegister(
REG05,reg05->raw);
807 if (
this->currentMode ==
MODE_AM )
808 this->currentAMSpace = space;
810 this->currentFMSpace = space;
812 reg05->refined.SPACE = space;
813 setRegister(
REG05, reg05->raw);
825 return reg0a->refined.RSSI;
837 return reg09->refined.SNR;
848 reg02->refined.DSMUTE = !value;
849 setRegister(
REG02,reg02->raw);
869 reg06->refined.SMUTER = value;
870 setRegister(
REG06,reg06->raw);
888 reg06->refined.SMUTEA = value;
889 setRegister(
REG06, reg06->raw);
902 reg14->refined.RSSIMTH = rssi;
903 reg14->refined.SNRMTH = snr;
904 setRegister(
REG14,reg14->raw);
914 reg14->refined.SKMUTE = value;
915 setRegister(
REG14, reg14->raw);
925 reg14->refined.AFCMUTE = value;
926 setRegister(
REG14, reg14->raw);
950 reg02->refined.MUTEL = left;
951 reg02->refined.MUTER = right;
952 setRegister(
REG02, reg02->raw);
974 reg02->refined.MONO = value;
975 reg02->refined.STEREO = !value;
976 setRegister(
REG02,reg02->raw);
989 return reg0a->refined.ST;
1000 if ( value > 31)
return;
1001 this->currentVolume = value;
1003 reg05->refined.VOLUME = value;
1005 setRegister(
REG05,reg05->raw);
1016 return this->currentVolume;
1026 if (
this->currentVolume < 31)
1028 this->currentVolume++;
1029 setVolume(
this->currentVolume);
1040 if (
this->currentVolume > 0)
1042 this->currentVolume--;
1043 setVolume(
this->currentVolume);
1086 reg04->refined.RDSEN = value;
1087 setRegister(
REG04,reg04->raw);
1104 return reg0a->refined.RDSR;
char * getRdsText(void)
Gets the RDS Text when the message is of the Group Type 2 version A.
void setFM(uint16_t minimum_frequency, uint16_t maximum_frequency, uint16_t default_frequency, uint16_t step)
Sets the receiver to FM mode.
void i2cInit(int pin_sdio, int pin_sclk)
Sets the MCU pins connected to the I2C bus.
uint16_t readRegister(uint8_t reg)
Gets an array of values from a BK108X given register.
void setBand(uint8_t band=1)
Sets the current band for AM or FM
bool isStereo()
Checks stereo / mono status.
uint16_t getChannel()
Gets the current channel.
char * getRdsTime()
Gets the RDS time and date when the Group type is 4.
char * getRdsText2A(void)
Gets the Text processed for the 2A group.
uint8_t i2cReceiveAck()
Gets Acknowledge (ACK)
void i2cAck()
Sends Acknowledge (ACK)
void setAudioMute(bool value)
Sets the Mute true or false.
uint16_t getDeviceId()
Returns the Device Indentifiction.
int getSnr()
Gets the current SNR.
void i2cBeginTransaction()
Starts the I2C bus transaction
void powerDown()
Powers the receiver off.
void getNext4Block(char *c)
Process data received from group 2A.
void seekHardware(uint8_t seek_mode, uint8_t direction)
Seeks a station via hardware functionality.
uint16_t getRealFrequency()
Gets the frequency based on READCHAN register (0x0B)
void setFrequency(uint16_t frequency)
Sets the FM frequency.
int getRssi()
Gets the current Rssi.
uint8_t getRdsVersionCode(void)
Gets the version code (extracted from the Block B)
void setSeekThreshold(uint8_t rssiValue, uint8_t snrValue)
Sets RSSI and SNR Seek Threshold.
char * getRdsText0A(void)
Gets the station name and other messages.
uint8_t i2cReadByte()
Gets a Byte from the slave device.
void setSpace(uint8_t space=0)
Sets the Space channel for AM or FM.
uint16_t getRealChannel()
Gets the current channel stored in register 0x0B.
bool getRdsSync()
Get the Rds Sync.
void setSoftmuteAttenuation(uint8_t value)
Sets Softmute Attenuation.
uint16_t getChipId()
Returns the Chip Indentifiction.
uint16_t getRegister(uint8_t reg)
Gets a givens current register content of the device.
void setChannel(uint16_t channel)
Sets the channel.
void setMuteThreshold(uint8_t rssi, uint8_t snr)
Set the Mute Threshold based on RSSI and SNR.
void setAfcMute(bool value)
Disable or Enable soft mute when AFCRL is high.
void writeRegister(uint8_t reg, uint16_t vakue)
Sends an array of values to a BK108X given register.
void setAudioMute(bool left, bool right)
Sets the Mute true or false.
void setVolumeDown()
Decrements the audio volume.
void seekSoftware(uint8_t seek_mode, uint8_t direction, void(*showFunc)()=NULL)
Seeks a station via Software.
void waitAndFinishTune()
Wait STC (Seek/Tune Complete) status becomes 0.
void getNext2Block(char *c)
Process data received from group 2B.
void setSoftmute(bool value)
Sets the Softmute true or false.
void getRdsStatus()
Gets the RDS registers information.
uint16_t getFrequency()
Gets the current frequency.
void setFrequencyUp()
Increments the current frequency.
void setSoftmuteAttack(uint8_t value)
Sets Softmute Attack/Recover Rate.
void reset()
Resets the device.
uint8_t getRdsProgramType(void)
Returns the Program Type (extracted from the Block B)
void setVolume(uint8_t value)
Sets the audio volume level.
uint8_t getRdsFlagAB(void)
Returns the current Text Flag A/B
void powerUp()
Powers the receiver on.
uint8_t getVolume()
Gets the current audio volume level.
void setup(int sda_pin, int sclk_pin, int rdsInterruptPin=-1, int seekInterruptPin=-1, uint8_t oscillator_type=OSCILLATOR_TYPE_CRYSTAL)
Starts the device.
void setFrequencyDown()
Decrements the current frequency.
void setSeekMute(bool value)
Disable or Enable soft mute when seeking.
void i2cNack()
Sends Not Acknowledge (ACK)
void setRdsMode(uint8_t rds_mode=0)
Sets the Rds Mode Standard or Verbose.
bk_reg0a getStatus()
Gets the current status (register 0x0A) content.
uint16_t getRdsGroupType()
Return the group type.
char * getRdsText2B(void)
Gets the Text processed for the 2B group.
void setAM(uint16_t minimum_frequency, uint16_t maximum_frequency, uint16_t default_frequency, uint16_t step, uint16_t am_space=0)
Sets the receiver to AM mode.
void setMono(bool value)
Sets the Mono true or false (stereo)
void i2cEndTransaction()
Finish the I2C bus transaction.
void setI2C(uint8_t i2c_addr=I2C_DEVICE_ADDR)
Sets I2C bus address.
void setRds(bool value)
Sets the RDS operation.
void setVolumeUp()
Increments the audio volume.
void setAgc(bool value)
Sets the AGC enable or disable.
void setRegister(uint8_t reg, uint16_t value)
Sets a given value to the device registers.
void i2cWriteByte(uint8_t data)
Sends a Byte to the slave device.
bool getRdsReady()
Returns true if RDS Ready.