mirror of
https://github.com/geekammo/MicroView-Arduino-Library.git
synced 2026-03-03 00:34:06 +01:00
added pixel line clear
This commit is contained in:
149
MicroView.cpp
149
MicroView.cpp
@@ -2,36 +2,70 @@
|
||||
#include <MicroView.h>
|
||||
#include <SPI.h>
|
||||
|
||||
/*
|
||||
Screen memory buffer 64 x 48 divided by 8 = 384 bytes
|
||||
Screen memory buffer is required because in SPI mode, the host cannot read the GDRAM of the controller. This buffer serves as a scratch RAM for graphical functions.
|
||||
*/
|
||||
static uint8_t screenmemory [] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0xC0, 0xC0,
|
||||
0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80,
|
||||
0xC0, 0xC0, 0xE0, 0xE0, 0xF0, 0x78, 0xF8, 0xBC, 0xFC, 0xDE, 0xDE, 0xCF, 0xCF, 0xC7, 0xC3, 0xC3,
|
||||
0xC7, 0x87, 0x8F, 0x0F, 0x1E, 0x1E, 0x3C, 0x78, 0x78, 0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0xC0, 0x80,
|
||||
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,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00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F,
|
||||
0x0F, 0x07, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
void MICROVIEW::begin() {
|
||||
// SPI
|
||||
SPI.setClockDivider(SPI_CLOCK_DIV2);
|
||||
SPI.begin();
|
||||
// Setting up SPI pins
|
||||
pinMode(MOSI, OUTPUT);
|
||||
pinMode(SCK, OUTPUT);
|
||||
pinMode(DC, OUTPUT);
|
||||
pinMode(RESET, OUTPUT);
|
||||
pinMode(cs, OUTPUT);
|
||||
digitalWrite(cs, HIGH);
|
||||
pinMode(CS, OUTPUT);
|
||||
digitalWrite(CS, HIGH);
|
||||
|
||||
sckport = portOutputRegister(digitalPinToPort(SCK));
|
||||
sckpinmask = digitalPinToBitMask(SCK);
|
||||
mosiport = portOutputRegister(digitalPinToPort(MOSI));
|
||||
mosipinmask = digitalPinToBitMask(MOSI);
|
||||
csport = portOutputRegister(digitalPinToPort(cs));
|
||||
cspinmask = digitalPinToBitMask(cs);
|
||||
csport = portOutputRegister(digitalPinToPort(CS));
|
||||
cspinmask = digitalPinToBitMask(CS);
|
||||
dcport = portOutputRegister(digitalPinToPort(DC));
|
||||
dcpinmask = digitalPinToBitMask(DC);
|
||||
|
||||
digitalWrite(RESET, HIGH);
|
||||
// VDD (3.3V) goes high at start, lets just chill for a ms
|
||||
delay(1);
|
||||
// VDD (3.3V) goes high at start, lets just chill for 5 ms
|
||||
delay(5);
|
||||
// bring reset low
|
||||
digitalWrite(RESET, LOW);
|
||||
|
||||
// Setup SPI frequency
|
||||
SPI.setClockDivider(SPI_CLOCK_DIV2);
|
||||
SPI.begin();
|
||||
|
||||
// wait 10ms
|
||||
delay(10);
|
||||
// bring out of reset
|
||||
digitalWrite(RESET, HIGH);
|
||||
// turn on VCC
|
||||
|
||||
// Init sequence for 64x48 OLED module
|
||||
command(DISPLAYOFF); // 0xAE
|
||||
|
||||
@@ -52,10 +86,6 @@ void MICROVIEW::begin() {
|
||||
command(NORMALDISPLAY); // 0xA6
|
||||
command(DISPLAYALLONRESUME); // 0xA4
|
||||
|
||||
|
||||
// command(MEMORYMODE); // 0x20
|
||||
// command(0x00); // Horizontal Memory Addressing mode
|
||||
|
||||
command(SEGREMAP | 0x1);
|
||||
|
||||
command(COMSCANDEC);
|
||||
@@ -105,14 +135,91 @@ void MICROVIEW::setColumnAddress(uint8_t add) {
|
||||
return;
|
||||
}
|
||||
|
||||
void MICROVIEW::clear(void) {
|
||||
for (int i=0;i<8; i++) {
|
||||
setPageAddress(i);
|
||||
setColumnAddress(0);
|
||||
for (int j=0; j<0x80; j++) {
|
||||
data(0);
|
||||
|
||||
/*
|
||||
Clear GDRAM inside the LCD controller - mode = ALL
|
||||
Clear screen page buffer - mode = PAGE
|
||||
*/
|
||||
void MICROVIEW::clear(uint8_t mode) {
|
||||
uint8_t page=6, col=0x40;
|
||||
if (mode==ALL) {
|
||||
|
||||
for (int i=0;i<8; i++) {
|
||||
setPageAddress(i);
|
||||
setColumnAddress(0);
|
||||
for (int j=0; j<0x80; j++) {
|
||||
data(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
memset(screenmemory,0,384); // (64 x 48) / 8 = 384
|
||||
display();
|
||||
}
|
||||
}
|
||||
|
||||
void MICROVIEW::display(void) {
|
||||
uint8_t i, j;
|
||||
|
||||
for (i=0; i<6; i++) {
|
||||
setPageAddress(i);
|
||||
setColumnAddress(0);
|
||||
for (j=0;j<0x40;j++) {
|
||||
data(screenmemory[i*0x40+j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MICROVIEW::pixel(uint8_t x, uint8_t y, uint8_t color) {
|
||||
if ((x<0) || (x>LCDWIDTH-1) || (y<0) || (y>LCDHEIGHT-1))
|
||||
return;
|
||||
|
||||
if (color==WHITE)
|
||||
screenmemory[x+ (y/8)*LCDWIDTH] |= _BV((y%8));
|
||||
else
|
||||
screenmemory[x+ (y/8)*LCDWIDTH] &= ~_BV((y%8));
|
||||
|
||||
//display();
|
||||
}
|
||||
|
||||
// bresenham's algorithm
|
||||
void MICROVIEW::line(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint8_t color) {
|
||||
uint8_t steep = abs(y1 - y0) > abs(x1 - x0);
|
||||
if (steep) {
|
||||
swap(x0, y0);
|
||||
swap(x1, y1);
|
||||
}
|
||||
|
||||
if (x0 > x1) {
|
||||
swap(x0, x1);
|
||||
swap(y0, y1);
|
||||
}
|
||||
|
||||
uint8_t dx, dy;
|
||||
dx = x1 - x0;
|
||||
dy = abs(y1 - y0);
|
||||
|
||||
int8_t err = dx / 2;
|
||||
int8_t ystep;
|
||||
|
||||
if (y0 < y1) {
|
||||
ystep = 1;
|
||||
} else {
|
||||
ystep = -1;}
|
||||
|
||||
for (; x0<x1; x0++) {
|
||||
if (steep) {
|
||||
pixel(y0, x0, color);
|
||||
} else {
|
||||
pixel(x0, y0, color);
|
||||
}
|
||||
err -= dy;
|
||||
if (err < 0) {
|
||||
y0 += ystep;
|
||||
err += dx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MICROVIEW::stopScroll(void){
|
||||
@@ -121,7 +228,7 @@ void MICROVIEW::stopScroll(void){
|
||||
|
||||
void MICROVIEW::scrollRight(uint8_t start, uint8_t stop){
|
||||
if (stop<start) // stop must be larger or equal to start
|
||||
return;
|
||||
return;
|
||||
stopScroll(); // need to disable scrolling before starting to avoid memory corrupt
|
||||
command(RIGHTHORIZONTALSCROLL);
|
||||
command(0x00);
|
||||
|
||||
28
MicroView.h
28
MicroView.h
@@ -7,11 +7,13 @@
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
// CS is defined when constructing MICROVIEW class, thus the library can control multiple slave MicroViews.
|
||||
#define DC 8
|
||||
#define SCK 13
|
||||
#define MOSI 11
|
||||
#define RESET 12
|
||||
#define swap(a, b) { uint8_t t = a; a = b; b = t; }
|
||||
|
||||
#define DC 8
|
||||
#define SCK 13
|
||||
#define MOSI 11
|
||||
#define RESET 12
|
||||
#define CS 10
|
||||
|
||||
#define BLACK 0
|
||||
#define WHITE 1
|
||||
@@ -19,6 +21,9 @@
|
||||
#define LCDWIDTH 64
|
||||
#define LCDHEIGHT 48
|
||||
|
||||
#define PAGE 0
|
||||
#define ALL 1
|
||||
|
||||
#define SETCONTRAST 0x81
|
||||
#define DISPLAYALLONRESUME 0xA4
|
||||
#define DISPLAYALLON 0xA5
|
||||
@@ -54,17 +59,22 @@
|
||||
|
||||
class MICROVIEW {
|
||||
public:
|
||||
MICROVIEW(uint8_t CS) :cs(CS) {}
|
||||
MICROVIEW(void) {};
|
||||
void begin(void);
|
||||
void command(uint8_t c);
|
||||
void data(uint8_t c);
|
||||
void clear(void);
|
||||
void clear(uint8_t mode);
|
||||
void invert(uint8_t i);
|
||||
void display(void);
|
||||
void drawPixel(uint8_t x, uint8_t y, uint8_t color);
|
||||
void pixel(uint8_t x, uint8_t y, uint8_t color);
|
||||
void line(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint8_t color);
|
||||
|
||||
void drawBitmap(void);
|
||||
|
||||
|
||||
void setColumnAddress(uint8_t add);
|
||||
void setPageAddress(uint8_t add);
|
||||
|
||||
|
||||
void scrollRight(uint8_t start, uint8_t stop);
|
||||
void scrollLeft(uint8_t start, uint8_t stop);
|
||||
@@ -74,7 +84,7 @@ class MICROVIEW {
|
||||
|
||||
|
||||
private:
|
||||
uint8_t cs;
|
||||
//uint8_t cs;
|
||||
volatile uint8_t *mosiport, *sckport, *csport, *dcport; // use volatile because these are fixed location port address
|
||||
uint8_t mosipinmask, sckpinmask, cspinmask, dcpinmask;
|
||||
};
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
MicroView KEYWORD1
|
||||
MICROVIEW KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
@@ -15,6 +15,7 @@ MicroView KEYWORD1
|
||||
begin KEYWORD2
|
||||
clear KEYWORD2
|
||||
home KEYWORD2
|
||||
display KEYWORD2
|
||||
|
||||
|
||||
#######################################
|
||||
|
||||
Reference in New Issue
Block a user