-
Notifications
You must be signed in to change notification settings - Fork 0
/
clockControl.c
161 lines (120 loc) · 3.73 KB
/
clockControl.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
/*
* clockControl.c
*
* Created on: Oct 4, 2014
* Author: watsonb
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <xparameters.h>
#include "xil_io.h"
#include <stdio.h>
#include <supportFiles/display.h>
#include "src/buttons.h"
#include "intervalTimer.h"
#include<supportFiles/utils.h>
#include "clockDisplay.h"
#include "clockControl.h"
#define AD_TIMER_EXPIRED_VALUE 2
#define RATE_TIMER_EXPIRED_VALUE 2
#define AUTO_TIMER_EXPIRED_VALUE 10
#define SECOND_INCREMENT_EXPIRED_VALUE 20
#define RESET 0
#define ZERO 0
static uint32_t adTimer= 0;
static uint32_t autoTimer = 0;
static uint32_t rateTimer = 0;
static uint32_t secondTimer = 0;
static uint32_t actionCounter = 0;
enum clockControl_States{cC_init_st, cC_waiting_for_touch_st, cC_ad_timer_running_st, cC_auto_timer_running_st, cC_rate_timer_running_st, cC_rate_timer_expired_st} cC_State;
//*******************************************************************************
// clockControl_tick()
// implements the state machine as given by the lab specs
// the clock will begin to run automatically after the display is touched
// after reaching the corresponding expired_value, a transition will occur
// autoTimer controls when the automatic incrementing will begin
// rateTimer controls how quickly the numbers will change
// rateTimer_expired_st allows the rateTimer to continue incrementing
// only while in the waiting_for_touch_st, the clock will increment by one second
//*******************************************************************************
void clockControl_tick()
{
switch(cC_State)
{
case cC_init_st:
cC_State = cC_waiting_for_touch_st;
break;
case cC_waiting_for_touch_st:
secondTimer++;
//If an action has been performed, start the clock
if(actionCounter > ZERO && (secondTimer % SECOND_INCREMENT_EXPIRED_VALUE == ZERO))
{
clockDisplay_advanceTimeOneSecond();
}
//Reset the timer values
adTimer = RESET;
autoTimer = RESET;
rateTimer = RESET;
if(display_isTouched())
{
display_clearOldTouchData();
cC_State = cC_ad_timer_running_st;
}
break;
case cC_ad_timer_running_st:
adTimer++;
if(display_isTouched() && adTimer == AD_TIMER_EXPIRED_VALUE)
{
actionCounter++;
cC_State = cC_auto_timer_running_st;
}
if(!display_isTouched() && adTimer == AD_TIMER_EXPIRED_VALUE)
{
clockDisplay_performIncDec();
cC_State = cC_waiting_for_touch_st;
}
break;
case cC_auto_timer_running_st:
autoTimer++;
if(display_isTouched() && autoTimer == AUTO_TIMER_EXPIRED_VALUE)
{
actionCounter++;
cC_State = cC_rate_timer_running_st;
clockDisplay_performIncDec();
}
if(!display_isTouched())
{
clockDisplay_performIncDec();
cC_State = cC_waiting_for_touch_st;
}
break;
case cC_rate_timer_running_st:
rateTimer++;
if(!display_isTouched())
{
cC_State = cC_waiting_for_touch_st;
}
if(display_isTouched() && rateTimer == RATE_TIMER_EXPIRED_VALUE)
{
actionCounter++;
cC_State = cC_rate_timer_expired_st;
}
break;
case cC_rate_timer_expired_st:
rateTimer = RESET;
if(display_isTouched())
{
actionCounter++;
clockDisplay_performIncDec();
cC_State = cC_rate_timer_running_st;
}
if(!display_isTouched())
{
cC_State = cC_waiting_for_touch_st;
}
break;
default:
cC_State = cC_init_st;
}
}