-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeSource.c
146 lines (118 loc) · 3.02 KB
/
TimeSource.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
//
// TimeSource.c
// ObserverPattern
//
// Created by Balu Pillai on 23/05/2020.
// Copyright © 2020 Balu Pillai. All rights reserved.
//
#include "TimeSource.h"
#include <assert.h>
#include "types.h"
#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode
{
TimeObserver item;
struct ListNode* next;
}listNode_t;
static listNode_t observers;
/* Local helper functions for managing the linked-list (implementation omitted). */
static void appendToList(const TimeObserver *observer)
{
/* Append a copy of the observer to the linked-list. */
listNode_t *head = &observers;
while(NULL != head->next)
{
head = head->next;
}
head->next = (listNode_t *) malloc(sizeof(listNode_t));
if(head->next !=NULL)
{
head->next->item = *observer;
head->next->next = NULL;
}
}
void getAllObservers()
{
listNode_t *head = &observers;
uint8_t id = 0;
while(NULL != head->next)
{
printf("Observer %d instance address is %d", ++id, head->item.instance);
head = head->next;
}
}
static void removeFromList(const TimeObserver* observer)
{
/* Identify the observer in the linked-list and remove that node. */
listNode_t *head = &observers;
listNode_t *prevNode = &observers;
while(NULL != head->next)
{
if(head->item.instance == observer->instance)
{
printf("Found node to delete");
prevNode->next = head->next->next;
//printf("%d", observer->instance);
free(head);
break;
}
else
{
prevNode = head;
head = head->next;
}
}
}
void attach(const TimeObserver* observer)
{
assert(NULL != observer);
appendToList(observer);
}
void detach(const TimeObserver* observer)
{
assert(NULL != observer);
removeFromList(observer);
}
void notifyObservers(uint32 time)
{
struct ListNode* node = observers.next;
/* Walk through the linked-list and notify every observer that another millisecond passed. */
while(NULL != node)
{
TimeObserver* observer = &node->item;
observer->notification(observer->instance, &time);
node = node->next;
}
}
static uint32 calculateNewTime(void)
{
static uint32_t incrementVal;
return ++incrementVal;
}
static boolean _setChanged()
{
/* Logic to debounce */
return TRUE;
}
/* Implementation of the original responsibility of the TimeSource (code for initialization, etc
omitted). */
void msTick()
{
uint32 currentTime = 0U;
static uint32 prevTime;
boolean changed = FALSE;
/* Invoke a function encapsulating the knowledge about time representation. */
currentTime = calculateNewTime();
if(currentTime!=prevTime)
{
/* Debouncing */
changed = _setChanged();
if(TRUE == changed)
{
/* Ready to send notification */
notifyObservers(currentTime);
prevTime = currentTime;
}
}
}