MegTempControl.c
10.5 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <math.h>
#include "JZsdkLib.h"
#include "BaseConfig.h"
#include "Megaphone/Megaphone.h"
#include "Megaphone/Volume/VolumeLimit.h"
#include "Megaphone/Volume/volume.h"
#define C_TIME 10
#define MAX_TIME 240
// 定义常量
//H1T ADC方案
#define R0 10000.0 // 在T0 = 25摄氏度时的电阻值 (10k欧)
#define B_NUM 3428.0 // 物质的B参数
#define T0 298.15 // T0 = 25摄氏度在开尔文下的值
#define ADC_MAX_VALUE 63 // ADC值的最大值
#define ADC_VOLTAGE_MAX 1900 // ADC模块的最大电压值 (1.9V)
#define REF_VOLTAGE 3300 // 稳压源电压 (3300mV)
#define MAX_TEMPRATURE_LIMIT 55 //60
#define MAX_TEMPRATURE 50 //53 //80最大52.75度
#define EXAMPLE_IOCTL_MAGIC 'A'
#define EXAMPLE_IOCTL_GET_DATA _IOR(EXAMPLE_IOCTL_MAGIC, 0, int)
static double g_MegTemp = 0.0;
static int MegTempFlag = JZ_FLAGCODE_OFF;
// 计算温度的函数
static double MegTempControl_calculateTemperature(int adcValue)
{
// 计算实际电压
double voltage = (ADC_VOLTAGE_MAX / ADC_MAX_VALUE) * adcValue;
printf("real V:%f\n",voltage);
// 计算热敏电阻 R
double R = (REF_VOLTAGE*1000-1000*voltage)/voltage;
// 根据热敏电阻的公式计算 T
double T = (1 / ((1 / T0) + ((log(R / R0)) / B_NUM))); // 将 T 计算为开尔文温度
// 将开尔文转换为摄氏度
return T - 273.15;
}
/************************
*
* 温度模拟线程
*
*
* ************************/
static T_JZsdkReturnCode MegTempRead_ImitateTem(int *fd)
{
int PlayTime = 0;
int PlayVolume = 0;
int RealVolume = 0;
int ImitateTem = 36; //模拟温度
int CalParam = 0; //计算用参数 达到MAX_TIME*100 就必须开始降低音量 小于MAX_TIME*80 就可以不降了
int PlayStatus = JZ_FLAGCODE_OFF;
int val;
JZSDK_LOG_INFO("执行软温控");
while (1)
{
//执行驱动读操作
if (*fd >= 0)
{
if (ioctl(*fd, EXAMPLE_IOCTL_GET_DATA, &val) > 0)
{
if (val != 0 && val != 63)
{
JZSDK_LOG_INFO("温控恢复正常");
break;
}
else
{
JZSDK_LOG_DEBUG("温控修复失败");
}
}
else
{
close(*fd);
*fd = open("/dev/sunxi_adc",O_RDWR);
if (*fd < 0)
{
JZSDK_LOG_ERROR("重启测温模块失败");
}
}
}
//获取播放状态
int status = Megaphone_GetMegaphoneStatus();
if (status != JZ_FLAGCODE_OFF)
{
PlayStatus = JZ_FLAGCODE_ON;
}
else
{
PlayStatus = JZ_FLAGCODE_OFF;
}
//通过实际音量 获取反推的100音量
RealVolume = Megaphone_get_RealTargetVolume();
if (DEVICE_VERSION == JZ_H1T)
{
if (status == 0x20)
{
PlayVolume = RealVolume * MAX_TTS_VOLUME /100;
}
else
{
PlayVolume = RealVolume * MAX_VOLUME/100;
}
}
//正在进行播放
if (PlayStatus == JZ_FLAGCODE_ON)
{
PlayTime+=C_TIME;
if (PlayTime >= MAX_TIME)
{
//最大计算时间是MAX_TIMEs
PlayTime = MAX_TIME;
}
}
if (PlayStatus == JZ_FLAGCODE_OFF)
{
PlayTime-=C_TIME;
if (PlayTime <= 0)
{
PlayTime = 0;
}
}
//计算温度变化
CalParam = PlayVolume*PlayTime;
//超过阈值,开始降温
if (CalParam >= MAX_TIME*100)
{
ImitateTem = MAX_TEMPRATURE_LIMIT;
}
#if DEVICE_VERSION == JZ_H1T
else if (CalParam < MAX_TIME*100 && CalParam >= MAX_TIME*71)
{
ImitateTem = MAX_TEMPRATURE_LIMIT - 5;
}
#else
else if (CalParam < MAX_TIME*100 && CalParam >= MAX_TIME*80)
{
ImitateTem = MAX_TEMPRATURE_LIMIT - 5;
}
#endif
else
{
ImitateTem = MAX_TEMPRATURE - 1;
}
g_MegTemp = ImitateTem;
delayS(C_TIME);
}
}
static void *MegTempRead_task(void *arg)
{
double RealTemperature = 0.0;
int val;
//打开驱动
int fd = open("/dev/sunxi_adc",O_RDWR);
if(fd < 0)
{
return NULL;
JZSDK_LOG_ERROR("测温模块出错");
}
delayS(1);
// close(fd);
// JZSDK_LOG_INFO("关闭驱动");
// delayS(1);
// fd = open("/dev/sunxi_adc",O_RDWR);
// if(fd < 0)
// {
// JZSDK_LOG_ERROR("重启测温模块出错");
// }
// delayS(1);
//执行驱动读操作
int ret = ioctl(fd, EXAMPLE_IOCTL_GET_DATA, &val);
if (ret < 0 || val == 63 || val == 0)
{
if (ret < 0)
{
JZSDK_LOG_ERROR("测温出错");
}
if (val == 0)
{
JZSDK_LOG_ERROR("读值出错");
}
if (val == 63)
{
JZSDK_LOG_ERROR("最大值出错");
}
//执行软温控
MegTempRead_ImitateTem(&fd);
}
JZSDK_LOG_INFO("执行硬温控");
while (1)
{
//执行驱动读操作
if (ioctl(fd, EXAMPLE_IOCTL_GET_DATA, &val) < 0)
{
JZSDK_LOG_ERROR("测温出错");
delayS(2);
continue;
}
//JZSDK_LOG_DEBUG("Data from driver: %d\n", val);
if(val>0)
{
RealTemperature = MegTempControl_calculateTemperature(val);
g_MegTemp = RealTemperature;
JZSDK_LOG_DEBUG("T= %lf℃\n", g_MegTemp);
}
delayS(C_TIME);
}
close(fd);
}
static int CalFlag = 0;
static T_JZsdkReturnCode MegTempControl_calculateVolume(double temperature, int *volume, int TargetVolume)
{
int volumeDecrement = 0;
int tempDelta;
//检测到高于上限温度
tempDelta = temperature - MAX_TEMPRATURE_LIMIT;
if (tempDelta > 12)
{
(*volume) = 0;
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
else if (tempDelta > 10)
{
volumeDecrement = 10;
}
else if (tempDelta > 7)
{
volumeDecrement = 6;
}
else if (tempDelta > 5)
{
volumeDecrement = 3;
}
else if (tempDelta > 3)
{
volumeDecrement = 2;
}
else if (tempDelta >= 0)
{
volumeDecrement = 1; // 默认减少1,适用于 tempDelta <= 3 的情况
}
else
{
volumeDecrement = 0;
}
if ((temperature >= MAX_TEMPRATURE ) && (temperature <= MAX_TEMPRATURE_LIMIT))
{
CalFlag++;
if (CalFlag %2 == 1)
{
volumeDecrement = 1;
}
}
//检测到低于稳定温度
if ( (*volume) < TargetVolume && (temperature < MAX_TEMPRATURE))
{
volumeDecrement = -1;
}
if (*volume > 0)
{
(*volume) -= volumeDecrement;
if ((*volume) <= 0)
{
(*volume) = 0;
}
}
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
static void *MegTempControl_task(void *arg)
{
int PlayVolume_old = Megaphone_get_RealTargetVolume();
int PlayVolume_new = PlayVolume_old;
int BalanceVolume = PlayVolume_old;
int OldBalanceVolume = PlayVolume_old;
while (1)
{
//获取当前音量
PlayVolume_new = Megaphone_get_RealTargetVolume();
//对比音量变化
if (PlayVolume_new != PlayVolume_old) //修改过喊话器音量
{
BalanceVolume = PlayVolume_new;
PlayVolume_old = PlayVolume_new;
}
//计算音量加减
MegTempControl_calculateVolume(g_MegTemp, &BalanceVolume, PlayVolume_new);
//调整音量
if (BalanceVolume != OldBalanceVolume)
{
//JZSDK_LOG_DEBUG("温控调整了温度");
//修改音量
Megaphone_SetSystemVolume(BalanceVolume);
//修改ui
Meg_TempControlVolumeUi(BalanceVolume);
}
OldBalanceVolume = BalanceVolume;
delayS(C_TIME);
}
}
T_JZsdkReturnCode MegTempControl_Init()
{
if (DEVICE_VERSION != JZ_H1T)
{
//只有h1t有喊话器温控
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
pthread_t MegTempReadTask;
pthread_attr_t task_attribute; //线程属性
pthread_attr_init(&task_attribute); //初始化线程属性
pthread_attr_setdetachstate(&task_attribute, PTHREAD_CREATE_DETACHED); //设置线程属性
int task_ret = pthread_create(&MegTempReadTask,&task_attribute,MegTempRead_task,NULL); //TTS线程
if(task_ret != 0)
{
JZSDK_LOG_ERROR("创建MegTempReadTask线程失败!\n");
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
}
if (MegTempFlag == JZ_FLAGCODE_ON)
{
pthread_t MegTempControlTask;
pthread_attr_t task_attribute; //线程属性
pthread_attr_init(&task_attribute); //初始化线程属性
pthread_attr_setdetachstate(&task_attribute, PTHREAD_CREATE_DETACHED); //设置线程属性
int task_ret = pthread_create(&MegTempControlTask,&task_attribute,MegTempControl_task,NULL); //TTS线程
if(task_ret != 0)
{
JZSDK_LOG_ERROR("创建MegTempControl_Task线程失败!\n");
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
}
}
JZSDK_LOG_DEBUG("MegTempControl_Init");
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
double MegTempControl_GetTemp()
{
return g_MegTemp;
}
// #define TARGET_VOLUME 100
// #define MAX_TEMPERATURE 55
// #define MIN_TEMPERATURE 50
// void adjustVolume(int temperature, int* volume) {
// if (temperature <= MIN_TEMPERATURE) {
// *volume = TARGET_VOLUME;
// }
// else if (temperature >= MAX_TEMPERATURE) {
// *volume = 0;
// }
// else {
// int temperatureRange = MAX_TEMPERATURE - MIN_TEMPERATURE;
// int volumeRange = TARGET_VOLUME - 0;
// int temperatureDiff = temperature - MIN_TEMPERATURE;
// int volumeDiff = (temperatureDiff * volumeRange) / temperatureRange;
// *volume = volumeRange - volumeDiff;
// }
// }