90 lines
1.8 KiB
C
Raw Normal View History

2021-01-29 12:31:58 +01:00
#pragma once
//
// FILE: M62429.h
// AUTHOR: Rob Tillaart
// PURPOSE: Arduino library for M62429 volume control IC
2022-11-15 10:47:38 +01:00
// VERSION: 0.3.6
2021-01-29 12:31:58 +01:00
// URL: https://github.com/RobTillaart/M62429
2021-05-28 13:36:28 +02:00
2021-01-29 12:31:58 +01:00
#include "Arduino.h"
2021-05-28 13:36:28 +02:00
2022-11-15 10:47:38 +01:00
#define M62429_VERSION (F("0.3.6"))
2021-05-28 13:36:28 +02:00
2021-01-29 12:31:58 +01:00
2021-12-21 12:50:11 +01:00
// minimum pulse width CLOCK = 1.6 us (datasheet);
2021-01-29 12:31:58 +01:00
// digitalWrite takes enough time on UNO / AVR so clock_delay == 0
// Note that if clock pulses are long enough the data pulses are too.
#ifndef M62429_CLOCK_DELAY
#ifdef __AVR__
2022-01-05 16:44:57 +01:00
#define M62429_CLOCK_DELAY 0
2021-01-29 12:31:58 +01:00
#else
2022-01-05 16:44:57 +01:00
#define M62429_CLOCK_DELAY 2
2021-01-29 12:31:58 +01:00
#endif
#endif
2021-05-28 13:36:28 +02:00
2021-01-29 12:31:58 +01:00
// ERROR CODES
2022-01-05 16:44:57 +01:00
#define M62429_OK 0
#define M62429_MUTED -1
#define M62429_CHANNEL_ERROR -10
2021-01-29 12:31:58 +01:00
2022-02-12 09:23:23 +01:00
// CHANNELS
2022-02-12 14:33:18 +01:00
#define M62429_LEFT 0
#define M62429_RIGHT 1
#define M62429_BOTH 2
2022-02-12 09:23:23 +01:00
2021-01-29 12:31:58 +01:00
class M62429
{
public:
void begin(uint8_t dataPin, uint8_t clockPin);
// channel = { 0, 1 }
int getVolume(uint8_t channel);
// channel = { 0, 1, 2 = both; volume = {0 .. 255 }
int setVolume(uint8_t channel, uint8_t volume);
2022-02-12 14:33:18 +01:00
// increment /decrement until min/max = 0..255
2022-02-13 15:58:42 +01:00
int incr(uint8_t channel = 2);
int decr(uint8_t channel = 2);
2022-02-12 14:33:18 +01:00
int average();
2021-01-29 12:31:58 +01:00
void muteOn();
void muteOff();
bool isMuted() { return _muted; };
2022-02-12 09:23:23 +01:00
2021-01-29 12:31:58 +01:00
private:
uint8_t _vol[2] = { 0, 0 };
uint8_t _data = 0;
uint8_t _clock = 0;
bool _muted = false;
2022-02-12 09:23:23 +01:00
void _setAttn(uint8_t channel, uint8_t attn);
2021-01-29 12:31:58 +01:00
};
2022-02-21 19:21:27 +01:00
class M62429_RAW
{
public:
void begin(uint8_t dataPin, uint8_t clockPin);
// channel = { 0, 1 }
int getAttn(uint8_t channel);
// channel = { 0, 1, 2 = both; attn = {0 .. 87 }
void setAttn(uint8_t channel, uint8_t attn);
private:
uint8_t _attn[2] = { 0, 0 };
uint8_t _data = 0;
uint8_t _clock = 0;
};
2021-01-29 12:31:58 +01:00
// -- END OF FILE --
2022-01-05 16:44:57 +01:00