Setting a single bit in a register of the SGTL5000

DIYLAB

Well-known member
Hi all,

how can I set a single bit in a register of the SGTL5000 from the audio shield without modifying the control_sgtl5000.h?
"write" and "modify" are protected functions.
 
Copy the code? Its just some Wire library transactions.

To quote the licence:
" * 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, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*"
 
Yes, that's fine, but such an overhead for one bit?
There must be a simple solution without touching the libs!?
At the moment I already use the two functions by making them public, but I want to leave the audiolib as it is.

Maybe I am the only one who wants to set a bit? :D
 
Which bit? The correct way would be to have a function for that, in the library. Code can be changed..
Or perhaps it possible to use inheritance.

Overhead: I don't see a dramatic overhead here.. the times where it was needed to save program space are over, since many years.
Of course, it could be avoided. But there is no urgend need.. you'll get nothing back for saving 100 bytes or such..
 
That would be useful for the library.
I'll do a pullrequest :)
With a bit luck it will be there in one of the next versions.
 
The library does not need to be changed to be able to modify the various sgtl5000 registers. Since the read, write and modify methods are designated "protected" and not "private" in the library, they can be accessed by doing a class inheritance.
 
Here is the code pieces I used to get at the sgtl5000 registers so I could mute the DACs separately. It uses the codec library without changes.

Code:
#ifndef PLATFORM_H
#define PLATFORM_H
#include <Arduino.h>
#include <Audio.h>

class AudioCodecControl: public AudioControlSGTL5000 {

public:
inline void mutesDAC(bool _muteL, bool _muteR) __attribute__((always_inline))
	{
		uint16_t mute_bits = ((uint16_t)_muteL<<2) + ((uint16_t)_muteR<<3); // puts mute bits where they need to go in PLATFORM_AUDIO_ADCDAC_CTRL register
		modify(PLATFORM_AUDIO_ADCDAC_CTRL, mute_bits, 0x000C); // modify performs write(read(reg)&(~0x000C))|mute_bits;
	}
};

class Platform
{
    AudioCodecControl sgtl5000_1; // Codec Control Class
   
   ....................other stuff
	
void Platform::mutesAudioChip(bool _muteL, bool _muteR)
{
    sgtl5000_1.mutesDAC(_muteL, _muteR);
}
extern Platform PC;
#endif /* PLATFORM_H */

I hope this helps.
 
Sorry, I just noticed two omissions. Here is the code with a few added lines:

Code:
#ifndef PLATFORM_H
#define PLATFORM_H
#include <Arduino.h>
#include <Audio.h>

#define PLATFORM_AUDIO_ADCDAC_CTRL 0x000E

class AudioCodecControl: public AudioControlSGTL5000 {

public:
inline void mutesDAC(bool _muteL, bool _muteR) __attribute__((always_inline))
	{
		uint16_t mute_bits = ((uint16_t)_muteL<<2) + ((uint16_t)_muteR<<3); // puts mute bits where they need to go in PLATFORM_AUDIO_ADCDAC_CTRL register
		modify(PLATFORM_AUDIO_ADCDAC_CTRL, mute_bits, 0x000C); // modify performs write(read(reg)&(~0x000C))|mute_bits;
	}
};

class Platform
{
    AudioCodecControl sgtl5000_1; // Codec Control Class
public:
    
inline void mutesAudioChip(bool _muteL, bool _muteR) __attribute__((always_inline));
   
....................other stuff
	
void Platform::mutesAudioChip(bool _muteL, bool _muteR)
{
    sgtl5000_1.mutesDAC(_muteL, _muteR);
}
extern Platform PC;
#endif /* PLATFORM_H */
 
Back
Top