EE445M RTOS
Taken at the University of Texas Spring 2015
clock.c
Go to the documentation of this file.
1 /* clock.c
2  * Hershal Bhave and Eric Crosson
3  * 2014-02-09
4  * Implementation for the Clock Hands Library
5  * Lab 3
6  * Last Revision: LOOK IN GIT FGT
7  * LM3S1968
8  */
9 
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <math.h>
13 #include "clock.h"
14 
16  float x, y;
17 
18  x = cosf((ch->angle - CH_ANGLE_OFFSET) * PI_PI / 180) * ch->radius;
19  y = sinf((ch->angle - CH_ANGLE_OFFSET) * PI_PI / 180) * ch->radius;
20 
21  /* avoid underflow (subtracting too much from an unsigned number) */
22  if(-x > ch->center->x) {x = -ch->center->x;}
23  if(-y > ch->center->y) {y = -ch->center->y;}
24 
25  ch->pointOnCircumference->x = ch->center->x+x;
26  ch->pointOnCircumference->y = ch->center->y+y;
27 
29  return ch->shapeBuffer;
30 }
31 
32 clockhand* CHCreateClockHand(point* center, ushort radius, uchar type) {
33  clockhand* ch = (clockhand*) calloc(1, sizeof(clockhand));
34 
35  ch->radius = radius;
36  ch->center = shape_duplicate_point(center);
37  ch->type = type;
38 
39  ch->angle = 0;
40 
42 
43  return ch;
44 }
45 
47  ushort tmpHours;
48 
49  switch(ch->type) {
50  case CH_HOURS:
51  tmpHours = tm->hours;
52  while(tmpHours > 12) {
53  tmpHours -= 12;
54  }
55  ch->angle = (short)(30*tmpHours + (tm->minutes)/2.0);
56  break;
57  case CH_MINUTES:
58  ch->angle = (short)(6.0*tm->minutes + tm->seconds/10.0);
59  break;
60  case CH_SECONDS:
61  ch->angle = 6*tm->seconds + 3.0/500*tm->ms;
62  break;
63  case CH_MS:
64  ch->angle = (short)((9/25.0)*(tm->ms));
65  break;
66  }
67 
68  /* printf("--- ANGLE FOR %i: %i --- %i\n", ch->type, ch->angle, CH_SECONDS); */
69 }
70 
72 
73  /* SHDestroyShape(ch->shapeBuffer); */
74  /* free(ch->shapeBuffer); */
75  free(ch->pointOnCircumference);
76  free(ch->center);
77  free(ch);
78 }
79 
81 
82  /* point* p; */
83  /* p = SHCreatePoint(p->x, p->y, p->shade); */
84  /* SHDestroyShape(ch->shapeBuffer); */
85  /* ch->center = p; */
86 
87  free(ch->shapeBuffer->points);
88  free(ch->shapeBuffer);
89 }
90 
92 
93  ushort i;
94  clockface* cf = (clockface*) calloc(1, sizeof(clockface));
95 
96  /* Moved to CFToShapes */
97  /* cf->shapeBuffer = (shape**)malloc(numHands * sizeof(shape*)); */
98  cf->numHands = numHands;
99 
100  /* Shallow copy of clockhands */
101  cf->hands = (clockhand**) calloc(numHands, sizeof(clockhand*));
102  for(i=0; i < numHands; ++i) {
103  cf->hands[i] = chs[i];
104  }
105 
106  /* Don't deep copy timehandle, we need it for the interrupt */
107  cf->timehandle = th;
108 
109  return cf;
110 }
111 
113  free(cf->hands);
114  free(cf);
115 }
116 
118  clockhand* dup = (clockhand*) calloc(1, sizeof(clockhand));
119 
120  dup->radius = ch->radius;
121  dup->center = shape_duplicate_point(ch->center);
122  dup->type = ch->type;
125  return dup;
126 }
127 
129  ushort i;
130  for(i=0; i<cf->numHands; ++i) {
131  CHCalculateAngle(cf->hands[i], cf->timehandle);
132  }
133 }
134 
136  ushort i;
137 
138  cf->shapeBuffer = (shape**)malloc(cf->numHands * sizeof(shape*));
139 
140  for(i=0; i<cf->numHands; ++i) {
141  cf->shapeBuffer[i] = CHToShape(cf->hands[i]);
142  }
143  return cf->shapeBuffer;
144 }
145 
147  ushort i;
148  for(i=0; i<cf->numHands; ++i) {
149  /* free(cf->shapeBuffer[i]); */
151  }
152  free(cf->shapeBuffer);
153 }
unsigned short y
Definition: shape.h:16
shape * CHToShape(clockhand *ch)
Definition: clock.c:15
shape * shape_create(ushort num_points,...)
Definition: shape.c:31
Representation of a shape.
Definition: shape.h:26
short seconds
Definition: timekit.h:24
ushort radius
Definition: clock.h:35
void CFDestroyClockFace(clockface *cf)
Definition: clock.c:112
#define CH_SECONDS
Definition: clock.h:18
#define PI_PI
Definition: clock.h:29
unsigned short ushort
Definition: defines.h:35
shape * shapeBuffer
Definition: clock.h:43
void CHCalculateAngle(clockhand *ch, clocktime *tm)
Definition: clock.c:46
#define CH_ANGLE_OFFSET
Definition: clock.h:27
void CFUpdateTime(clockface *cf)
Definition: clock.c:128
point ** points
Definition: shape.h:29
point * shape_duplicate_point(point *p)
Definition: shape.h:77
shape ** CFToShapes(clockface *cf)
Definition: clock.c:135
clocktime * timehandle
Definition: clock.h:61
clockhand * CHCreateClockHand(point *center, ushort radius, uchar type)
Definition: clock.c:32
void CHDestroyClockHand(clockhand *ch)
Definition: clock.c:71
unsigned char uchar
Definition: defines.h:34
uchar type
Definition: clock.h:39
shape * shape_duplicate_shape(shape *s)
Definition: shape.c:66
#define CH_MINUTES
Definition: clock.h:17
#define CH_MS
Definition: clock.h:19
void CFDestroyShapes(clockface *cf)
Definition: clock.c:146
point * center
Definition: clock.h:37
void CHDestroyClockHandShape(clockhand *ch)
Definition: clock.c:80
point * pointOnCircumference
Definition: clock.h:46
ushort angle
Definition: clock.h:45
Representation of an ordered pair with a shade.
Definition: shape.h:13
clockface * CFCreateClockFace(ushort numHands, clockhand **chs, clocktime *th)
Definition: clock.c:91
shape ** shapeBuffer
Definition: clock.h:65
short minutes
Definition: timekit.h:23
clockhand * CHDuplicateClockHand(clockhand *ch)
Definition: clock.c:117
short hours
Definition: timekit.h:22
#define CH_HOURS
Definition: clock.h:16
unsigned short x
Definition: shape.h:15
clockhand ** hands
Definition: clock.h:54
short ms
Definition: timekit.h:25
ushort numHands
Definition: clock.h:55