makes GPIO_PIN_RST optional for the sx1276
[ExpressLRS.git] / src / lib / DAC / DAC.cpp
blob43eb4474a694998181e350eb2c7a49fbfa8a2cc2
2 #include "DAC.h"
4 #if defined(POWER_OUTPUT_DAC)
5 #include "helpers.h"
6 #include "logging.h"
7 #include <Wire.h>
9 #ifndef DAC_REF_VCC
10 #define DAC_REF_VCC 3300
11 #endif
13 void DAC::init()
15 DBGLN("Init DAC Driver");
17 Wire.setSDA(GPIO_PIN_SDA); // set is needed or it wont work :/
18 Wire.setSCL(GPIO_PIN_SCL);
19 Wire.begin();
20 m_state = UNKNOWN;
23 void DAC::standby()
25 if (m_state != STANDBY)
27 Wire.beginTransmission(POWER_OUTPUT_DAC);
28 Wire.write(0x00);
29 Wire.write(0x00);
30 Wire.endTransmission();
31 m_state = STANDBY;
35 void DAC::resume()
37 if (m_state != RUNNING)
39 DAC::setVoltageRegDirect(m_currVoltageRegVal);
43 void DAC::setVoltageMV(uint32_t voltsMV)
45 uint8_t ScaledVolts = map(voltsMV, 0, DAC_REF_VCC, 0, 255);
46 setVoltageRegDirect(ScaledVolts);
47 m_currVoltageMV = voltsMV;
48 DBGLN("DAC Voltage %dmV", m_currVoltageMV);
51 void DAC::setVoltageRegDirect(uint8_t voltReg)
53 m_currVoltageRegVal = voltReg;
54 uint8_t RegH = ((voltReg & 0b11110000) >> 4) + (0b0000 << 4);
55 uint8_t RegL = (voltReg & 0b00001111) << 4;
57 Wire.beginTransmission(POWER_OUTPUT_DAC);
58 Wire.write(RegH);
59 Wire.write(RegL);
60 Wire.endTransmission();
63 void DAC::setPower(int16_t milliVolts)
65 DAC::setVoltageMV(milliVolts);
68 DAC TxDAC;
70 #endif // defined(POWER_OUTPUT_DAC)