2 * libmostvolume example
4 * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * You may also obtain this software under a propriety license from Microchip.
20 * Please contact Microchip for further information.
24 #include "device_value.h"
25 #include "callbacks.h"
28 /*#include <iostream>*/
30 #define MUTE_VALUE 0x03FFU
31 #define MUTE_VALUE_HB 0x03U
32 #define MUTE_VALUE_LB 0xFFU
34 #define CONTROL_MASTER 0x07U
35 #define CONTROL_CH_1 0x08U
36 #define CONTROL_CH_2 0x09U
38 CDeviceValue::CDeviceValue(uint16_t address, DeviceValueType type, uint16_t key)
40 this->_is_initial = true;
41 this->_address = address;
42 this->_target_value = 0x01u;
43 this->_actual_value = 0x01u;
48 _tx_payload[0] = CONTROL_MASTER;// 7: master, 8: channel 1, 9: Channel 2
49 _tx_payload[1] = MUTE_VALUE_HB; //HB:Volume
50 _tx_payload[2] = MUTE_VALUE_LB; //LB:Volume
54 CDeviceValue::~CDeviceValue()
58 void CDeviceValue::ApplyMostValue(uint8_t value, DeviceValueType type, uint8_t tx_payload[])
60 uint16_t tmp = MUTE_VALUE;
65 tmp = 0x80U + 0x37FU - (0x37FU * ((int32_t)value) / (0xFFU));
66 //tmp = 0x3FF - (0x3FF * ((int32_t)value) / (0xFF));
67 //tmp = 0x100 + 0x2FF - (0x2FF * ((int32_t)value) / (0xFF));
68 tx_payload[0] = CONTROL_CH_1;
70 case DEVICE_VAL_RIGHT:
71 tmp = 0x80U + 0x37FU - (0x37FU * ((int32_t)value) / (0xFFU));
72 //tmp = 0x3FF - (0x3FF * ((int32_t)value) / (0xFF));
73 //tmp = 0x100 + 0x2FF - (0x2FF * ((int32_t)value) / (0xFF));
74 tx_payload[0] = CONTROL_CH_2;
77 /*std::cerr << "CDeviceValue::ApplyMostValue() error matching incorrect" << std::endl;*/
78 case DEVICE_VAL_MASTER:
79 tmp = 0x100U + 0x2FFU - (0x2FFU * ((int32_t)value) / (0xFFU));
80 tx_payload[0] = CONTROL_MASTER;
84 tx_payload[1] = (uint8_t)((tmp >> 8U) & (uint16_t)0xFFU); //HB:Volume
85 tx_payload[2] = (uint8_t)(tmp & (uint16_t)0xFFU); //LB:Volume
88 // returns true if target is not actual value
89 bool CDeviceValue::RequiresUpdate()
91 if (this->_target_value != this->_actual_value)
99 bool CDeviceValue::FireUpdateMessage(void)
102 ApplyMostValue(this->_target_value, _type, _tx_payload);
104 ret = Ucs_I2c_WritePort( CSetup::GetInstance()->RetrieveUnicensInst(),
106 0x0F00u, /* i2c port handle */
107 UCS_I2C_DEFAULT_MODE, /* 0 */
108 0u, /* block count */
109 0x2Au, /* i2c slave address */
110 0x03E8u, /* timeout 1000 milliseconds */
111 _tx_payload_sz, /* data length */
112 &_tx_payload[0], /* data pointer */
113 &Clb_OnWriteI2CPortResult
116 if (ret == UCS_RET_SUCCESS)
118 // Clb_RegisterI2CResultCB(OnI2cResult, this);
119 // mark value as set!
120 this->_actual_value = this->_target_value;