EE445M RTOS
Taken at the University of Texas Spring 2015
timekit.c
Go to the documentation of this file.
1 /* timekit.c
2  * Hershal Bhave and Eric Crosson
3  * 2014-02-04
4  * TimeKit framework for time representation
5  * Lab 3
6  * Last Revision: LOOK IN GIT FGT
7  * LM3S1968- pinless
8  */
9 
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include "timekit.h"
13 
15  clocktime* t = (clocktime*)malloc(sizeof(clocktime));
16  t->hours = 0;
17  t->minutes = 0;
18  t->seconds = 0;
19  t->ms = 0;
20  t->mode = mode;
21  return t;
22 }
23 
24 void TKDestroy(clocktime* tm) {
25  free(tm);
26 }
27 
28 // Return the human-readable string of tm's internal time value.
29 // Input: Pointer to time struct
30 // Output: Pointer to start of human-readable string
31 char* TKToString(clocktime* tm) {
32 
33  switch(tm->mode) {
35  sprintf(tm->printBuffer, "%0.2d:%0.2d", tm->hours, tm->minutes);
36  break;
38  sprintf(tm->printBuffer, "%0.2d:%0.2d:%0.2d",
39  tm->hours, tm->minutes, tm->seconds);
40  break;
42  sprintf(tm->printBuffer, "%0.2d:%0.2d:%0.2d:%0.3d",
43  tm->hours, tm->minutes, tm->seconds, tm->ms);
44  break;
45  default:
46  printf("ERROR OCCURRED IN toString!!");
47  break;
48  }
49  return tm->printBuffer;
50 }
51 
52 // Increment value of tm's hours
53 // Input: tm to increment, hours to increment by
54 // Output: void
55 void TKIncrementHours(clocktime* tm, short hours) {
56 
57  short tmpHours = tm->hours + hours;
58 
59  while ((1U * tmpHours) >= TK_NUM_HOURS) {
60  tmpHours %= TK_NUM_HOURS;
61  }
62 
63  tm->hours = tmpHours;
64 }
65 
66 // Increment the value of tm's minutes
67 // Input: tm to increment, minutes to increment by
68 // Output: void
69 void TKIncrementMinutes(clocktime* tm, short minutes) {
70  short tmpMinutes = tm->minutes + minutes;
71 
72  if(minutes > 0) {
73  while (tmpMinutes >= TK_NUM_MINUTES) {
74  tmpMinutes -= TK_NUM_MINUTES;
75  TKIncrementHours(tm, 1);
76  }
77  } else {
78  while (tmpMinutes < 0) {
79  tmpMinutes += TK_NUM_MINUTES;
80  TKIncrementHours(tm, -1);
81  }
82  }
83  tm->minutes = tmpMinutes;
84 }
85 
86 // Increment the value of tm's seconds
87 // Input: tm to increment, seconds to increment by
88 // Output: void
89 void TKIncrementSeconds(clocktime* tm, short seconds) {
90  short tmpSeconds = tm->seconds + seconds;
91 
92  if(seconds > 0) {
93  while (tmpSeconds >= TK_NUM_SECONDS) {
94  tmpSeconds -= TK_NUM_SECONDS;
95  TKIncrementMinutes(tm, 1);
96  }
97  } else {
98  while (tmpSeconds < 0) {
99  tmpSeconds += TK_NUM_SECONDS;
100  TKIncrementMinutes(tm, -1);
101  }
102 }
103  tm->seconds = tmpSeconds;
104 }
105 
106 // Increment the value of tm's miliseconds
107 // Input: tm to increment, ms to increment by
108 // Output: void
109 void TKIncrementMS(clocktime* tm, short ms) {
110  short tmpMs = tm->ms + ms;
111 
112  if(ms > 0) {
113  while (tmpMs >= TK_NUM_MS) {
114  tmpMs -= TK_NUM_MS;
115  TKIncrementSeconds(tm, 1);
116  }
117  } else {
118  while (tmpMs < 0) {
119  tmpMs += TK_NUM_MS;
120  TKIncrementSeconds(tm, -1);
121  }
122  }
123 
124  tm->ms = tmpMs;
125 }
126 
127 // Verify that the values inside of alleged time object tm are values
128 // possible to see on a clock.
129 // Input: time object tm
130 // Output: either TK_TIME_VALID or not
132  if(tm->hours > TK_NUM_HOURS) {
133  return TK_TIME_INVALID_HOURS;
134  } else if(tm->minutes > TK_NUM_MINUTES) {
136  } else if((tm->mode > 0) && (tm->seconds > TK_NUM_SECONDS)) {
138  } else if((tm->mode > 1) && (tm->ms > TK_NUM_MS)) {
139  return TK_TIME_INVALID_MS;
140  }
141  return TK_TIME_VALID;
142 }
143 
144 // Reset a time object back to zero.
145 // Input: time object tm
146 // Output: void
148  short error = TKValidateTime(tm);
149 
150  switch(error) {
151  case TK_TIME_VALID:
152  break;
154  tm->hours = 0;
155  break;
157  tm->minutes = 0;
158  break;
160  tm->seconds = 0;
161  break;
162  case TK_TIME_INVALID_MS:
163  tm->ms = 0;
164  break;
165  default:
166  break;
167  }
168 }
169 
171 
172  clocktime* handle = (clocktime*)malloc(sizeof(clocktime));
173 
174  handle->hours = th->hours;
175  handle->minutes = th->minutes;
176  handle->seconds = th->seconds;
177  handle->ms = th->ms;
178  handle->mode = th->mode;
179 
180  /* Don't copy the printbuffer! */
181 
182  return handle;
183 }
#define TK_TIME_VALID
Definition: timekit.h:10
void TKIncrementHours(clocktime *tm, short hours)
Definition: timekit.c:55
char mode
Definition: timekit.h:26
void TKDestroy(clocktime *tm)
Definition: timekit.c:24
#define TK_PRINT_MODE_HOURS_MINUTES
Definition: timekit.h:6
#define TK_TIME_INVALID_HOURS
Definition: timekit.h:11
#define TK_TIME_INVALID_MINUTES
Definition: timekit.h:12
short seconds
Definition: timekit.h:24
#define TK_NUM_HOURS
Definition: timekit.h:16
#define TK_TIME_INVALID_MS
Definition: timekit.h:14
#define TK_NUM_MS
Definition: timekit.h:19
void TKIncrementMS(clocktime *tm, short ms)
Definition: timekit.c:109
#define TK_NUM_SECONDS
Definition: timekit.h:18
clocktime * TKDuplicateTimeHandle(clocktime *th)
Definition: timekit.c:170
#define TK_TIME_INVALID_SECONDS
Definition: timekit.h:13
char printBuffer[20]
Definition: timekit.h:27
char * TKToString(clocktime *tm)
Definition: timekit.c:31
#define TK_NUM_MINUTES
Definition: timekit.h:17
short minutes
Definition: timekit.h:23
void TKCleanupTime(clocktime *tm)
Definition: timekit.c:147
void TKIncrementSeconds(clocktime *tm, short seconds)
Definition: timekit.c:89
short TKValidateTime(clocktime *tm)
Definition: timekit.c:131
short hours
Definition: timekit.h:22
void TKIncrementMinutes(clocktime *tm, short minutes)
Definition: timekit.c:69
#define TK_PRINT_MODE_HOURS_MINUTES_SECONDS_MS
Definition: timekit.h:8
#define TK_PRINT_MODE_HOURS_MINUTES_SECONDS
Definition: timekit.h:7
short ms
Definition: timekit.h:25
clocktime * TKCreateTimeHandle(char mode)
Definition: timekit.c:14