-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
258 lines (233 loc) · 4.05 KB
/
common.c
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
/*
* common.c
*
* Created on: May 17, 2013
* Author: DVD
*/
#define MYONE 100000
#define MYONE_PWM 100
static u8 NumberBuffer[36];
static u8* Nstr;
static s32 NumberToConvert;
static u8 FlagSign;
static s32 A,B,C;
void Delay(u32 len)
{
while(len--);
}
void Delayms(u32 ms)
{
ms *= 11538;
while(ms--);
}
s32 Abs(s32 Num)
{
if(Num >= 0) return Num;
return -Num;
}
u32 MirrorNum(u32 Num)
{
u8 i;
u32 Out = 0;
for(i=0;i<32;i++)
{
Out >>= 1;
Out |= (Num & 0x80000000);
Num <<= 1;
}
return Out;
}
s32 FracNum(s32 Num, s32 C, s32 Z)
{
Num = Num * C;
Num = Num / Z;
return Num;
}
u8 DigitsCount(s32 Num, u32 NumBase)
{
u8 Cnt = 0;
while(Num)
{
Cnt++;
Num /= NumBase;
}
return Cnt;
}
u8 StrLen(u8* Str)
{
u8 Ind;
while(Str[Ind++] != '\0');
return Ind-1;
}
void PutDigit(u8 Digit)
{
*(--Nstr) = Digit;
}
s32 InitNum(s32 Num)
{
FlagSign = 0;
Nstr = &NumberBuffer[36];
PutDigit('\0');
if(Num < 0)
{
FlagSign = '-';
return -Num;
}
return Num;
}
void PutOneDigit(u32 NumBase)
{
u8 Tmp = (NumberToConvert % NumBase);
if(Tmp > 9){ Tmp += 7; }
PutDigit(Tmp + 48);
NumberToConvert /= NumBase;
}
void PutNumberString(u32 NumBase)
{
while(NumberToConvert)
{
PutOneDigit(NumBase);
}
}
u8* Bin2Str(s32 Num)
{
u8 RestToFill;
u8 Flag = 0;
if(Num < 0)
{
Num &= 0x7FFFFFFF;
Flag = 0xFF;
}
NumberToConvert = InitNum(Num);
PutNumberString(2);
RestToFill = 32 - DigitsCount(Num,2);
while(RestToFill)
{
if(Flag && (RestToFill == 1))
{
PutDigit('1');
}else{
PutDigit('0');
}
RestToFill--;
}
return Nstr;
}
u8* Dec2NcharsStr(s32 Num ,u8 DigCount)
{
u8 ExtraDigits = 0;
NumberToConvert = InitNum(Num);
ExtraDigits = DigCount - DigitsCount(Num,10);
PutNumberString(10);
while(ExtraDigits--){ PutDigit(95); }
return Nstr;
}
u8* Dec2Str(s32 Num)
{
NumberToConvert = InitNum(Num);
if(Num){ PutNumberString(10); }
else { PutDigit('0'); }
if(FlagSign){ PutDigit(FlagSign); }
return Nstr;
}
u8* FloatDec2Str(s32 Num)
{
u32 FractionalCnt = DigitsCount(MYONE,10)-1;
NumberToConvert = InitNum(Num);
if(NumberToConvert)
{
while(FractionalCnt--){ PutOneDigit(10); }
PutDigit('.');
PutNumberString(10);
if(Abs(Num) < MYONE){ PutDigit('0'); }
}else{
while(FractionalCnt--){ PutDigit('0'); }
PutDigit('.');
PutDigit('0');
}
if(FlagSign){ PutDigit(FlagSign); }
return Nstr;
}
u8* PWM2Str(u32 Num)
{
if(Num > MAX_VALUE_PWM){ Num = MAX_VALUE_PWM; }
Num = 100*MYONE_PWM*Num;
Num = Num / MAX_VALUE_PWM;
NumberToConvert = InitNum(Num);
// PutDigit('%');
if(NumberToConvert)
{
PutOneDigit(10);
PutOneDigit(10);
PutDigit('.');
PutNumberString(10);
if(Abs(Num) < MYONE_PWM){ PutDigit('0'); }
}else{
PutDigit('0');
PutDigit('0');
PutDigit('.');
PutDigit('0');
}
return Nstr;
}
s32 SumArr(s32* Arr, u32 ArrLen)
{
s32 Sum = 0;
while(ArrLen)
{
Sum += Arr[ArrLen-1];
ArrLen--;
}
return Sum;
}
void FillArr(s32* Arr, u32 ArrLen,s32 Value)
{
while(ArrLen)
{
Arr[ArrLen-1] = Value;
ArrLen--;
}
}
s32 NumPowOf(s32 Num, s32 Pow)
{
s32 Data = Num;
if(Pow < 0){ return 0; }
if(Pow == 0){ return 1; }
if(Pow > 0)
{
Pow--;
while(Pow)
{
Data *= Num;
Pow--;
}
}
return Data;
}
s32 QuadraticEquation(s32 Num)
{
if(Num < 0){ return 0; }
if(Num > 1244){ Num = 1244; }
// 1234567 = 1.234567
A = 591;
B = 1725684;
C = 190058487;
// Calc Result
A = A*NumPowOf(Num,2);
A = A/100;
B = Num * B;
B = B/100;
C = C/100;
Num = MYONE/10000;
return Num*(A+B+C);
}
s32 LinearEquation(s32 Num)
{
if(Num < 0){ return 0; }
if(Num > 70000){ Num = 70000; }
B = 27354;
C = -2200000;
B = (B*Num);
Num = MYONE/10000;
return Num*(B+C);
}