Skip to content

Commit

Permalink
initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
qhris committed Dec 27, 2016
0 parents commit 6615910
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)
Copyright (c) 2016 Christoffer Hjalmarsson

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 and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
142 changes: 142 additions & 0 deletions Muses72320.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
The MIT License (MIT)
Copyright (c) 2016 Christoffer Hjalmarsson
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 and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "Muses72320.h"
#include <SPI.h>

typedef Muses72320 Self;

// pass through some types into global scope.
using data_t = Self::data_t;
using volume_t = Self::volume_t;

// control select addresses, chip address (low 4) ignored.
static const data_t s_control_attenuation_l = 0b00000000;
static const data_t s_control_attenuation_r = 0b00100000;
static const data_t s_control_gain_l = 0b00010000;
static const data_t s_control_gain_r = 0b00110000;
static const data_t s_control_states = 0b01000000;

// control state bits.
static const data_t s_state_bit_zero_crossing = 5;
static const data_t s_state_bit_gain = 6;
static const data_t s_state_bit_attenuation = 7;

static const int s_slave_select_pin = 10;
static const SPISettings s_muses_spi_settings(250000, MSBFIRST, SPI_MODE2);

static inline data_t volume_to_attenuation(volume_t volume)
{
// volume to attenuation data conversion:
// #=====================================#
// | 0.0 dB | in: [ 0] -> 0b00010000 |
// | -111.5 dB | in: [223] -> 0b11101111 |
// #=====================================#
return static_cast<data_t>(min(-volume, 223) + 0x10);
}

static inline data_t volume_to_gain(volume_t gain)
{
// volume to gain data conversion:
// #===================================#
// | 0 dB | in: [ 0] -> 0b00000000 |
// | +31.5 dB | in: [63] -> 0b01111111 |
// #===================================#
return static_cast<data_t>(min(gain, 63));
}

Self::Muses72320(address_t chip_address) :
chip_address(chip_address & 0b0111),
states(0)
{ }

void Self::begin()
{
pinMode(s_slave_select_pin, OUTPUT);
SPI.begin();
}

void Self::setVolume(volume_t lch, volume_t rch)
{
if (bitRead(states, s_state_bit_attenuation)) {
// interconnected left and right channels.
transfer(s_control_attenuation_l, volume_to_attenuation(lch));
} else {
// independent left and right channels.
transfer(s_control_attenuation_l, volume_to_attenuation(lch));
transfer(s_control_attenuation_r, volume_to_attenuation(rch));
}
}

void Self::setGain(volume_t lch, volume_t rch)
{
if (bitRead(states, s_state_bit_gain)) {
// interconnected left and right channels.
transfer(s_control_gain_l, volume_to_gain(lch));
} else {
// independent left and right channels.
transfer(s_control_gain_l, volume_to_gain(lch));
transfer(s_control_gain_r, volume_to_gain(rch));
}
}

void Self::mute()
{
if (bitRead(states, s_state_bit_attenuation)) {
transfer(s_control_attenuation_l, 0);
} else {
transfer(s_control_attenuation_l, 0);
transfer(s_control_attenuation_r, 0);
}
}

void Self::setZeroCrossing(bool enabled)
{
// 0 is enabled, 1 is disabled.
bitWrite(states, s_state_bit_zero_crossing, !enabled);
transfer(s_control_states, states);
}

void Self::setAttenuationLink(bool enabled)
{
// 1 is enabled, 0 is disabled.
bitWrite(states, s_state_bit_attenuation, enabled);
transfer(s_control_states, states);
}

void Self::setGainLink(bool enabled)
{
// 1 is enabled, 0 is disabled.
bitWrite(states, s_state_bit_gain, enabled);
transfer(s_control_states, states);
}

void Self::transfer(address_t address, data_t data)
{
SPI.beginTransaction(s_muses_spi_settings);
digitalWrite(s_slave_select_pin, LOW);
SPI.transfer(data);
SPI.transfer(address | chip_address);
digitalWrite(s_slave_select_pin, HIGH);
SPI.endTransaction();
}
78 changes: 78 additions & 0 deletions Muses72320.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
The MIT License (MIT)
Copyright (c) 2016 Christoffer Hjalmarsson
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 and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef INCLUDED_MUSES_72320
#define INCLUDED_MUSES_72320

#include <Arduino.h>

class Muses72320
{
public:
// contextual data types.
typedef uint8_t pin_t;
typedef uint8_t data_t;
typedef int16_t volume_t;
typedef uint8_t address_t;

// specify a connection over an address using three output pins.
Muses72320(address_t chip_address);

// set the pins in their correct states.
void begin();

// set the volume using the following formula:
// (-0.5 * volume) db
// audio level goes from [-111.5, 0.0] dB
// input goes from -223 to 0.
void setVolume(volume_t left, volume_t right);
inline void setVolume(volume_t volume) { setVolume(volume, volume); }

// gain is constrained to [0, 31.5].
// input goes from 0 to 63.
void setGain(volume_t left, volume_t right);
inline void setGain(volume_t volume) { setGain(volume, volume); }

void mute();

// enable or disable zero crossing.
// enabling zero crossing only works if the zero crossing terminal is set low.
void setZeroCrossing(bool enabled);
void setAttenuationLink(bool enabled);
void setGainLink(bool enabled);

private:
void transfer(address_t address, data_t data);

private:
// for multiple chips on the same bus line.
address_t chip_address;

// muses state bits:
// 7: link l/r attenuation
// 6: link l/r gain
// 5: disable zero crossing
// [4-0]: not used
data_t states;
};

#endif // INCLUDED_MUSES_72320
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Muses 72320

Arduino library for communicating with the Muses 72320 audio chip.
The data sheets can be found [here](http://www.njr.com/semicon/PDF/MUSES72320_E.pdf) (pdf).

## Download

Download the latest release over at the [Releases](https://github.com/qhris/Muses72320/releases) page.

## Example

```c++
#include <Muses72320.h>

// The address wired into the muses chip (usually 0).
static const byte MUSES_ADDRESS = 0;

static Muses72320 Muses(MUSES_ADDRESS);
static Muses72320::volume_t CurrentVolume = -20;

void setup()
{
// Initialize muses (SPI, pin modes)...
Muses.begin();

// Muses initially starts in a muted state, set a volume to enable sound.
Muses.setVolume(CurrentVolume);

// These are the default states and could be removed...
Muses.setZeroCrossing(true); // Enable/Disable zero crossing.
Muses.setAttenuationLink(false); // Left channel controls both L/R gain channel.
Muses.setGainLink(false); // Left channel controls both L/R attenuation channel.
}

void loop()
{
CurrentVolume -= 1;
if (CurrentVolume < -223)
{
CurrentVolume = 0;
}

Muses.setVolume(CurrentVolume);
// Equivalent to 'Muses.setVolume(CurrentVolume, CurrentVolume)' for L/R ch.

delay(10);
}

```
## Problems
Please post any problems on the [Issues](https://github.com/qhris/Muses72320/issues) page.
## License
Please read over the LICENSE file included in the project.
16 changes: 16 additions & 0 deletions library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "Muses72320",
"keywords": "audio",
"description": "Arduino libary for the audio chip Muses 72320",
"repository": {
"type": "git",
"url": "https://github.com/qhris/Muses72320"
},
"version": "0.1",
"exclude": [
"*.png",
"*.zip"
],
"frameworks": "arduino",
"platforms": "*"
}
9 changes: 9 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=Muses72320
version=0.1
author=Christoffer Hjalmarsson
maintainer=Christoffer Hjalmarsson
sentence=Library for the audio chip MUSES 72320
paragraph=Library for the audio chip MUSES 72320
category=Device Control
url=https://github.com/qhris/Muses72320
architectures=*

0 comments on commit 6615910

Please sign in to comment.