summaryrefslogtreecommitdiffstats
path: root/source/qFix/qfixLCD.h
blob: 9ecd93f34a6cb2708ed6437354c9ff93dda15e42 (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//------------------------------------------------------------------
// qfixLCD.h
//
// This file contains the class LCD which represents the
// LCD display.
//
// Copyright 2004 by KTB mechatronics GmbH
// Author: Stefan Enderle
//------------------------------------------------------------------

#ifndef qfixLCD_h
#define qfixLCD_h


#include "qfixI2C.h"
#include "qfixI2CDefs.h"
#include "string.h"


static const uint8_t LCD_CMD_CHANGEID   = 0;
static const uint8_t LCD_CMD_CLEAR      = 1;
static const uint8_t LCD_CMD_PRINTSTR   = 2;
static const uint8_t LCD_CMD_PRINTSTRXY = 3;
static const uint8_t LCD_CMD_LIGHTON    = 4;
static const uint8_t LCD_CMD_LIGHTOFF   = 5;
static const uint8_t LCD_CMD_PRINTINT   = 6;
static const uint8_t LCD_CMD_PRINTINTXY = 7;



/**
* \class LCD
* \brief Represents the qfix LC-display.
* \author Stefan Enderle
*
* The class LCD represents the
* physical LC-display and can be used to output text.
* The I2C bus is completely abstracted.
*/
class LCD
{
private:
  I2C i2c;
  uint8_t id;

public:
  /** Default constructor. If only one LC-display is used,
  * this constructor can be used. It sets the internal ID to 0.
  */
  LCD();

  /** Constructor with given ID.
  * If two or more LC-displays are used,
  * this constructor must be used. It sets the internal ID to the given value.
  */
  LCD(uint8_t ID);

  /** Change logical device ID
  */
  void changeID(uint8_t newID);

  /** Clears the display
  */
  void clear();

  /** prints the string data at the current cursor position.
   *  The string must end with 0.
  */
  void print(char* data);

  /** Prints the string data at position line/col. The string
   * must end with 0.
  */
  void print(uint8_t line, uint8_t col, char* data);

  /** prints the integer data at the current cursor position.
  */
  void print(int data);

  /** Prints the integer data at position line/col. 
  */
  void print(uint8_t line, uint8_t col, int data);

  /** Puts on the backlight. 
  * \see lightOff
  */
  void lightOn();

  /** Puts off the backlight. 
  * \see lightOff
  */
  void lightOff();
};



LCD::LCD() : i2c(), id(0)
{
}


LCD::LCD(uint8_t ID) : i2c(), id(ID)
{
}


void LCD::clear()
{
  uint8_t buf[2];

  buf[0] = id;
  buf[1] = LCD_CMD_CLEAR;
  i2c.send(I2C_ADDR_LCD, buf, 2);
}


void LCD::print(char* data)
{
  int len=strlen(data);
  uint8_t buf[len+2];

  buf[0] = id;
  buf[1] = LCD_CMD_PRINTSTR;
  for (int i=0; i<len; i++) buf[i+2] = uint8_t(data[i]);

  i2c.send(I2C_ADDR_LCD, buf, len+2);
}


void LCD::print(uint8_t line, uint8_t col, char* data)
{
  int len=strlen(data);
  uint8_t buf[len+4];

  buf[0] = id;
  buf[1] = LCD_CMD_PRINTSTRXY;
  buf[2] = line;
  buf[3] = col;
  for (int i=0; i<len; i++) buf[i+4] = uint8_t(data[i]);

  i2c.send(I2C_ADDR_LCD, buf, len+4);
}


void LCD::print(int data)
{
  int len=sizeof(int);
  uint8_t buf[len+2];
  uint8_t* ptr = (uint8_t*) &data;    // pointer to data

  buf[0] = id;
  buf[1] = LCD_CMD_PRINTINT;
  for (int i=0; i<len; i++) {
    buf[i+2] = *ptr;
    ptr++;
  }

  i2c.send(I2C_ADDR_LCD, buf, len+2);
}


void LCD::print(uint8_t line, uint8_t col, int data)
{
  int len=sizeof(int);
  uint8_t buf[len+4];
  uint8_t* ptr = (uint8_t*) &data;    // pointer to data

  buf[0] = id;
  buf[1] = LCD_CMD_PRINTINTXY;
  buf[2] = line;
  buf[3] = col;
  for (int i=0; i<len; i++) {
    buf[i+4] = *ptr;
    ptr++;
  }

  i2c.send(I2C_ADDR_LCD, buf, len+4);
}


void LCD::lightOn()
{
  uint8_t buf[2];

  buf[0] = id;
  buf[1] = LCD_CMD_LIGHTON;
  i2c.send(I2C_ADDR_LCD, buf, 2);
}

void LCD::lightOff()
{
  uint8_t buf[2];

  buf[0] = id;
  buf[1] = LCD_CMD_LIGHTOFF;
  i2c.send(I2C_ADDR_LCD, buf, 2);
}


void LCD::changeID(uint8_t newID)
{
  uint8_t buf[3];

  buf[0] = id;
  buf[1] = LCD_CMD_CHANGEID;
  buf[2] = newID;
  i2c.send(I2C_ADDR_LCD, buf, 3);

  id = newID;
}


#endif