updated app-templates
[apps/agl-service-unicens.git] / ucs2-vol / src / device_value.cpp
1 /*
2  * libmostvolume example
3  *
4  * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  * You may also obtain this software under a propriety license from Microchip.
20  * Please contact Microchip for further information.
21  *
22  */
23
24 #include "device_value.h"
25 #include "callbacks.h"
26 #include "ucs_api.h"
27 #include "setup.h"
28 /*#include <iostream>*/
29
30 #define MUTE_VALUE      0x03FFU
31 #define MUTE_VALUE_HB   0x03U
32 #define MUTE_VALUE_LB   0xFFU
33
34 #define CONTROL_MASTER  0x07U
35 #define CONTROL_CH_1    0x08U
36 #define CONTROL_CH_2    0x09U
37
38 CDeviceValue::CDeviceValue(uint16_t address, DeviceValueType type, uint16_t key)
39 {
40     this->_is_initial = true;
41     this->_address = address;
42     this->_target_value = 0x01u;
43     this->_actual_value = 0x01u;
44
45     this->_type = type;
46     this->_key = key;
47
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
51     _tx_payload_sz = 3u;
52 }
53
54 CDeviceValue::~CDeviceValue()
55 {
56 }
57
58 void CDeviceValue::ApplyMostValue(uint8_t value, DeviceValueType type, uint8_t tx_payload[])
59 {
60     uint16_t tmp = MUTE_VALUE;
61
62     switch (type)
63     {
64         case DEVICE_VAL_LEFT:
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;
69             break;
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;
75             break;
76         default:
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;
81             break;
82     }
83
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
86 }
87
88 // returns true if target is not actual value
89 bool CDeviceValue::RequiresUpdate()
90 {
91     if (this->_target_value != this->_actual_value)
92     {
93         return true;
94     }
95
96     return false;
97 }
98
99 bool CDeviceValue::FireUpdateMessage(void)
100 {
101     Ucs_Return_t ret;
102     ApplyMostValue(this->_target_value, _type, _tx_payload);
103
104     ret = Ucs_I2c_WritePort( CSetup::GetInstance()->RetrieveUnicensInst(),
105             this->_address,
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
114             );
115
116     if (ret == UCS_RET_SUCCESS)
117     {
118         // Clb_RegisterI2CResultCB(OnI2cResult, this);
119         // mark value as set!
120         this->_actual_value = this->_target_value;
121         return true;
122     }
123
124     return false;
125 }