Welcome to mirror list, hosted at ThFree Co, Russian Federation.

u8g_dev_ht1632.c « utility « U8glib « libraries « Arduino_1.0.x « ArduinoAddons - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4977793f1e022806d469393fba1443db8822d0ee (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 
  u8g_dev_ht1632.c
 
  1-Bit (BW) Driver for HT1632 controller
 
  Universal 8bit Graphics Library
 
  Copyright (c) 2013, olikraus@gmail.com
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without modification,
  are permitted provided that the following conditions are met:
 
  * Redistributions of source code must retain the above copyright notice, this list
    of conditions and the following disclaimer.
   
  * Redistributions in binary form must reproduce the above copyright notice, this
    list of conditions and the following disclaimer in the documentation and/or other
    materials provided with the distribution.
 
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.



    U8G_PIN_NONE can be used as argument
   
    uint8_t u8g_InitSPI(u8g_t *u8g, u8g_dev_t *dev, uint8_t sck, uint8_t mosi, uint8_t cs, uint8_t a0, uint8_t reset)
    {
      ...      
      u8g->pin_list[U8G_PI_SCK] = sck;
      u8g->pin_list[U8G_PI_MOSI] = mosi;
      u8g->pin_list[U8G_PI_CS] = cs;
      u8g->pin_list[U8G_PI_A0] = a0;
      u8g->pin_list[U8G_PI_RESET] = reset;

mapping  

#define DATA_PIN --> U8G_PI_MOSI
#define WR_PIN    --> U8G_PI_SCK
#define CS_PIN      --> U8G_PI_CS
				    U8G_PI_A0 --> not used
				    U8G_PI_RESET --> not used

Usage:

    u8g_InitSPI(&u8g, &u8g_dev_ht1632_24x16, WR_PIN, DATA_IN, CS_PIN, U8G_PIN_NONE, U8G_PIN_NONE)

*/
 
#include "u8g.h"
 
#define WIDTH 24
#define HEIGHT 16
#define PAGE_HEIGHT 16
 
/* http://forum.arduino.cc/index.php?topic=168537.0 */
 
#define HT1632_CMD_SYSDIS       0x00    // CMD= 0000-0000-x Turn off oscil
#define HT1632_CMD_SYSON        0x01    // CMD= 0000-0001-x Enable system oscil
#define HT1632_CMD_LEDOFF       0x02    // CMD= 0000-0010-x LED duty cycle gen off
#define HT1632_CMD_LEDON        0x03    // CMD= 0000-0011-x LEDs ON
#define HT1632_CMD_BLOFF        0x08    // CMD= 0000-1000-x Blink OFF
#define HT1632_CMD_BLON         0x09    // CMD= 0000-1001-x Blink On
#define HT1632_CMD_SLVMD        0x10    // CMD= 0001-00xx-x Slave Mode
#define HT1632_CMD_MSTMD        0x14    // CMD= 0001-01xx-x Master Mode
#define HT1632_CMD_RCCLK        0x18    // CMD= 0001-10xx-x Use on-chip clock
#define HT1632_CMD_EXTCLK       0x1C    // CMD= 0001-11xx-x Use external clock
#define HT1632_CMD_COMS00       0x20    // CMD= 0010-ABxx-x commons options
#define HT1632_CMD_COMS01       0x24    // CMD= 0010-ABxx-x commons options
#define HT1632_CMD_COMS10       0x28    // CMD= 0010-ABxx-x commons options
#define HT1632_CMD_COMS11       0x2C    // P-MOS OUTPUT AND 16COMMON OPTION
#define HT1632_CMD_PWM          0xA0    // CMD= 101x-PPPP-x PWM duty cycle
 
#define HT1632_ID_CMD   4       /* ID = 100 - Commands */
#define HT1632_ID_RD    6       /* ID = 110 - Read RAM */
#define HT1632_ID_WR    5       /* ID = 101 - Write RAM */
 
#define HT1632_ID_LEN           3               // IDs are 3 bits
#define HT1632_CMD_LEN          8               // CMDs are 8 bits
#define HT1632_DATA_LEN         8               // Data are 4*2 bits
#define HT1632_ADDR_LEN         7               // Address are 7 bits
 
#if defined(ARDUINO)
 
#if ARDUINO < 100
#include <WProgram.h>
#else
#include <Arduino.h>
#endif
 
//#define WR_PIN 3
//#define DATA_PIN 2
//#define CS_PIN 4
 
void ht1632_write_data_MSB(u8g_t *u8g, uint8_t cnt, uint8_t data, uint8_t extra)
{
  int8_t i;
  uint8_t data_pin = u8g->pin_list[U8G_PI_MOSI];
  uint8_t wr_pin = u8g->pin_list[U8G_PI_SCK];
 
  for(i = cnt - 1; i >= 0; i--)
  {
    if ((data >> i) & 1)
    {  
      digitalWrite(data_pin, HIGH);
    }
    else
    {
      digitalWrite(data_pin, LOW);
    }
 
    digitalWrite(wr_pin, LOW);
    u8g_MicroDelay();
    digitalWrite(wr_pin, HIGH);
    u8g_MicroDelay();
  }
 
  // Send an extra bit
  if (extra)
  {
    digitalWrite(data_pin, HIGH);
    digitalWrite(wr_pin, LOW);
    u8g_MicroDelay();
    digitalWrite(wr_pin, HIGH);
    u8g_MicroDelay();
  }
}
 
void ht1632_write_data(u8g_t *u8g, uint8_t cnt, uint8_t data)
{
  uint8_t i;
  uint8_t data_pin = u8g->pin_list[U8G_PI_MOSI];
  uint8_t wr_pin = u8g->pin_list[U8G_PI_SCK];
  for (i = 0; i < cnt; i++)
  {
 
    if ((data >> i) & 1) {
      digitalWrite(data_pin, HIGH);
    }
    else {
      digitalWrite(data_pin, LOW);
    }
 
    digitalWrite(wr_pin, LOW);
    u8g_MicroDelay();
    digitalWrite(wr_pin, HIGH);
    u8g_MicroDelay();
  }
}
 
 
void ht1632_init(u8g_t *u8g)
{
  //uint8_t i;
  uint8_t data_pin = u8g->pin_list[U8G_PI_MOSI];
  uint8_t wr_pin = u8g->pin_list[U8G_PI_SCK];
  uint8_t cs_pin = u8g->pin_list[U8G_PI_CS];
  pinMode(data_pin, OUTPUT);
  pinMode(wr_pin, OUTPUT);
  pinMode(cs_pin, OUTPUT);
 
  digitalWrite(data_pin, HIGH);
  digitalWrite(wr_pin, HIGH);
  digitalWrite(cs_pin, HIGH);
 
  digitalWrite(cs_pin, LOW);
  /* init display once after startup */
  ht1632_write_data_MSB(u8g, 3, HT1632_ID_CMD, false); // IDs are 3 bits
  ht1632_write_data_MSB(u8g, 8, HT1632_CMD_SYSDIS, true); // 8 bits
  ht1632_write_data_MSB(u8g, 8, HT1632_CMD_SYSON, true); // 8 bits
  ht1632_write_data_MSB(u8g, 8, HT1632_CMD_COMS11, true); // 8 bits
  ht1632_write_data_MSB(u8g, 8, HT1632_CMD_LEDON, true); // 8 bits
  ht1632_write_data_MSB(u8g, 8, HT1632_CMD_BLOFF, true); // 8 bits
  ht1632_write_data_MSB(u8g, 8, HT1632_CMD_PWM+15, true); // 8 bits  
  digitalWrite(cs_pin, HIGH);
 
  /* removed following (debug) code */
  /*
  digitalWrite(cs_pin, LOW);
  ht1632_write_data_MSB(u8g, 3, HT1632_ID_WR, false); // Send "write to display" command
  ht1632_write_data_MSB(u8g, 7, 0, false);
  for(i = 0; i<48; ++i)
  {
    ht1632_write_data(u8g, 8, 0xFF);
  }
  digitalWrite(cs_pin, HIGH);
  */
}
 
/*
  page: 0=data contain lines 0..16, 1=data contain lines 16..32  (a 24x16 display will only have page 0)
  cnt: width of the display
  data: pointer to a buffer with 2*cnt bytes.
*/
void ht1632_transfer_data(u8g_t *u8g, uint8_t page, uint8_t cnt, uint8_t *data)
{
  uint8_t addr;
  uint8_t cs_pin = u8g->pin_list[U8G_PI_CS];
  /* send data to the ht1632 */
  digitalWrite(cs_pin, LOW);
  ht1632_write_data_MSB(u8g, 3, HT1632_ID_WR, false); // Send "write to display" command
  ht1632_write_data_MSB(u8g, 7, page*2*cnt, false); 
  
  // Operating in progressive addressing mode
  for (addr = 0; addr < cnt; addr++)
  {
    ht1632_write_data(u8g, 8, data[addr]);  
    ht1632_write_data(u8g, 8, data[addr+cnt]);
  }  
  digitalWrite(cs_pin, HIGH);
}

/* value is between 0...15 */
void ht1632_set_contrast(u8g_t *u8g, uint8_t value)
{
  uint8_t cs_pin = u8g->pin_list[U8G_PI_CS];
  digitalWrite(cs_pin, LOW);
  ht1632_write_data_MSB(u8g, 3, HT1632_ID_CMD, false);
  ht1632_write_data_MSB(u8g, 8, HT1632_CMD_PWM + value, false);
  digitalWrite(cs_pin, HIGH);
}
 
#else
void ht1632_init(u8g_t *u8g)
{
}
 
void ht1632_transfer_data(u8g_t *u8g, uint8_t page, uint8_t cnt, uint8_t *data)
{
}

void ht1632_set_contrast(u8g_t *u8g, uint8_t value)
{
}

#endif /* ARDUINO */
 
 
uint8_t u8g_dev_ht1632_24x16_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
{
  switch(msg)
  {
    case U8G_DEV_MSG_INIT:
      ht1632_init(u8g);
      break;
    case U8G_DEV_MSG_STOP:
      break;
    case U8G_DEV_MSG_PAGE_NEXT:
      {
	u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
       
	/* current page: pb->p.page */
	/* ptr to the buffer: pb->buf */
	ht1632_transfer_data(u8g, pb->p.page, WIDTH, pb->buf);
      }
      break;
    case U8G_DEV_MSG_CONTRAST:
      /* values passed to SetContrast() are between 0 and 255, scale down to 0...15 */
      ht1632_set_contrast(u8g, (*(uint8_t *)arg) >> 4);
    return 1;
  }
  return u8g_dev_pb16v1_base_fn(u8g, dev, msg, arg);
}
 
uint8_t u8g_dev_ht1632_24x16_buf[WIDTH*2] U8G_NOCOMMON ; 
u8g_pb_t u8g_dev_ht1632_24x16_pb = { {16, HEIGHT, 0, 0, 0},  WIDTH, u8g_dev_ht1632_24x16_buf}; 
u8g_dev_t u8g_dev_ht1632_24x16 = { u8g_dev_ht1632_24x16_fn, &u8g_dev_ht1632_24x16_pb, u8g_com_null_fn };