-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathVario.cpp
More file actions
executable file
·134 lines (131 loc) · 4.03 KB
/
Vario.cpp
File metadata and controls
executable file
·134 lines (131 loc) · 4.03 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
#include "Vario.h"
typedef struct VarioBuff_t {
uint32_t ToneTimeLeft;
uint32_t TonePeriod;
uint32_t ToneHalfPeriod;
uint32_t ToneTimePause;
} VarioBuff_t;
const Vario_table VarioTab = {
/*Silence Zone
Lo, Hi (M/S*100)*/
{-60, 10},
/*Table
M/S*100,Hertz,Cycle,Duty*/
{
/*
{-1000,200 ,200,100},
{-300 ,290 ,200,100},
{-200 ,360 ,200,100},
{-100 ,440 ,200,100},
{-50 ,470 ,600,100},
{-0 ,490 ,600, 50},
{ 50 ,550 ,550, 50},
{ 100 ,590 ,500, 50},
{ 200 ,670 ,400, 50},
{ 300 ,740 ,310, 50},
{ 500 ,880 ,250, 50},
{ 1000,1100,200, 50}}
*/
{-1000,288 ,650,80},
{-300 ,416 ,650,80},
{-200 ,448 ,650,80},
{-100 ,480 ,650,80},
{-50 ,496 ,650,80},
{-0 ,512 ,600, 50},
{ 50 ,550 ,600, 50},
{ 100 ,590 ,550, 50},
{ 200 ,670 ,400, 50},
{ 300 ,704 ,310, 50},
{ 500 ,880 ,250, 50},
{ 1000,1100,200, 50}}
/*
tone=-10.00,288,650,80
tone=-3.00,416,650,80
tone=-2.00,448,650,80
tone=-1.00,480,650,80
tone=-0.50,496,650,80
tone=0.00,512,600,50
tone=0.50,544,550,50
tone=1.00,576,500,50
tone=2.00,640,400,50
tone=3.00,704,310,50
tone=5.00,832,240,50
tone=10.0,1152,240,50
*/
};
/*------------------------------------------------------------------------------------------*/
static int32_t map(int32_t x, int32_t in_min, int32_t in_max, int32_t out_min, int32_t out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
/*------------------------------------------------------------------------------------------*/
static void Vario_GetData(int32_t Vspeed, Vario_data *data) {
uint8_t MinPoint;
uint8_t MaxPoint;
// Range check
if (Vspeed < VarioTab.Data[0].MS)
Vspeed = VarioTab.Data[0].MS;
if (Vspeed > VarioTab.Data[11].MS)
Vspeed = VarioTab.Data[11].MS;
// Interpolation
uint8_t i = 0;
while (!(Vspeed <= VarioTab.Data[i].MS)) {
i++;
}
if (Vspeed == VarioTab.Data[i].MS) {
data->Hertz = VarioTab.Data[i].Hertz;
data->Cycle = VarioTab.Data[i].Cycle;
data->Duty = VarioTab.Data[i].Duty;
} else {
MinPoint = i - 1;
MaxPoint = i;
data->Hertz = map(Vspeed, VarioTab.Data[MinPoint].MS,
VarioTab.Data[MaxPoint].MS, VarioTab.Data[MinPoint].Hertz,
VarioTab.Data[MaxPoint].Hertz);
data->Cycle = map(Vspeed, VarioTab.Data[MinPoint].MS,
VarioTab.Data[MaxPoint].MS, VarioTab.Data[MinPoint].Cycle,
VarioTab.Data[MaxPoint].Cycle);
data->Duty = map(Vspeed, VarioTab.Data[MinPoint].MS,
VarioTab.Data[MaxPoint].MS, VarioTab.Data[MinPoint].Duty,
VarioTab.Data[MaxPoint].Duty);
}
}
/*------------------------------------------------------------------------------------------*/
VarioBuff_t WorkBuff;
VarioBuff_t LoadBuff;
bool VarioPlay = false;
/*------------------------------------------------------------------------------------------*/
void Vario_play(int32_t Vspeed, bool play) {
Vario_data data;
if ((play == false) | ((Vspeed > VarioTab.SilenceZone.Lo) & (Vspeed < VarioTab.SilenceZone.Hi))) {
VarioPlay = false;
return;
}
VarioPlay = true;
Vario_GetData(Vspeed, &data);
LoadBuff.TonePeriod = 1000000 / 128 / data.Hertz;
LoadBuff.ToneHalfPeriod = (LoadBuff.TonePeriod / 2);
LoadBuff.ToneTimeLeft = data.Cycle * 1000 / 128;
LoadBuff.ToneTimePause = LoadBuff.ToneTimeLeft - (LoadBuff.ToneTimeLeft * data.Duty / 100);
}
/*------------------------------------------------------------------------------------------*/
void Vario_driver(void) {
static uint32_t toneCounter = 0;
if ((WorkBuff.ToneTimeLeft == 0) & (VarioPlay)) {
WorkBuff = LoadBuff;
}
if (WorkBuff.ToneTimeLeft > WorkBuff.ToneTimePause) {
if (toneCounter == WorkBuff.ToneHalfPeriod) {
Buzzer_ClrVal();
} else
if (toneCounter >= WorkBuff.TonePeriod) {
toneCounter = 0;
Buzzer_SetVal();
}
toneCounter++;
} else {
Buzzer_ClrVal();
}
if (WorkBuff.ToneTimeLeft)
WorkBuff.ToneTimeLeft--;
}
/*------------------------------------------------------------------------------------------*/