summaryrefslogtreecommitdiffstats
path: root/ucs2-vol/src/device_container.cpp
blob: bda3b7964f6b8da4f6b1032e1b63c8c5fd64d03e (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
/*
 * 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 "device_container.h"
#include "callbacks.h"
#include "ucs_api.h"

#define DEVCONT_TIME_RETRIGGER      (uint16_t)100U
#define DEVCONT_TIME_NOW            (uint16_t)0U
#define DEVCONT_TIME_STOP           (uint16_t)0xFFFFU

#define DEVCONT_UNUSED(a)     (a = a)

CDeviceContainer::CDeviceContainer()
{
    this->_idx_processing = 0U;
    this->_ucs_inst_ptr = NULL;
    this->_values_pptr = NULL;
    this->_values_sz = 0U;
    this->_tx_busy = false;
    this->_service_requested = false;
    this->_service_fptr = NULL;
}

CDeviceContainer::~CDeviceContainer()
{
    Clb_RegisterI2CResultCB(NULL, NULL);    /* avoid that the result callback is fired after object is destroyed */
}

void CDeviceContainer::RegisterValues(CDeviceValue** list_pptr, uint16_t list_sz)
{
    this->_idx_processing = 0U;
    this->_values_pptr = list_pptr;
    this->_values_sz = list_sz;
    this->_tx_busy = false;

    if ((list_pptr != NULL) && (list_sz > 0U))
    {
        this->_idx_processing = list_sz - 1U;
    }
}

void CDeviceContainer::ClearValues()
{
    this->_idx_processing = 0U;
    this->_values_pptr = NULL;
    this->_values_sz = 0U;
    this->_tx_busy = false;
}

void CDeviceContainer::SetValue(uint16_t key, uint8_t value)
{
     uint16_t idx;
     bool req_update = false;

     for (idx = 0U; idx < this->_values_sz; idx++)
     {
         if (this->_values_pptr[idx]->GetKey() == key)
         {
            this->_values_pptr[idx]->SetValue(value);
            if (this->_values_pptr[idx]->RequiresUpdate())
            {
                req_update = true;
            }
         }
     }

    if (req_update && (!this->_tx_busy))
    {
        RequestService(DEVCONT_TIME_NOW); //fire callback
    }
}

void CDeviceContainer::IncrementProcIndex(void)
{
    if ((_idx_processing + 1U) >=  this->_values_sz)
    {
        _idx_processing = 0U;
    }
    else
    {
        _idx_processing++;
    }
}

// starts at latest position, searches next value to update, waits until response
void CDeviceContainer::Update()
{
    uint16_t cnt;
    bool error = false;
    _service_requested = false;

    if (this->_ucs_inst_ptr == NULL)
    {
        return;
    }

    if (this->_tx_busy)
    {
        return;
    }
    
    for (cnt = 0u; cnt < this->_values_sz; cnt++)   /* just run one cycle */
    {
        IncrementProcIndex();

        if (_values_pptr[_idx_processing]->RequiresUpdate())
        {
            if (_values_pptr[_idx_processing]->FireUpdateMessage())
            {
                Clb_RegisterI2CResultCB(&OnI2cResult, this);
                this->_tx_busy = true;
                break;
            }
            else
            {
                error = true;
            }
        }
    }

    if (error)
    {
        RequestService(DEVCONT_TIME_RETRIGGER);
    }
}

void CDeviceContainer::HandleI2cResult(Ucs_I2c_Result_t result)
{
    DEVCONT_UNUSED(result);
    this->_tx_busy = false;
    this->RequestService(DEVCONT_TIME_NOW);
}

void CDeviceContainer::OnI2cResult(Ucs_I2c_Result_t result, void *obj_ptr)
{
    ((CDeviceContainer*)obj_ptr)->HandleI2cResult(result);
}

void CDeviceContainer::RequestService(uint16_t timeout)
{
    if (!_service_requested)
    {
        _service_requested = true;

        if (_service_fptr != NULL)
        {
            _service_fptr(timeout);
        }
    }
}