Skip to content

Commit 70fa56e

Browse files
author
brabo
committed
Merge pull request #1 from brabo/master
Merging in my i2c-touchscreen example
2 parents 7ac34c0 + 357124d commit 70fa56e

File tree

13 files changed

+1420
-0
lines changed

13 files changed

+1420
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
OBJS = sdram.o clock.o lcd-spi.o gfx.o stmpe811.o
2+
3+
BINARY = ts-i2c
4+
5+
# we use sin/cos from the library
6+
LDLIBS += -lm
7+
8+
LDSCRIPT = ../stm32f429i-discovery.ld
9+
10+
include ../../Makefile.include
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
README ts-i2c
2+
-----------------
3+
4+
This example sets up a very basic LCD display on the DISCO
5+
board and fills it with red.
6+
Pressing on the touchscreen will turn the screen green.
7+
8+
It uses some graphics code that was inspired by AdaFruit graphics library.
9+
10+
The stmpe811 example driver uses i2c to communicate with the
11+
touchscreen.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* This file is part of the libopencm3 project.
3+
*
4+
* Copyright (C) 2014 Chuck McManis <[email protected]>
5+
*
6+
* This library is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Lesser General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This library is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this library. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
/*
21+
* Now this is just the clock setup code from systick-blink as it is the
22+
* transferrable part.
23+
*/
24+
25+
#include <libopencm3/stm32/rcc.h>
26+
#include <libopencm3/cm3/nvic.h>
27+
#include <libopencm3/cm3/systick.h>
28+
29+
/* Common function descriptions */
30+
#include "clock.h"
31+
32+
/* milliseconds since boot */
33+
static volatile uint32_t system_millis;
34+
35+
/* Called when systick fires */
36+
void sys_tick_handler(void)
37+
{
38+
system_millis++;
39+
}
40+
41+
/* simple sleep for delay milliseconds */
42+
void msleep(uint32_t delay)
43+
{
44+
uint32_t wake = system_millis + delay;
45+
while (wake > system_millis);
46+
}
47+
48+
/* Getter function for the current time */
49+
uint32_t mtime(void)
50+
{
51+
return system_millis;
52+
}
53+
54+
/*
55+
* clock_setup(void)
56+
*
57+
* This function sets up both the base board clock rate
58+
* and a 1khz "system tick" count. The SYSTICK counter is
59+
* a standard feature of the Cortex-M series.
60+
*/
61+
void clock_setup(void)
62+
{
63+
/* Base board frequency, set to 168Mhz */
64+
rcc_clock_setup_hse_3v3(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_168MHZ]);
65+
66+
/* clock rate / 168000 to get 1mS interrupt rate */
67+
systick_set_reload(168000);
68+
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB);
69+
systick_counter_enable();
70+
71+
/* this done last */
72+
systick_interrupt_enable();
73+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* This include file describes the functions exported by clock.c
3+
*/
4+
#ifndef __CLOCK_H
5+
#define __CLOCK_H
6+
7+
/*
8+
* Definitions for functions being abstracted out
9+
*/
10+
void msleep(uint32_t);
11+
uint32_t mtime(void);
12+
void clock_setup(void);
13+
14+
#endif /* generic header protector */
15+
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* This is the core graphics library for all our displays, providing a common
3+
* set of graphics primitives (points, lines, circles, etc.). It needs to be
4+
* paired with a hardware-specific library for each display device we carry
5+
* (to handle the lower-level functions).
6+
*
7+
* Adafruit invests time and resources providing this open source code, please
8+
* support Adafruit & open-source hardware by purchasing products from Adafruit!
9+
*
10+
* Copyright (c) 2013 Adafruit Industries. All rights reserved.
11+
*
12+
* Redistribution and use in source and binary forms, with or without
13+
* modification, are permitted provided that the following conditions are met:
14+
*
15+
* - Redistributions of source code must retain the above copyright notice,
16+
* this list of conditions and the following disclaimer.
17+
* - Redistributions in binary form must reproduce the above copyright notice,
18+
* this list of conditions and the following disclaimer in the documentation
19+
* and/or other materials provided with the distribution.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31+
* POSSIBILITY OF SUCH DAMAGE.
32+
*
33+
* Modified the AdaFruit library to be a C library, changed the font and
34+
* generally munged it in a variety of ways, creating a reasonably quick
35+
* and dirty way to put something "interesting" on the LCD display.
36+
* --Chuck McManis (2013, 2014)
37+
*
38+
*/
39+
40+
#include <stdint.h>
41+
#include <math.h>
42+
#include <stdlib.h>
43+
#include "gfx.h"
44+
45+
struct gfx_state __gfx_state;
46+
47+
void
48+
gfx_draw_pixel(int x, int y, uint16_t color)
49+
{
50+
if ((x < 0) || (x >= __gfx_state._width) ||
51+
(y < 0) || (y >= __gfx_state._height)) {
52+
return; /* off screen so don't draw it */
53+
}
54+
(__gfx_state.drawpixel)(x, y, color);
55+
}
56+
#define true 1
57+
58+
void
59+
gfx_init(void (*pixel_func)(int, int, uint16_t), int width, int height)
60+
{
61+
__gfx_state._width = width;
62+
__gfx_state._height = height;
63+
__gfx_state.rotation = 0;
64+
__gfx_state.cursor_y = __gfx_state.cursor_x = 0;
65+
__gfx_state.textsize = 1;
66+
__gfx_state.textcolor = 0;
67+
__gfx_state.textbgcolor = 0xFFFF;
68+
__gfx_state.wrap = true;
69+
__gfx_state.drawpixel = pixel_func;
70+
}
71+
72+
/* Bresenham's algorithm - thx wikpedia */
73+
void gfx_draw_line(int16_t x0, int16_t y0,
74+
int16_t x1, int16_t y1,
75+
uint16_t color)
76+
{
77+
int16_t steep = abs(y1 - y0) > abs(x1 - x0);
78+
if (steep) {
79+
swap(x0, y0);
80+
swap(x1, y1);
81+
}
82+
83+
if (x0 > x1) {
84+
swap(x0, x1);
85+
swap(y0, y1);
86+
}
87+
88+
int16_t dx, dy;
89+
dx = x1 - x0;
90+
dy = abs(y1 - y0);
91+
92+
int16_t err = dx / 2;
93+
int16_t ystep;
94+
95+
if (y0 < y1) {
96+
ystep = 1;
97+
} else {
98+
ystep = -1;
99+
}
100+
101+
for (; x0 <= x1; x0++) {
102+
if (steep) {
103+
gfx_draw_pixel(y0, x0, color);
104+
} else {
105+
gfx_draw_pixel(x0, y0, color);
106+
}
107+
err -= dy;
108+
if (err < 0) {
109+
y0 += ystep;
110+
err += dx;
111+
}
112+
}
113+
}
114+
115+
void gfx_draw_fast_vline(int16_t x, int16_t y,
116+
int16_t h, uint16_t color)
117+
{
118+
/* Update in subclasses if desired! */
119+
gfx_draw_line(x, y, x, y + h - 1, color);
120+
}
121+
122+
void gfx_fill_rect(int16_t x, int16_t y, int16_t w, int16_t h,
123+
uint16_t color)
124+
{
125+
/* Update in subclasses if desired! */
126+
int16_t i;
127+
for (i = x; i < x + w; i++) {
128+
gfx_draw_fast_vline(i, y, h, color);
129+
}
130+
}
131+
132+
void gfx_fill_screen(uint16_t color)
133+
{
134+
gfx_fill_rect(0, 0, __gfx_state._width, __gfx_state._height, color);
135+
}
136+
137+
/* Return the size of the display (per current rotation) */
138+
uint16_t gfx_width(void)
139+
{
140+
return __gfx_state._width;
141+
}
142+
143+
uint16_t gfx_height(void)
144+
{
145+
return __gfx_state._height;
146+
}
147+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* A simple port of the AdaFruit minimal graphics code to my
3+
* demo code.
4+
*/
5+
#ifndef _GFX_H
6+
#define _GFX_H
7+
#include <stdint.h>
8+
9+
#define swap(a, b) { int16_t t = a; a = b; b = t; }
10+
11+
void gfx_draw_pixel(int x, int y, uint16_t color);
12+
void gfx_draw_line(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
13+
uint16_t color);
14+
void gfx_draw_fast_vline(int16_t x, int16_t y, int16_t h, uint16_t color);
15+
void gfx_fill_rect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
16+
void gfx_fill_screen(uint16_t color);
17+
18+
void gfx_init(void (*draw)(int, int, uint16_t), int, int);
19+
20+
uint16_t gfx_height(void);
21+
uint16_t gfx_width(void);
22+
23+
#define GFX_WIDTH 320
24+
#define GFX_HEIGHT 240
25+
26+
struct gfx_state {
27+
int16_t _width, _height, cursor_x, cursor_y;
28+
uint16_t textcolor, textbgcolor;
29+
uint8_t textsize, rotation;
30+
uint8_t wrap;
31+
void (*drawpixel)(int, int, uint16_t);
32+
};
33+
34+
extern struct gfx_state __gfx_state;
35+
36+
#define GFX_COLOR_WHITE 0xFFFF
37+
#define GFX_COLOR_BLACK 0x0000
38+
#define GFX_COLOR_GREY 0xF7DE
39+
#define GFX_COLOR_BLUE 0x001F
40+
#define GFX_COLOR_BLUE2 0x051F
41+
#define GFX_COLOR_RED 0xF800
42+
#define GFX_COLOR_MAGENTA 0xF81F
43+
#define GFX_COLOR_GREEN 0x07E0
44+
#define GFX_COLOR_CYAN 0x7FFF
45+
#define GFX_COLOR_YELLOW 0xFFE0
46+
47+
#endif /* _ADAFRUIT_GFX_H */

0 commit comments

Comments
 (0)