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_container.h"
25 #include "callbacks.h"
28 #define DEVCONT_TIME_RETRIGGER (uint16_t)100U
29 #define DEVCONT_TIME_NOW (uint16_t)0U
30 #define DEVCONT_TIME_STOP (uint16_t)0xFFFFU
32 #define DEVCONT_UNUSED(a) (a = a)
34 CDeviceContainer::CDeviceContainer()
36 this->_idx_processing = 0U;
37 this->_ucs_inst_ptr = NULL;
38 this->_values_pptr = NULL;
39 this->_values_sz = 0U;
40 this->_tx_busy = false;
41 this->_service_requested = false;
42 this->_service_fptr = NULL;
45 CDeviceContainer::~CDeviceContainer()
47 Clb_RegisterI2CResultCB(NULL, NULL); /* avoid that the result callback is fired after object is destroyed */
50 void CDeviceContainer::RegisterValues(CDeviceValue** list_pptr, uint16_t list_sz)
52 this->_idx_processing = 0U;
53 this->_values_pptr = list_pptr;
54 this->_values_sz = list_sz;
55 this->_tx_busy = false;
57 if ((list_pptr != NULL) && (list_sz > 0U))
59 this->_idx_processing = list_sz - 1U;
63 void CDeviceContainer::ClearValues()
65 this->_idx_processing = 0U;
66 this->_values_pptr = NULL;
67 this->_values_sz = 0U;
68 this->_tx_busy = false;
71 void CDeviceContainer::SetValue(uint16_t key, uint8_t value)
74 bool req_update = false;
76 for (idx = 0U; idx < this->_values_sz; idx++)
78 if (this->_values_pptr[idx]->GetKey() == key)
80 this->_values_pptr[idx]->SetValue(value);
81 if (this->_values_pptr[idx]->RequiresUpdate())
88 if (req_update && (!this->_tx_busy))
90 RequestService(DEVCONT_TIME_NOW); //fire callback
94 void CDeviceContainer::IncrementProcIndex(void)
96 if ((_idx_processing + 1U) >= this->_values_sz)
106 // starts at latest position, searches next value to update, waits until response
107 void CDeviceContainer::Update()
111 _service_requested = false;
113 if (this->_ucs_inst_ptr == NULL)
123 for (cnt = 0u; cnt < this->_values_sz; cnt++) /* just run one cycle */
125 IncrementProcIndex();
127 if (_values_pptr[_idx_processing]->RequiresUpdate())
129 if (_values_pptr[_idx_processing]->FireUpdateMessage())
131 Clb_RegisterI2CResultCB(&OnI2cResult, this);
132 this->_tx_busy = true;
144 RequestService(DEVCONT_TIME_RETRIGGER);
148 void CDeviceContainer::HandleI2cResult(Ucs_I2c_Result_t result)
150 DEVCONT_UNUSED(result);
151 this->_tx_busy = false;
152 this->RequestService(DEVCONT_TIME_NOW);
155 void CDeviceContainer::OnI2cResult(Ucs_I2c_Result_t result, void *obj_ptr)
157 ((CDeviceContainer*)obj_ptr)->HandleI2cResult(result);
160 void CDeviceContainer::RequestService(uint16_t timeout)
162 if (!_service_requested)
164 _service_requested = true;
166 if (_service_fptr != NULL)
168 _service_fptr(timeout);