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
simple
colorpal.c
1
/*
2
* @file colorpal.c
3
*
4
* @author Andy Lindsay
5
*
6
* @version 0.50
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
18
void
colorPal_reset(
colorPal
*device);
19
void
colorPal_init(
colorPal
*device);
20
21
void
colorPal_reset(
colorPal
*device)
22
{
23
colorPal_t
*cp = (
colorPal_t
*) device->
devst
;
24
low(cp->tx_pin);
25
input(cp->rx_pin);
26
while
(input(cp->rx_pin) == 0);
27
low(cp->tx_pin);
28
pause(80);
29
input(cp->rx_pin);
30
pause(10);
31
}
32
33
34
void
colorPal_init(
colorPal
*device)
35
{
36
37
colorPal_t
*cp = (
colorPal_t
*) device->
devst
;
38
39
colorPal_reset(device);
40
dprint(device,
"=(00 $ m)!"
);
41
pause(10);
42
low(cp->rx_pin);
43
}
44
45
46
void
colorPal_getRGB(
colorPal
*device,
int
*r,
int
*g,
int
*b)
47
{
48
char
rs[4], gs[4], bs[4];
49
50
while
(readChar(device) !=
'$'
);
51
52
memset(rs, 0, 4);
53
for
(
int
i = 0; i < 3; i++)
54
{
55
rs[i] = readChar(device);
56
}
57
memset(gs, 0, 4);
58
for
(
int
i = 0; i < 3; i++)
59
{
60
gs[i] = readChar(device);
61
}
62
memset(bs, 0, 4);
63
for
(
int
i = 0; i < 3; i++)
64
{
65
bs[i] = readChar(device);
66
}
67
sscan(rs,
"%x"
, r);
68
sscan(gs,
"%x"
, g);
69
sscan(bs,
"%x"
, b);
70
}
71
72
73
serial
*colorPal_open(
int
sioPin)
74
{
75
int
rxpin = sioPin;
76
int
txpin = rxpin;
77
int
mode = 0b1100;
78
int
baudrate = 2400;
79
80
colorPal_t
*cpptr;
81
82
text_t
* text = (
text_t
*) malloc(
sizeof
(
text_t
));
83
84
// set pins first for boards that can misbehave intentionally like the Quickstart
85
if
(txpin >= SERIAL_MIN_PIN && txpin <= SERIAL_MAX_PIN) {
86
DIRA |= (1<<txpin);
87
OUTA |= (1<<txpin);
88
}
89
if
(rxpin >= SERIAL_MIN_PIN && rxpin <= SERIAL_MAX_PIN) {
90
DIRA &= ~(1<<rxpin);
91
}
92
93
//memset(text, 0, sizeof(text_t));
94
cpptr = (
colorPal_t
*) malloc(
sizeof
(
colorPal_t
));
95
text->
devst
= cpptr;
96
//memset((char*)serptr, 0, sizeof(Serial_t));
97
98
text->
txChar
= serial_txChar;
/* required for terminal to work */
99
text->
rxChar
= serial_rxChar;
/* required for terminal to work */
100
101
cpptr->rx_pin = rxpin;
/* recieve pin */
102
cpptr->tx_pin = txpin;
/* transmit pin*/
103
cpptr->mode = mode;
104
cpptr->baud = baudrate;
105
cpptr->ticks = CLKFREQ/baudrate;
/* baud from clkfreq (cpu clock typically 80000000 for 5M*pll16x) */
106
107
waitcnt(4*CLKFREQ/5+CNT);
108
colorPal_init(text);
109
110
return
text;
111
}
112
113
114
void
colorPal_close(
colorPal
*device)
115
{
116
if
(!device)
117
return
;
118
colorPal_t
*cp = (
colorPal_t
*) device->
devst
;
119
OUTA &= ~(1 << cp->tx_pin);
120
DIRA &= ~(1 << cp->tx_pin);
121
DIRA &= ~(1 << cp->rx_pin);
122
free((
void
*)device->
devst
);
123
free(device);
124
device = 0;
125
}
126
127
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::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