summaryrefslogtreecommitdiffstats
path: root/plugin/unicens-output/ucs2-vol/src/device_value.cpp
blob: 4031778d9ef80164a38d6de8d7bce6376f497542 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * libmostvolume example
 *
 * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You may also obtain this software under a propriety license from Microchip.
 * Please contact Microchip for further information.
 *
 */
#include <stddef.h>
#include "device_value.h"
#include "setup.h"

#define MUTE_VALUE      0x03FFU
#define MUTE_VALUE_HB   0x03U
#define MUTE_VALUE_LB   0xFFU

#define CONTROL_MASTER  0x07U
#define CONTROL_CH_1    0x08U
#define CONTROL_CH_2    0x09U

CDeviceValue::CDeviceValue(uint16_t address, DeviceValueType type, uint16_t key, bool is_i2c)
{
    this->_is_available = false;
    this->_is_busy = false;
    this->_address = address;
    this->_is_i2c = is_i2c;
    this->_target_value = 0x01u;
    this->_actual_value = 0x01u;

    this->_result_fptr = NULL;
    this->_result_user_ptr = NULL;

    this->_type = type;
    this->_key = key;

    _tx_payload[0] = CONTROL_MASTER;// 7: master, 8: channel 1, 9: Channel 2
    _tx_payload[1] = MUTE_VALUE_HB; //HB:Volume
    _tx_payload[2] = MUTE_VALUE_LB; //LB:Volume

    if (type == DEVICE_VAL_FIBERDYNE_MASTER)
        _tx_payload_sz = 2u;
    else
        _tx_payload_sz = 3u;
}

CDeviceValue::~CDeviceValue()
{
}

void CDeviceValue::ApplyMostValue(uint8_t value, DeviceValueType type, uint8_t tx_payload[])
{
    uint16_t tmp = MUTE_VALUE;

    if (type == DEVICE_VAL_FIBERDYNE_MASTER)
    {
        tx_payload[0] = 0x00U;
        tx_payload[1] = value;
        
        return;
    }   

    switch (type)
    {
        case DEVICE_VAL_LEFT:
            tmp = (uint16_t)(0x80U + 0x37FU - (0x37FU * ((int32_t)value) / (0xFFU)));
            //tmp = 0x3FF - (0x3FF * ((int32_t)value) / (0xFF));
            //tmp = 0x100 + 0x2FF - (0x2FF * ((int32_t)value) / (0xFF));
            tx_payload[0] = CONTROL_CH_1;
            break;
        case DEVICE_VAL_RIGHT:
            tmp = (uint16_t)(0x80U + 0x37FU - (0x37FU * ((int32_t)value) / (0xFFU)));
            //tmp = 0x3FF - (0x3FF * ((int32_t)value) / (0xFF));
            //tmp = 0x100 + 0x2FF - (0x2FF * ((int32_t)value) / (0xFF));
            tx_payload[0] = CONTROL_CH_2;
            break;
        default:
            /*std::cerr << "CDeviceValue::ApplyMostValue() error matching incorrect" << std::endl;*/
        case DEVICE_VAL_MASTER:
            tmp = (uint16_t)(0x100U + 0x2FFU - (0x2FFU * ((int32_t)value) / (0xFFU)));
            tx_payload[0] = CONTROL_MASTER;
            break;
    }

    tx_payload[1] = (uint8_t)((tmp >> 8U) & (uint16_t)0xFFU); //HB:Volume
    tx_payload[2] = (uint8_t)(tmp  & (uint16_t)0xFFU); //LB:Volume
}

// returns true if target is not actual value
bool CDeviceValue::RequiresUpdate()
{
    if (this->_is_available && !this->_is_busy && (this->_target_value != this->_actual_value))
    {
        return true;
    }

    return false;
}

bool CDeviceValue::FireControlMessage(lib_most_volume_sendmessage_cb_t sendmsg_fptr)
{
    ApplyMostValue(this->_target_value, _type, _tx_payload);

    if (this->_is_available && !this->_is_busy)
    {
        sendmsg_fptr(this->_address, 0x100U, _tx_payload, _tx_payload_sz);
        
        this->_actual_value = this->_target_value;
    }

    return true;
}

bool CDeviceValue::FireUpdateMessage(lib_most_volume_writei2c_cb_t writei2c_fptr,
                                     lib_most_volume_writei2c_result_cb_t result_fptr,
                                     void *result_user_ptr)
{
    int ret = -1;
    ApplyMostValue(this->_target_value, _type, _tx_payload);

    if (this->_is_available && !this->_is_busy)
    {
        ret = writei2c_fptr(this->_address, &_tx_payload[0], _tx_payload_sz,
                            &OnI2cResult,
                            this);

        if (ret == 0)
        {
            this->_transmitted_value = this->_target_value;
            this->_is_busy = true;
            this->_result_fptr = result_fptr;
            this->_result_user_ptr = result_user_ptr;
            return true;
        }
    }

    return false;
}

void CDeviceValue::HandleI2cResult(uint8_t result)
{
    if (result == 0)
    {
        /* transmission succeeded - now apply transmitted value */
        this->_actual_value = this->_transmitted_value;
    }

    if (this->_result_fptr)
    {
        /* notify container */
        this->_result_fptr(result, this->_result_user_ptr);
    }

    this->_result_fptr = NULL;
    this->_result_user_ptr = NULL;
    this->_is_busy = false;
}

void CDeviceValue::OnI2cResult(uint8_t result, void *obj_ptr)
{
    ((CDeviceValue*)obj_ptr)->HandleI2cResult(result);
}