Skip to content

Instantly share code, notes, and snippets.

@stravant
Created October 30, 2012 16:46
Show Gist options
  • Save stravant/3981441 to your computer and use it in GitHub Desktop.
Save stravant/3981441 to your computer and use it in GitHub Desktop.
Mapping
/* Simple Image Drawing
*
* Draws an image to the screen. The image is stored in "parrot.lcd" on
* the SD card. The image file contains only raw pixel byte-pairs.
*/
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include <SD.h>
// standard U of A library settings, assuming Atmel Mega SPI pins
#define SD_CS 5 // Chip select line for SD card
#define TFT_CS 6 // Chip select line for TFT display
#define TFT_DC 7 // Data/command line for TFT
#define TFT_RST 8 // Reset line for TFT (or connect to +5V)
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
#define BLOCK_LEN 512
///////////////////////////////////////////////////////////////////////////////
class SdData {
public:
SdData(): mReady(false) {
if (SD.begin(SD_CS) && Card.init(SPI_HALF_SPEED, SD_CS))
mReady = true;
}
bool isReady() const {return mReady; }
Sd2Card Card;
private:
bool mReady;
};
SdData Memory;
///////////////////////////////////////////////////////////////////////////////
class SelectLocationGui {
public:
SelectLocationGui(): mCursorLastDrawnAtX(0), mCursorLastDrawnAtY(0),
mCursorX(64), mCursorY(80),
mOriginX(0), mOriginY(0), mZoom(1) {
}
void move(int dx, int dy) {
mCursorX += dx*mZoom;
mCursorY += dy*mZoom;
//keep cursor in bounds
if (mCursorX < 0) mCursorX = 0;
if (mCursorY < 0) mCursorY = 0;
if (mCursorX > 2048) mCursorX = 2048;
if (mCursorY > 2048) mCursorY = 2048;
//redraw the cursor dirty region
drawCursor();
//if the cursor is near the edge of the viewport, then shift
if (mCursorX < mOriginX+mBorder*mZoom) shift(-mBorder*mZoom*2, 0);
if (mCursorY < mOriginY+mBorder*mZoom) shift(0, -mBorder*mZoom*2);
if (mCursorX > mOriginX+(128-mBorder)*mZoom) shift(mBorder*mZoom*2, 0);
if (mCursorY > mOriginY+(160-mBorder)*mZoom) shift(0, mBorder*mZoom*2);
}
int getCursorX() const {return mCursorX; }
int getCursorY() const {return mCursorY; }
// 1 => out, 0 => none, -1 => in
void zoom(int dir) {
int xofs = -(mCursorX-mOriginX);
int yofs = -(mCursorY-mOriginY);
xofs /= mZoom;
yofs /= mZoom;
//
mZoom += dir;
if (mZoom > 4) mZoom = 4;
if (mZoom < 1) mZoom = 1;
//
mOriginX = mCursorX + xofs*mZoom;
mOriginY = mCursorY + yofs*mZoom;
//
show();
}
void show() {
draw(0, 0, 128, 160);
}
private:
void shift(int dx, int dy) {
int oldOriginX = mOriginX, oldOriginY = mOriginY;
mOriginX += dx;
mOriginY += dy;
if (mOriginX < 0)
mOriginX = 0;
if (mOriginX > 2048-128)
mOriginX = 2048-128;
if (mOriginY < 0)
mOriginY = 0;
if (mOriginY > 2048-160)
mOriginY = 2048-160;
if (mOriginX != oldOriginX || mOriginY != oldOriginY) {
show();
}
}
void draw(int32_t x, int32_t y, int32_t w, int32_t h, bool dodrawCursor = true) {
tft.setAddrWindow(x, y, x+w-1, y+h-1);
//
x *= mZoom;
y *= mZoom;
x += mOriginX;
y += mOriginY;
w *= mZoom;
h *= mZoom;
//
if (x < 0) {
//todo:
}
if (y < 0) {
//todo:
}
//
File file = SD.open("yeg-big.lcd");
int32_t ImWidth = 2048;
int32_t ImHeight = 2048;
//
for (int32_t row = y; row < y+h; row += mZoom) {
uint16_t pixels[w*mZoom];
file.seek((ImWidth*2)*row + 2*x);
file.read((uint8_t*)pixels, 2*w*mZoom);
for (int i = 0; i < w; i += mZoom)
tft.pushColor((pixels[i] >> 8) | (pixels[i] << 8));
}
if (dodrawCursor)
drawCursor();
//
file.close();
}
void drawCursor() {
draw((mCursorLastDrawnAtX-mOriginX)/mZoom - mCursorSize/2,
(mCursorLastDrawnAtY-mOriginY)/mZoom - mCursorSize/2,
mCursorSize+1, mCursorSize+1, false);
//
tft.drawRect((mCursorX-mOriginX)/mZoom - mCursorSize/2,
(mCursorY-mOriginY)/mZoom - mCursorSize/2,
mCursorSize+1, mCursorSize+1, tft.Color565(0x00, 0x00, 0x00));
//
tft.drawRect((mCursorX-mOriginX)/mZoom, (mCursorY-mOriginY)/mZoom, 1, 1,
tft.Color565(0x00, 0x00, 0x00));
//
mCursorLastDrawnAtX = mCursorX;
mCursorLastDrawnAtY = mCursorY;
}
private:
int mCursorLastDrawnAtX, mCursorLastDrawnAtY;
int mCursorX;
int mCursorY;
int mOriginX;
int mOriginY;
int mZoom;
static const int mBorder = 25;
static const int mCursorSize = 10;
};
///////////////////////////////////////////////////////////////////////////////
class ListPlacesGui {
public:
typedef char Entry[64];
typedef void (EntryProvider*)(int index, char* buf, int len);
public:
ListPlacesGui() {
}
void setEntryProvider(EntryProvider* prov) const {mEntryProvider = prov; }
void show() {
}
void scroll(int dir) {
}
private:
EntryProvider* mEntryProvider;
int mFirstEntry;
int mEntryCount;
};
void setup(void) {
Serial.begin(9600);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
tft.initR(INITR_REDTAB);
// clear to yellow
tft.fillScreen(tft.Color565(0xff, 0xff, 0x00));
SelectLocationGui gui;
gui.show();
int joyCenterX = analogRead(7), joyCenterY = analogRead(6);
{
int lastXMove = 0;
int lastYMove = 0;
while (true) {
int joyX = (analogRead(7)-joyCenterX), joyY = (analogRead(6)-joyCenterY);
int tm = millis();
if (abs(joyX) > 20 && (tm-lastXMove)*5 > (1024-abs(joyX))) {
lastXMove = tm;
gui.move(joyX/abs(joyX)*5, 0);
}
if (abs(joyY) > 20 && (tm-lastYMove)*5 > (1024-abs(joyY))) {
lastYMove = tm;
gui.move(0, joyY/abs(joyY)*5);
}
Serial.print(digitalRead(10));
if (!digitalRead(10)) {
gui.zoom(1);
}
if (!digitalRead(11)) {
gui.zoom(-1);
}
}
}
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment