Skip to content
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

Commit 30c3b70

Browse files
authored
Add FakeAnalogWrite example
Add FakeAnalogWrite example
1 parent ab3f53c commit 30c3b70

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

examples/FakeAnalogWrite/FakeAnalogWrite.ino

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ void setup()
220220
}
221221
}
222222

223+
#define USING_MAPPING_TABLE false
224+
223225
void fakeAnalogWrite(uint16_t pin, uint16_t value)
224226
{
225227
uint16_t localValue;
@@ -231,20 +233,20 @@ void fakeAnalogWrite(uint16_t pin, uint16_t value)
231233
if ( (curISRTimerData[i].beingUsed) && (curISRTimerData[i].pin == pin) )
232234
{
233235
localValue = (value < MAX_PWM_VALUE) ? value : MAX_PWM_VALUE;
234-
236+
235237
if (curISRTimerData[i].PWM_PremapValue == localValue)
236238
{
237-
#if (LOCAL_DEBUG > 0)
239+
#if (LOCAL_DEBUG > 0)
238240
Serial.print("Ignore : Same Value for index = ");
239241
Serial.println(i);
240242
#endif
241-
243+
242244
return;
243245
}
244246
else if (curISRTimerData[i].PWM_Value >= 0)
245-
{
247+
{
246248
curISRTimerData[i].PWM_PremapValue = localValue;
247-
249+
248250
// Mapping to corect value
249251
if ( ( localValue == 0) || ( localValue == MAX_PWM_VALUE - 1) )
250252
{
@@ -253,6 +255,9 @@ void fakeAnalogWrite(uint16_t pin, uint16_t value)
253255
}
254256
else
255257
{
258+
259+
#if USING_MAPPING_TABLE
260+
256261
// Get the mapping index
257262
for (int j = 0; j < MAPPING_TABLE_SIZE; j++)
258263
{
@@ -271,18 +276,22 @@ void fakeAnalogWrite(uint16_t pin, uint16_t value)
271276
// Can use map() function
272277
// Can use map() function
273278
curISRTimerData[i].PWM_Value = (uint16_t) ( (localIndex * 10 ) +
274-
( (value - mappingTable[localIndex]) * 10 ) / (mappingTable[localIndex + 1] - mappingTable[localIndex]) );
279+
( (localValue - mappingTable[localIndex]) * 10 ) / (mappingTable[localIndex + 1] - mappingTable[localIndex]) );
280+
281+
#else
282+
curISRTimerData[i].PWM_Value = localValue;
283+
#endif
275284

276285
#if (LOCAL_DEBUG > 0)
277-
Serial.print("Update index = ");
278-
Serial.print(i);
279-
Serial.print(", pin = ");
280-
Serial.print(pin);
281-
Serial.print(", input PWM_Value = ");
282-
Serial.print(value);
283-
Serial.print(", mapped PWM_Value = ");
284-
Serial.println(curISRTimerData[i].PWM_Value);
285-
#endif
286+
Serial.print("Update index = ");
287+
Serial.print(i);
288+
Serial.print(", pin = ");
289+
Serial.print(pin);
290+
Serial.print(", input PWM_Value = ");
291+
Serial.print(value);
292+
Serial.print(", mapped PWM_Value = ");
293+
Serial.println(curISRTimerData[i].PWM_Value);
294+
#endif
286295
}
287296
}
288297
else
@@ -316,6 +325,10 @@ void fakeAnalogWrite(uint16_t pin, uint16_t value)
316325
}
317326
else
318327
{
328+
curISRTimerData[i].PWM_PremapValue = localValue;
329+
330+
#if USING_MAPPING_TABLE
331+
319332
// Get the mapping index
320333
for (int j = 0; j < MAPPING_TABLE_SIZE; j++)
321334
{
@@ -333,7 +346,10 @@ void fakeAnalogWrite(uint16_t pin, uint16_t value)
333346
// Can use map() function
334347
// Can use map() function
335348
curISRTimerData[i].PWM_Value = (uint16_t) ( (localIndex * 10 ) +
336-
( (value - mappingTable[localIndex]) * 10 ) / (mappingTable[localIndex + 1] - mappingTable[localIndex]) );
349+
( (localValue - mappingTable[localIndex]) * 10 ) / (mappingTable[localIndex + 1] - mappingTable[localIndex]) );
350+
#else
351+
curISRTimerData[i].PWM_Value = localValue;
352+
#endif
337353
}
338354

339355
curISRTimerData[i].countPWM = 0;
@@ -411,7 +427,7 @@ void loop()
411427
Serial.print(", max = ");
412428
Serial.println(MAX_PWM_VALUE - 1);
413429
#endif
414-
430+
415431
delay(DELAY_BETWEEN_CHANGE_MS);
416432
}
417433

0 commit comments

Comments
 (0)