A feature complete driver for the MAX9723 headphone amplifier from Maxim.
Dependents: MAX9723_HelloWorld
MAX9723.cpp@0:99db25d6f38d, 2014-05-30 (annotated)
- Committer:
- neilt6
- Date:
- Fri May 30 19:46:37 2014 +0000
- Revision:
- 0:99db25d6f38d
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
neilt6 | 0:99db25d6f38d | 1 | /* MAX9723 Driver Library |
neilt6 | 0:99db25d6f38d | 2 | * Copyright (c) 2014 Neil Thiessen |
neilt6 | 0:99db25d6f38d | 3 | * |
neilt6 | 0:99db25d6f38d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
neilt6 | 0:99db25d6f38d | 5 | * you may not use this file except in compliance with the License. |
neilt6 | 0:99db25d6f38d | 6 | * You may obtain a copy of the License at |
neilt6 | 0:99db25d6f38d | 7 | * |
neilt6 | 0:99db25d6f38d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
neilt6 | 0:99db25d6f38d | 9 | * |
neilt6 | 0:99db25d6f38d | 10 | * Unless required by applicable law or agreed to in writing, software |
neilt6 | 0:99db25d6f38d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
neilt6 | 0:99db25d6f38d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
neilt6 | 0:99db25d6f38d | 13 | * See the License for the specific language governing permissions and |
neilt6 | 0:99db25d6f38d | 14 | * limitations under the License. |
neilt6 | 0:99db25d6f38d | 15 | */ |
neilt6 | 0:99db25d6f38d | 16 | |
neilt6 | 0:99db25d6f38d | 17 | #include "MAX9723.h" |
neilt6 | 0:99db25d6f38d | 18 | |
neilt6 | 0:99db25d6f38d | 19 | MAX9723::MAX9723(PinName sda, PinName scl, Address addr, int hz) : m_I2C(sda, scl), m_ADDR((int)addr) |
neilt6 | 0:99db25d6f38d | 20 | { |
neilt6 | 0:99db25d6f38d | 21 | //Initialize the member variables |
neilt6 | 0:99db25d6f38d | 22 | m_AmpValue = 255; |
neilt6 | 0:99db25d6f38d | 23 | |
neilt6 | 0:99db25d6f38d | 24 | //Set the I2C bus frequency |
neilt6 | 0:99db25d6f38d | 25 | m_I2C.frequency(hz); |
neilt6 | 0:99db25d6f38d | 26 | } |
neilt6 | 0:99db25d6f38d | 27 | |
neilt6 | 0:99db25d6f38d | 28 | bool MAX9723::open() |
neilt6 | 0:99db25d6f38d | 29 | { |
neilt6 | 0:99db25d6f38d | 30 | //Probe for the MAX9723 using a Zero Length Transfer |
neilt6 | 0:99db25d6f38d | 31 | if (!m_I2C.write(m_ADDR, NULL, 0)) { |
neilt6 | 0:99db25d6f38d | 32 | //Make sure the amp value variable is set to the default register value |
neilt6 | 0:99db25d6f38d | 33 | m_AmpValue = 255; |
neilt6 | 0:99db25d6f38d | 34 | |
neilt6 | 0:99db25d6f38d | 35 | //Write the default register value |
neilt6 | 0:99db25d6f38d | 36 | m_I2C.write(m_ADDR, &m_AmpValue, 1); |
neilt6 | 0:99db25d6f38d | 37 | |
neilt6 | 0:99db25d6f38d | 38 | //Return success |
neilt6 | 0:99db25d6f38d | 39 | return true; |
neilt6 | 0:99db25d6f38d | 40 | } else { |
neilt6 | 0:99db25d6f38d | 41 | //Return failure |
neilt6 | 0:99db25d6f38d | 42 | return false; |
neilt6 | 0:99db25d6f38d | 43 | } |
neilt6 | 0:99db25d6f38d | 44 | } |
neilt6 | 0:99db25d6f38d | 45 | |
neilt6 | 0:99db25d6f38d | 46 | bool MAX9723::enabled() |
neilt6 | 0:99db25d6f38d | 47 | { |
neilt6 | 0:99db25d6f38d | 48 | //Return the status of the SHUTDOWN bit |
neilt6 | 0:99db25d6f38d | 49 | return (m_AmpValue & (1 << 7)); |
neilt6 | 0:99db25d6f38d | 50 | } |
neilt6 | 0:99db25d6f38d | 51 | |
neilt6 | 0:99db25d6f38d | 52 | void MAX9723::enabled(bool enabled) |
neilt6 | 0:99db25d6f38d | 53 | { |
neilt6 | 0:99db25d6f38d | 54 | //Set or clear the SHUTDOWN bit |
neilt6 | 0:99db25d6f38d | 55 | if (enabled) |
neilt6 | 0:99db25d6f38d | 56 | m_AmpValue |= (1 << 7); |
neilt6 | 0:99db25d6f38d | 57 | else |
neilt6 | 0:99db25d6f38d | 58 | m_AmpValue &= ~(1 << 7); |
neilt6 | 0:99db25d6f38d | 59 | |
neilt6 | 0:99db25d6f38d | 60 | //Write the value back out |
neilt6 | 0:99db25d6f38d | 61 | m_I2C.write(m_ADDR, &m_AmpValue, 1); |
neilt6 | 0:99db25d6f38d | 62 | } |
neilt6 | 0:99db25d6f38d | 63 | |
neilt6 | 0:99db25d6f38d | 64 | bool MAX9723::bassMax() |
neilt6 | 0:99db25d6f38d | 65 | { |
neilt6 | 0:99db25d6f38d | 66 | //Return the status of the BassMax ENABLE bit |
neilt6 | 0:99db25d6f38d | 67 | return (m_AmpValue & (1 << 6)); |
neilt6 | 0:99db25d6f38d | 68 | } |
neilt6 | 0:99db25d6f38d | 69 | |
neilt6 | 0:99db25d6f38d | 70 | void MAX9723::bassMax(bool enabled) |
neilt6 | 0:99db25d6f38d | 71 | { |
neilt6 | 0:99db25d6f38d | 72 | //Set or clear the BassMax ENABLE bit |
neilt6 | 0:99db25d6f38d | 73 | if (enabled) |
neilt6 | 0:99db25d6f38d | 74 | m_AmpValue |= (1 << 6); |
neilt6 | 0:99db25d6f38d | 75 | else |
neilt6 | 0:99db25d6f38d | 76 | m_AmpValue &= ~(1 << 6); |
neilt6 | 0:99db25d6f38d | 77 | |
neilt6 | 0:99db25d6f38d | 78 | //Write the value back out |
neilt6 | 0:99db25d6f38d | 79 | m_I2C.write(m_ADDR, &m_AmpValue, 1); |
neilt6 | 0:99db25d6f38d | 80 | } |
neilt6 | 0:99db25d6f38d | 81 | |
neilt6 | 0:99db25d6f38d | 82 | bool MAX9723::maxGain() |
neilt6 | 0:99db25d6f38d | 83 | { |
neilt6 | 0:99db25d6f38d | 84 | //Return the status of the MAXIMUM GAIN bit |
neilt6 | 0:99db25d6f38d | 85 | return (m_AmpValue & (1 << 5)); |
neilt6 | 0:99db25d6f38d | 86 | } |
neilt6 | 0:99db25d6f38d | 87 | |
neilt6 | 0:99db25d6f38d | 88 | void MAX9723::maxGain(bool max) |
neilt6 | 0:99db25d6f38d | 89 | { |
neilt6 | 0:99db25d6f38d | 90 | //Set or clear the MAXIMUM GAIN bit |
neilt6 | 0:99db25d6f38d | 91 | if (max) |
neilt6 | 0:99db25d6f38d | 92 | m_AmpValue |= (1 << 5); |
neilt6 | 0:99db25d6f38d | 93 | else |
neilt6 | 0:99db25d6f38d | 94 | m_AmpValue &= ~(1 << 5); |
neilt6 | 0:99db25d6f38d | 95 | |
neilt6 | 0:99db25d6f38d | 96 | //Write the value back out |
neilt6 | 0:99db25d6f38d | 97 | m_I2C.write(m_ADDR, &m_AmpValue, 1); |
neilt6 | 0:99db25d6f38d | 98 | } |
neilt6 | 0:99db25d6f38d | 99 | |
neilt6 | 0:99db25d6f38d | 100 | float MAX9723::volume() |
neilt6 | 0:99db25d6f38d | 101 | { |
neilt6 | 0:99db25d6f38d | 102 | //Return the bottom 5 bits |
neilt6 | 0:99db25d6f38d | 103 | return (m_AmpValue & 0x1F) / 31.0; |
neilt6 | 0:99db25d6f38d | 104 | } |
neilt6 | 0:99db25d6f38d | 105 | |
neilt6 | 0:99db25d6f38d | 106 | void MAX9723::volume(float volume) |
neilt6 | 0:99db25d6f38d | 107 | { |
neilt6 | 0:99db25d6f38d | 108 | //Mask off the old volume |
neilt6 | 0:99db25d6f38d | 109 | m_AmpValue &= 0xE0; |
neilt6 | 0:99db25d6f38d | 110 | |
neilt6 | 0:99db25d6f38d | 111 | //Set the new volume |
neilt6 | 0:99db25d6f38d | 112 | m_AmpValue |= (char)(volume * 31.0); |
neilt6 | 0:99db25d6f38d | 113 | |
neilt6 | 0:99db25d6f38d | 114 | //Write the value back out |
neilt6 | 0:99db25d6f38d | 115 | m_I2C.write(m_ADDR, &m_AmpValue, 1); |
neilt6 | 0:99db25d6f38d | 116 | } |
neilt6 | 0:99db25d6f38d | 117 | |
neilt6 | 0:99db25d6f38d | 118 | #ifdef MBED_OPERATORS |
neilt6 | 0:99db25d6f38d | 119 | MAX9723::operator float() |
neilt6 | 0:99db25d6f38d | 120 | { |
neilt6 | 0:99db25d6f38d | 121 | //Return the current volume |
neilt6 | 0:99db25d6f38d | 122 | return volume(); |
neilt6 | 0:99db25d6f38d | 123 | } |
neilt6 | 0:99db25d6f38d | 124 | |
neilt6 | 0:99db25d6f38d | 125 | MAX9723& MAX9723::operator=(float value) |
neilt6 | 0:99db25d6f38d | 126 | { |
neilt6 | 0:99db25d6f38d | 127 | //Set the volume |
neilt6 | 0:99db25d6f38d | 128 | volume(value); |
neilt6 | 0:99db25d6f38d | 129 | return *this; |
neilt6 | 0:99db25d6f38d | 130 | } |
neilt6 | 0:99db25d6f38d | 131 | #endif |