-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.c
92 lines (82 loc) · 1.59 KB
/
tools.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
/*
* tools.c
*
* Created on: Sep 17, 2013
* Author: nizinski_w
*/
#include <util/delay.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <config.h>
#include "log.h"
static volatile BOOL bStop;
/** called from ISR to stop interruptible delay
*
*/
void breakable_delay_break(void)
{
bStop = TRUE;
}
/**
* Interruptible delay (by keypress)
* @param __ms
*/
void breakable_delay_ms(unsigned int __ms)
{
bStop = FALSE;
while ( (__ms-- > 0) && (bStop==FALSE) )
{
set_sleep_mode (SLEEP_MODE_IDLE); // wait for int (1ms timer)
sleep_mode();
}
}
/**
* Increment *pucValue (by adding iStep) and make wrap around within range ucMin and ucMax
*
* @param pcValue
* @param iStep TRUE - increment up
* @param cMin
* @param cMax
*/
void vIncrementWithRange (char *pcValue, int iStep, char cMin, char cMax)
{
iStep += *pcValue;
//CLOCK_PRINTF_P (PSTR("iStep=%d ucMax=%d ucMin=%d\n"), iStep, ucMax, ucMin);
if (iStep > cMax)
{
//CLOCK_PRINTF_P(PSTR("MAX\n"));
*pcValue = cMin;
}
else
if (iStep < cMin)
{
//CLOCK_PRINTF_P(PSTR("MIN\n"));
*pcValue = cMax;
}
else
{
//CLOCK_PRINTF_P(PSTR("OK\n"));
*pcValue = iStep;
//CLOCK_PRINTF_P(PSTR("result %d\n"), *pucValue);
}
}
void WdtEnable(void)
{
wdt_enable(WDTO_2S);
}
void WdtDisable(void)
{
wdt_disable();
}
/**
* HW reset using watchdog
*/
void WdtResetHW(void)
{
LOG_P(PSTR("RESET!\n"));
cli();
wdt_enable(WDTO_500MS);
for (;;); // real reset
}