PropWare
C++ objects and CMake build system for Parallax Propeller
Home
Download
Getting Started
Documentation
Reference
About
Related Links
Namespace List
Class List
File List
Examples
Build System
File List
File Members
propsideworkspace
Learn
Simple Libraries
Sensor
libcolorpal
colorpal.c
1
/*
2
* @file colorpal.c
3
*
4
* @author Andy Lindsay
5
*
6
* @version 0.55
7
*
8
* @copyright
9
* Copyright (C) Parallax, Inc. 2012. All Rights MIT Licensed.
10
*
11
* @brief Simplifies reading Parallax ColorPAL sensor.
12
*/
13
14
15
#include "simpletools.h"
16
#include "colorpal.h"
17
#include "fdserial.h"
18
19
//void colorPal_reset(colorPal *device);
20
//void colorPal_init(colorPal *device);
21
22
23
void
colorPal_reset(
colorPal
*device)
24
{
25
colorPal_t
*cp = (
colorPal_t
*) device->
devst
;
26
low(cp->tx_pin);
27
input(cp->rx_pin);
28
while
(input(cp->rx_pin) == 0);
29
low(cp->tx_pin);
30
pause(80);
31
input(cp->rx_pin);
32
pause(10);
33
}
34
35
36
void
colorPal_init(
colorPal
*device)
37
{
38
colorPal_t
*cp = (
colorPal_t
*) device->
devst
;
39
40
colorPal_reset(device);
41
dprint(device,
"=(00 $ m)!"
);
42
//pause(10);
43
//low(cp->rx_pin);
44
}
45
46
47
void
colorPal_getRGB(
colorPal
*device,
int
*r,
int
*g,
int
*b)
48
{
49
char
rs[4], gs[4], bs[4];
50
51
fdserial_rxFlush(device);
52
53
while
(readChar(device) !=
'$'
);
54
55
memset(rs, 0, 4);
56
for
(
int
i = 0; i < 3; i++)
57
{
58
rs[i] = readChar(device);
59
}
60
memset(gs, 0, 4);
61
for
(
int
i = 0; i < 3; i++)
62
{
63
gs[i] = readChar(device);
64
}
65
memset(bs, 0, 4);
66
for
(
int
i = 0; i < 3; i++)
67
{
68
bs[i] = readChar(device);
69
}
70
71
//print("rs = %s, gs = %s, bs = %s\n", rs, gs, bs);
72
73
sscan(rs,
"%x"
, r);
74
sscan(gs,
"%x"
, g);
75
sscan(bs,
"%x"
, b);
76
}
77
78
79
colorPal
*colorPal_open(
int
sioPin)
80
{
81
int
mode = 0b1100;
82
int
baudrate = 4800;
83
int
txpin = sioPin;
84
int
rxpin = sioPin;
85
86
extern
int
binary_pst_dat_start[];
87
88
colorPal_t
*colpalptr;
89
90
//char* idstr = (char*) malloc(12);
91
//memset(idstr, 0, 12);
92
93
/* can't use array instead of malloc because it would go out of scope. */
94
char
* bufptr = (
char
*) malloc(2*(FDSERIAL_BUFF_MASK+1));
95
colorPal
* term = (
colorPal
*) malloc(
sizeof
(
colorPal
));
96
memset(term, 0,
sizeof
(
colorPal
));
97
98
colpalptr = (
void
*) malloc(
sizeof
(
colorPal_t
));
99
term->
devst
= colpalptr;
100
memset((
char
*)colpalptr, 0,
sizeof
(
colorPal_t
));
101
102
if
(rxpin == 31 && txpin == 30) {
103
simpleterm_close();
104
}
105
106
/* required for terminal to work */
107
term->
txChar
= fdserial_txChar;
108
term->
rxChar
= fdserial_rxChar;
109
110
colpalptr->rx_pin = rxpin;
/* recieve pin */
111
colpalptr->tx_pin = txpin;
/* transmit pin */
112
colpalptr->mode = mode;
/* interface mode */
113
//colpalptr->en = enablePin; /* interface mode */
114
115
/* baud from clkfreq (cpu clock typically 80000000 for 5M*pll16x) */
116
colpalptr->ticks = CLKFREQ/baudrate;
117
colpalptr->buffptr = bufptr;
/* receive and transmit buffer */
118
//colpalptr->idstr = idstr;
119
120
121
/*
122
// now start the kernel
123
#if defined(__PROPELLER_USE_XMM__)
124
{ unsigned int buffer[2048];
125
memcpy(buffer, binary_pst_dat_start, 2048);
126
term->cogid[0] = cognew(buffer, (void*)colopalptr) + 1;
127
}
128
#else
129
*/
130
term->
cogid
[0] = setStopCOGID(cognew((
void
*)binary_pst_dat_start, (
void
*)colpalptr));
131
//#endif
132
133
waitcnt(CLKFREQ/10+CNT);
// give cog chance to load
134
135
colorPal_init(term);
136
137
return
term;
138
}
139
140
141
void
colorPal_close(
colorPal
*device)
142
{
143
int
id
= device->
cogid
[0];
144
colorPal_t
*colpalptr = (
colorPal_t
*) device->
devst
;
145
146
while
(fdserial_rxCheck(device) >= 0)
147
;
// clear out queue by receiving all available
148
fdserial_txFlush(device);
149
150
if
(
id
> 0) cogstop(getStopCOGID(
id
));
151
152
free((
void
*)colpalptr->buffptr);
153
//free((void*)colpalptr->idstr);
154
free((
void
*)colpalptr);
155
free(device);
156
device = 0;
157
}
158
159
colorpal_serinfo
Definition:
colorpal.h:26
text_struct::rxChar
int(* rxChar)(struct text_struct *p)
Definition:
simpletext.h:73
text_struct::txChar
int(* txChar)(struct text_struct *p, int ch)
Definition:
simpletext.h:75
text_struct::cogid
int cogid[TERM_COG_LEN]
Definition:
simpletext.h:77
text_struct::devst
volatile void * devst
Definition:
simpletext.h:79
text_struct
Structure that contains data used by simple text device libraries.
Definition:
simpletext.h:70
Generated on Sat Aug 27 2016 00:58:09 for PropWare by
1.8.12