A web server for monitoring and controlling a MakerBot Replicator over the USB host and ethernet.

Dependencies:   IAP NTPClient RTC mbed-rtos mbed Socket lwip-sys lwip BurstSPI

Fork of LPC1768_Mini-DK by Frank Vannieuwkerke

Makerbot Server for LPC1768 Copyright (c) 2013, jake (at) allaboutjake (dot) 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.
  • The name of the author and/or copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

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, AUTHOR, OR ANY 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.

Warnings:

This is not a commercial product or a hardened and secure network appliance. It is intended as a thought experiment or proof of concept and should not be relied upon in any way. Always operate your 3D printer in a safe and controlled manner.

Do not connect this directly to the exposed internet. It is intended to be behind a secure firewall (and NAT) such that it will only accept commands from the local network. Imagine how much fun a hacker could have instructing your 3D printer to continually print Standford bunnies. Well it could be much worse then that- a malicious user could send commands that could crash your machine (both in the software sense, as well as in the "smash your moving parts against the side of the machine repeatedly sense), overheat your extruders, cause your build plate to catch fire, and do severe damage to the machine, any surrounding building and propery. You have been warned.

Never print unattended and be ready to step in and stop the machine if something goes wrong. Keep in mind, a 3D printer has heaters that are operating at high temperatures, and if something starts to burn, it could cause damage to the machine, other property, and/or hurt yourself, pets, or others.

You should understand what you are doing. The source code here is not intended as a finished product or set of step by step instructions. You should engineer your own solution, which may wind up being better than mine.

Proceed at your own risk. You've been warned. (Several times) If you break your Makerbot, burn your house down, or injure yourself or others, I take no responsibility.

Introduction

I've been working on a side project to solve the "last mile" problem for people wanting to print from the network on their bots. I feel like the first half of the problem is solved with the FlashAir- getting the files to the card. The next step is a lightweight way of sending the "play back capture" command to the bot.

I looked around for a microcontroller platform that supports both networking and can function as a USB host. I happened to have an mbed (mbed) on hand that fit the bill. The mbed also has a working online toolchain (you need to own an mbed to gain access to the compiler). Some people don't like the online development environment, but I'm a fan of "working" and "Mac compatible." It was a good start, but cost wise, you would need an mbed LPC1768 module and some sort of carrier board that has both USB host and ethernet, or rig up your own connector solution. I happened to also have a Seedstudio mbed shield carrier board. This provides ethernet and USB connectors, but is another $25, putting the solution at around $75.

I also had an LPC1768 development board here called the "Mini-DK2". It has a USB host and a wired ethernet connector on board (search ebay if you're interested). It's a single-board solution that costs only $32 (and for $40 you can get one with a touchscreen) Its the cheapest development board I've seen with both USB host and an ethernet connector. I considered RasPi, but I'm not on that bandwagon. Since I had the Mini-DK2 on hand from another project that never went anywhere, I moved from the mbed module and carrier board to the DK2.

The mbed environment can compile binaries that work on the DK2 (again, you need to own at least one 1768 mbed already to get a license to use the compiler), and the mbed libraries provide some nice features. A USB Host library and and Ethernet library were readily available. The USBHost library didn't quite work out of the box. It took some time and more learning about the USB protocols than I would have liked, but I have the board communicating over the USB Host and the Makerbot.

Changes to stock mbed libraries

Many libraries are imported, but then converted to folders as to unlink them.

mbed provides a USHost library that includes a USBHostSerial object for connecting to CDC serial devices. Unfortunately, it did not work for me out of the box. I spent some time learning about USB protocols. One good reference is [Jan Axelson's Lakeview Research](http://www.lvr.com/usb_virtual_com_port.htm) discussion about CDC.

I found that the stock library was sending the control transfers to Interface 1. From what I understand, the control transfers needed to go to interface 0. I modified the USBHostSerial library to correct this, and the serial port interface came to life.

Next, I found that I wasn't able to get reliable communication. I traced it to what I think is an odd C++ inheritance and override problem. The USBHostSerial class implements the Stream interface, allowing printf/scanf operations. This is done by overriding the virtual _getc and _putc methods. Unfortunately, and for a reason I can't understand, these methods were not being called consistently. Sometimes they would work, but other times they would not. My solution was to implement transmit/receive methods with different names, and since the names were different, they seemed to get called consistently. I'd like to learn exactly what's going on here, but I don't feel like debugging it for academic purposes when it works just fine with the added methods.

Usage

Connect up your chosen dev board to power, ethernet and the USB host to the Makerbot's USB cable. The Mini-DK uses a USB-OTG adapter for the USB host. If you're using a Mini-DK board with an LCD, it will inform you of it's IP address on the display. This means it is now listening for a connection on port 7654.

If you are using an mbed dev board, or a Mini-DK without a display, the message will be directed to the serial console. Connect your computer to the appropriate port at a baud rate of 115200 to see the messages.

Use a telnet client to connect to the given IP address at port 7654. Telnet clients typically revert to "line mode" on ports other than 21. This means you get a local echo and the command isn't sent until you press enter.

Once connected, you can send the following commands:

A <username>:<password> : Set a username & password for the web interface and the telnet interface. Use the format shown with a colon separating the username from the password.

V : Print the version and Makerbot name, as well as the local firmware version (the Makerbot_Server firmware as discussed here).

B <filename.x3g> : Build from SD the given filename. According tot he protocol spec, this command is limited to 12 characters, so 8.3 filenames only.

P : Pause an active build

R : Resume active build

C : Cancel build- note that this immediately halts the build and does not clear the build area. You might want to pause the build first, and then cancel shortly after to make sure the nozzle isn't left hot and in contact with a printed part.

S : Print build status, tool and platform temps

Q : Quit and logout

The Mini-DK has two onboard buttons (besides the ISP and reset buttons). Currently one button will trigger a pause (if the Makerbot is printing) and the other will resume (if the Makerbot it paused)

Compiling

Edit "Target.h" to set whether you're building for an MBED module or the Mini-DK2

Installation

If you are using a mbed, then you can simply load the BIN file to the mbed using the mass storage bootloader. The mbed mounts as if it were a USB thumbdrive, and you copy the BIN file to the drive. After a reset, you're running the installed firmware.

The MiniDK has a serial bootloader. You connect to this bootloader from the "top" USB connector (not the USB host one). Hold down the ISP button and then tap the reset button and then release the ISP button to put it into programming mode. I use [lpc21isp](http://sourceforge.net/projects/lpc21isp/) to load the binary. The other option is FlashMagic, which uses HEX files, so you'll need to use some sort of bin2hex utility to convert the firmware file if you use this utility. I can't really say if/how this works, as I don't use this method. See this (http://mbed.org/users/frankvnk/notebook/lpc1768-mini-dk/) for more info.

Credits

Some credits, where credit is due.

EthernetInterface - modified to include PHY code for both the MiniDK2 and MBED based on selected #definitions

Mini-DK - Thanks for Frank and Erik for doing all the heavy lifting getting the MBED compiler and libraries and peripherals working on the Mini-DK2

NTP Client - Thanks to Donatien for this library to set the clock over the network

RTC - Thanks to Erik for the RTC library. I've got it in my project, but I don't think I'm using it for anything (yet).

SimpleSocket - Thanks to Yamaguchi-san. Modified slightly to take out references to EthernetInterface::init() and ::getIPAddress(). For some reason these don't like to be called in a thread.

JPEGCamera - Thanks again to Yamaguchi-san. Modified to output the JPEG binary over a socket rather than to a file descriptor.

USBHost - modified as noted above

IAP - Thanks to Okano-san. Pulled out of the Mini-DK folder so that I could link it back to the base repository at the root level.

Committer:
jakeb
Date:
Fri Aug 23 21:45:08 2013 +0000
Revision:
15:688b3e3958fd
Initial commit of software v0.2;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jakeb 15:688b3e3958fd 1 /**************************************************************************************************
jakeb 15:688b3e3958fd 2 ***** *****
jakeb 15:688b3e3958fd 3 ***** Name: SPI_TFT.cpp *****
jakeb 15:688b3e3958fd 4 ***** Ver.: 1.0 *****
jakeb 15:688b3e3958fd 5 ***** Date: 04/01/2013 *****
jakeb 15:688b3e3958fd 6 ***** Auth: Frank Vannieuwkerke *****
jakeb 15:688b3e3958fd 7 ***** Erik Olieman *****
jakeb 15:688b3e3958fd 8 ***** Func: library for 240*320 pixel TFT with ILI9320 LCD Controller *****
jakeb 15:688b3e3958fd 9 ***** *****
jakeb 15:688b3e3958fd 10 ***** Rewrite from Peter Drescher code - http://mbed.org/cookbook/SPI-driven-QVGA-TFT *****
jakeb 15:688b3e3958fd 11 ***** *****
jakeb 15:688b3e3958fd 12 **************************************************************************************************/
jakeb 15:688b3e3958fd 13
jakeb 15:688b3e3958fd 14 #include "SPI_TFT_ILI9320.h"
jakeb 15:688b3e3958fd 15 #include "mbed.h"
jakeb 15:688b3e3958fd 16
jakeb 15:688b3e3958fd 17
jakeb 15:688b3e3958fd 18 #define BPP 16 // Bits per pixel
jakeb 15:688b3e3958fd 19
jakeb 15:688b3e3958fd 20
jakeb 15:688b3e3958fd 21 SPI_TFT::SPI_TFT(PinName mosi, PinName miso, PinName sclk, PinName cs, const char *name)
jakeb 15:688b3e3958fd 22 : GraphicsDisplay(name), _spi(mosi, miso, sclk), _cs(cs)
jakeb 15:688b3e3958fd 23 {
jakeb 15:688b3e3958fd 24 char_x = 0;
jakeb 15:688b3e3958fd 25 tft_reset();
jakeb 15:688b3e3958fd 26 set_orientation(0);
jakeb 15:688b3e3958fd 27 backgroundimage = false;
jakeb 15:688b3e3958fd 28 #ifndef NO_FLASH_BUFFER
jakeb 15:688b3e3958fd 29 backgroundOrientation = 0;
jakeb 15:688b3e3958fd 30 #endif
jakeb 15:688b3e3958fd 31 }
jakeb 15:688b3e3958fd 32
jakeb 15:688b3e3958fd 33 int SPI_TFT::width()
jakeb 15:688b3e3958fd 34 {
jakeb 15:688b3e3958fd 35 if (orientation == 0 || orientation == 2) return 240;
jakeb 15:688b3e3958fd 36 else return 320;
jakeb 15:688b3e3958fd 37 }
jakeb 15:688b3e3958fd 38
jakeb 15:688b3e3958fd 39 int SPI_TFT::height()
jakeb 15:688b3e3958fd 40 {
jakeb 15:688b3e3958fd 41 if (orientation == 0 || orientation == 2) return 320;
jakeb 15:688b3e3958fd 42 else return 240;
jakeb 15:688b3e3958fd 43 }
jakeb 15:688b3e3958fd 44
jakeb 15:688b3e3958fd 45 void SPI_TFT::set_orientation(unsigned int o)
jakeb 15:688b3e3958fd 46 {
jakeb 15:688b3e3958fd 47 orientation = o;
jakeb 15:688b3e3958fd 48 WindowMax();
jakeb 15:688b3e3958fd 49 }
jakeb 15:688b3e3958fd 50
jakeb 15:688b3e3958fd 51 void SPI_TFT::mod_orientation(void)
jakeb 15:688b3e3958fd 52 {
jakeb 15:688b3e3958fd 53 switch (orientation) {
jakeb 15:688b3e3958fd 54 case 0:
jakeb 15:688b3e3958fd 55 wr_reg(0x03, 0x10b0); // ID1 = 1, ID0 = 1, AM = 0 - Portrait
jakeb 15:688b3e3958fd 56 break;
jakeb 15:688b3e3958fd 57 case 1:
jakeb 15:688b3e3958fd 58 wr_reg(0x03, 0x10a8); // ID1 = 1, ID0 = 0, AM = 0 - Landscape
jakeb 15:688b3e3958fd 59 break;
jakeb 15:688b3e3958fd 60 case 2:
jakeb 15:688b3e3958fd 61 wr_reg(0x03, 0x1080); // ID1 = 0, ID0 = 0, AM = 1 - Portrait upside down
jakeb 15:688b3e3958fd 62 break;
jakeb 15:688b3e3958fd 63 case 3:
jakeb 15:688b3e3958fd 64 wr_reg(0x03, 0x1098); // ID1 = 0, ID0 = 1, AM = 1 - Landscape upside down
jakeb 15:688b3e3958fd 65 break;
jakeb 15:688b3e3958fd 66 }
jakeb 15:688b3e3958fd 67 }
jakeb 15:688b3e3958fd 68
jakeb 15:688b3e3958fd 69 void SPI_TFT::wr_cmd(unsigned char cmd)
jakeb 15:688b3e3958fd 70 {
jakeb 15:688b3e3958fd 71 _cs = 0;
jakeb 15:688b3e3958fd 72 _spi.write(0x70);
jakeb 15:688b3e3958fd 73 _spi.write(0x00);
jakeb 15:688b3e3958fd 74 _spi.write(cmd);
jakeb 15:688b3e3958fd 75 _cs = 1;
jakeb 15:688b3e3958fd 76 }
jakeb 15:688b3e3958fd 77
jakeb 15:688b3e3958fd 78 void SPI_TFT::wr_dat(unsigned short dat)
jakeb 15:688b3e3958fd 79 {
jakeb 15:688b3e3958fd 80 unsigned char u,l;
jakeb 15:688b3e3958fd 81 u = (dat >> 0x08);
jakeb 15:688b3e3958fd 82 l = (dat & 0xff);
jakeb 15:688b3e3958fd 83 _cs = 0;
jakeb 15:688b3e3958fd 84 _spi.write(0x72);
jakeb 15:688b3e3958fd 85 _spi.write(u);
jakeb 15:688b3e3958fd 86 _spi.write(l);
jakeb 15:688b3e3958fd 87 _cs = 1;
jakeb 15:688b3e3958fd 88 }
jakeb 15:688b3e3958fd 89
jakeb 15:688b3e3958fd 90 void SPI_TFT::wr_dat_start(void)
jakeb 15:688b3e3958fd 91 {
jakeb 15:688b3e3958fd 92 _spi.write(0x72);
jakeb 15:688b3e3958fd 93 }
jakeb 15:688b3e3958fd 94
jakeb 15:688b3e3958fd 95 unsigned short SPI_TFT::rd_dat(void) // IMPORTANT : SPI frequency needs to be lowered when reading
jakeb 15:688b3e3958fd 96 {
jakeb 15:688b3e3958fd 97 unsigned short val = 0;
jakeb 15:688b3e3958fd 98 _cs = 0;
jakeb 15:688b3e3958fd 99 _spi.frequency(SPI_F_LO);
jakeb 15:688b3e3958fd 100 _spi.write(0x73);
jakeb 15:688b3e3958fd 101 _spi.write(0x00);
jakeb 15:688b3e3958fd 102 val = _spi.write(0x00); // Dummy read
jakeb 15:688b3e3958fd 103 val = _spi.write(0x00); // Read D8..D15
jakeb 15:688b3e3958fd 104 val <<= 8;
jakeb 15:688b3e3958fd 105 val |= _spi.write(0x00); // Read D0..D7
jakeb 15:688b3e3958fd 106 _cs = 1;
jakeb 15:688b3e3958fd 107 _spi.frequency(SPI_F_HI);
jakeb 15:688b3e3958fd 108 return (val);
jakeb 15:688b3e3958fd 109 }
jakeb 15:688b3e3958fd 110
jakeb 15:688b3e3958fd 111 void SPI_TFT::wr_reg(unsigned char reg, unsigned short val)
jakeb 15:688b3e3958fd 112 {
jakeb 15:688b3e3958fd 113 wr_cmd(reg);
jakeb 15:688b3e3958fd 114 wr_dat(val);
jakeb 15:688b3e3958fd 115 }
jakeb 15:688b3e3958fd 116
jakeb 15:688b3e3958fd 117 unsigned short SPI_TFT::rd_reg(unsigned char reg)
jakeb 15:688b3e3958fd 118 {
jakeb 15:688b3e3958fd 119 wr_cmd(reg);
jakeb 15:688b3e3958fd 120 return(rd_dat());
jakeb 15:688b3e3958fd 121 }
jakeb 15:688b3e3958fd 122
jakeb 15:688b3e3958fd 123 unsigned short SPI_TFT::Read_ID(void) // IMPORTANT : SPI frequency needs to be lowered when reading
jakeb 15:688b3e3958fd 124 {
jakeb 15:688b3e3958fd 125 unsigned short val = 0;
jakeb 15:688b3e3958fd 126 _cs = 0;
jakeb 15:688b3e3958fd 127 _spi.write(0x70);
jakeb 15:688b3e3958fd 128 _spi.write(0x00);
jakeb 15:688b3e3958fd 129 _spi.write(0x00);
jakeb 15:688b3e3958fd 130 _cs = 1;
jakeb 15:688b3e3958fd 131 _spi.frequency(SPI_F_LO);
jakeb 15:688b3e3958fd 132 _cs = 0;
jakeb 15:688b3e3958fd 133 _spi.write(0x73);
jakeb 15:688b3e3958fd 134 val = _spi.write(0x00); // Dummy read
jakeb 15:688b3e3958fd 135 val = _spi.write(0x00); // Read D8..D15
jakeb 15:688b3e3958fd 136 val <<= 8;
jakeb 15:688b3e3958fd 137 val |= _spi.write(0x00); // Read D0..D7
jakeb 15:688b3e3958fd 138 _cs = 1;
jakeb 15:688b3e3958fd 139 _spi.frequency(SPI_F_HI);
jakeb 15:688b3e3958fd 140 return (val);
jakeb 15:688b3e3958fd 141 }
jakeb 15:688b3e3958fd 142
jakeb 15:688b3e3958fd 143 void SPI_TFT::SetCursor( unsigned short Xpos, unsigned short Ypos )
jakeb 15:688b3e3958fd 144 {
jakeb 15:688b3e3958fd 145 wr_reg(0x20, Xpos );
jakeb 15:688b3e3958fd 146 wr_reg(0x21, Ypos );
jakeb 15:688b3e3958fd 147 }
jakeb 15:688b3e3958fd 148
jakeb 15:688b3e3958fd 149 void SPI_TFT::tft_reset()
jakeb 15:688b3e3958fd 150 {
jakeb 15:688b3e3958fd 151 _spi.format(8,3); // 8 bit spi mode 3
jakeb 15:688b3e3958fd 152 _spi.frequency(SPI_F_HI); // 48 Mhz SPI clock
jakeb 15:688b3e3958fd 153
jakeb 15:688b3e3958fd 154 wr_reg(0x00,0x0000);
jakeb 15:688b3e3958fd 155 wr_reg(0x01,0x0100); // Driver Output Control
jakeb 15:688b3e3958fd 156 wr_reg(0x02,0x0700); // LCD Driver Waveform Control
jakeb 15:688b3e3958fd 157 wr_reg(0x03,0x1030); // Set the scan mode
jakeb 15:688b3e3958fd 158 wr_reg(0x04,0x0000); // Scaling Control
jakeb 15:688b3e3958fd 159 wr_reg(0x08,0x0202); // Display Control 2
jakeb 15:688b3e3958fd 160 wr_reg(0x09,0x0000); // Display Control 3
jakeb 15:688b3e3958fd 161 wr_reg(0x0a,0x0000); // Frame Cycle Contal
jakeb 15:688b3e3958fd 162 wr_reg(0x0c,(1<<0)); // Extern Display Interface Control 1
jakeb 15:688b3e3958fd 163 wr_reg(0x0d,0x0000); // Frame Maker Position
jakeb 15:688b3e3958fd 164 wr_reg(0x0f,0x0000); // Extern Display Interface Control 2
jakeb 15:688b3e3958fd 165
jakeb 15:688b3e3958fd 166 wait_ms(50);
jakeb 15:688b3e3958fd 167
jakeb 15:688b3e3958fd 168 wr_reg(0x07,0x0101); // Display Control
jakeb 15:688b3e3958fd 169
jakeb 15:688b3e3958fd 170 wait_ms(50);
jakeb 15:688b3e3958fd 171
jakeb 15:688b3e3958fd 172 wr_reg(0x10,(1<<12)|(0<<8)|(1<<7)|(1<<6)|(0<<4)); // Power Control 1
jakeb 15:688b3e3958fd 173 wr_reg(0x11,0x0007); // Power Control 2
jakeb 15:688b3e3958fd 174 wr_reg(0x12,(1<<8)|(1<<4)|(0<<0)); // Power Control 3
jakeb 15:688b3e3958fd 175 wr_reg(0x13,0x0b00); // Power Control 4
jakeb 15:688b3e3958fd 176 wr_reg(0x29,0x0000); // Power Control 7
jakeb 15:688b3e3958fd 177 wr_reg(0x2b,(1<<14)|(1<<4));
jakeb 15:688b3e3958fd 178
jakeb 15:688b3e3958fd 179 wr_reg(0x50,0); // Set X Start
jakeb 15:688b3e3958fd 180 wr_reg(0x51,239); // Set X End
jakeb 15:688b3e3958fd 181 wr_reg(0x52,0); // Set Y Start
jakeb 15:688b3e3958fd 182 wr_reg(0x53,319); // Set Y End
jakeb 15:688b3e3958fd 183
jakeb 15:688b3e3958fd 184 wait_ms(50);
jakeb 15:688b3e3958fd 185
jakeb 15:688b3e3958fd 186 wr_reg(0x60,0x2700); // Driver Output Control
jakeb 15:688b3e3958fd 187 wr_reg(0x61,0x0001); // Driver Output Control
jakeb 15:688b3e3958fd 188 wr_reg(0x6a,0x0000); // Vertical Srcoll Control
jakeb 15:688b3e3958fd 189
jakeb 15:688b3e3958fd 190 wr_reg(0x80,0x0000); // Display Position Partial Display 1
jakeb 15:688b3e3958fd 191 wr_reg(0x81,0x0000); // RAM Address Start Partial Display 1
jakeb 15:688b3e3958fd 192 wr_reg(0x82,0x0000); // RAM Address End-Partial Display 1
jakeb 15:688b3e3958fd 193 wr_reg(0x83,0x0000); // Displsy Position Partial Display 2
jakeb 15:688b3e3958fd 194 wr_reg(0x84,0x0000); // RAM Address Start Partial Display 2
jakeb 15:688b3e3958fd 195 wr_reg(0x85,0x0000); // RAM Address End Partial Display 2
jakeb 15:688b3e3958fd 196
jakeb 15:688b3e3958fd 197 wr_reg(0x90,(0<<7)|(16<<0)); // Frame Cycle Control
jakeb 15:688b3e3958fd 198 wr_reg(0x92,0x0000); // Panel Interface Control 2
jakeb 15:688b3e3958fd 199 wr_reg(0x93,0x0001); // Panel Interface Control 3
jakeb 15:688b3e3958fd 200 wr_reg(0x95,0x0110); // Frame Cycle Control
jakeb 15:688b3e3958fd 201 wr_reg(0x97,(0<<8));
jakeb 15:688b3e3958fd 202 wr_reg(0x98,0x0000); // Frame Cycle Control
jakeb 15:688b3e3958fd 203 wr_reg(0x07,0x0133);
jakeb 15:688b3e3958fd 204
jakeb 15:688b3e3958fd 205 wait_ms(100);
jakeb 15:688b3e3958fd 206 WindowMax();
jakeb 15:688b3e3958fd 207 }
jakeb 15:688b3e3958fd 208
jakeb 15:688b3e3958fd 209
jakeb 15:688b3e3958fd 210 void SPI_TFT::pixel(int x, int y, int color)
jakeb 15:688b3e3958fd 211 {
jakeb 15:688b3e3958fd 212 switch (orientation) {
jakeb 15:688b3e3958fd 213 case 0:
jakeb 15:688b3e3958fd 214 wr_reg(0x20, x);
jakeb 15:688b3e3958fd 215 wr_reg(0x21, y);
jakeb 15:688b3e3958fd 216 break;
jakeb 15:688b3e3958fd 217 case 1:
jakeb 15:688b3e3958fd 218 wr_reg(0x20, 239-y);
jakeb 15:688b3e3958fd 219 wr_reg(0x21, x);
jakeb 15:688b3e3958fd 220 break;
jakeb 15:688b3e3958fd 221 case 2:
jakeb 15:688b3e3958fd 222 wr_reg(0x20, 239-x);
jakeb 15:688b3e3958fd 223 wr_reg(0x21, 319-y);
jakeb 15:688b3e3958fd 224 break;
jakeb 15:688b3e3958fd 225 case 3:
jakeb 15:688b3e3958fd 226 wr_reg(0x20, y);
jakeb 15:688b3e3958fd 227 wr_reg(0x21, 319-x);
jakeb 15:688b3e3958fd 228 break;
jakeb 15:688b3e3958fd 229 }
jakeb 15:688b3e3958fd 230 wr_cmd(0x22);
jakeb 15:688b3e3958fd 231 wr_dat(color);
jakeb 15:688b3e3958fd 232 }
jakeb 15:688b3e3958fd 233
jakeb 15:688b3e3958fd 234
jakeb 15:688b3e3958fd 235 void SPI_TFT::window(int x, int y, int w, int h)
jakeb 15:688b3e3958fd 236 {
jakeb 15:688b3e3958fd 237 unsigned int xw1, yh1;
jakeb 15:688b3e3958fd 238 xw1 = x + w - 1;
jakeb 15:688b3e3958fd 239 yh1 = y + h - 1;
jakeb 15:688b3e3958fd 240 wr_reg(0x20, x);
jakeb 15:688b3e3958fd 241 wr_reg(0x21, y);
jakeb 15:688b3e3958fd 242 switch (orientation) {
jakeb 15:688b3e3958fd 243 case 0:
jakeb 15:688b3e3958fd 244 wr_reg(0x50, x);
jakeb 15:688b3e3958fd 245 wr_reg(0x51, xw1);
jakeb 15:688b3e3958fd 246 wr_reg(0x52, y);
jakeb 15:688b3e3958fd 247 wr_reg(0x53, yh1);
jakeb 15:688b3e3958fd 248 break;
jakeb 15:688b3e3958fd 249 case 1:
jakeb 15:688b3e3958fd 250 wr_reg(0x50, 239 - yh1);
jakeb 15:688b3e3958fd 251 wr_reg(0x51, 239 - y);
jakeb 15:688b3e3958fd 252 wr_reg(0x52, x);
jakeb 15:688b3e3958fd 253 wr_reg(0x53, xw1);
jakeb 15:688b3e3958fd 254 break;
jakeb 15:688b3e3958fd 255 case 2:
jakeb 15:688b3e3958fd 256 wr_reg(0x50, 239 - xw1);
jakeb 15:688b3e3958fd 257 wr_reg(0x51, 239 - x);
jakeb 15:688b3e3958fd 258 wr_reg(0x52, 319 - yh1);
jakeb 15:688b3e3958fd 259 wr_reg(0x53, 319 - y);
jakeb 15:688b3e3958fd 260 break;
jakeb 15:688b3e3958fd 261 case 3:
jakeb 15:688b3e3958fd 262 wr_reg(0x50, y);
jakeb 15:688b3e3958fd 263 wr_reg(0x51, yh1);
jakeb 15:688b3e3958fd 264 wr_reg(0x52, 319 - xw1);
jakeb 15:688b3e3958fd 265 wr_reg(0x53, 319 - x);
jakeb 15:688b3e3958fd 266 break;
jakeb 15:688b3e3958fd 267 }
jakeb 15:688b3e3958fd 268 }
jakeb 15:688b3e3958fd 269
jakeb 15:688b3e3958fd 270
jakeb 15:688b3e3958fd 271 void SPI_TFT::WindowMax(void)
jakeb 15:688b3e3958fd 272 {
jakeb 15:688b3e3958fd 273 window(0, 0, width(), height());
jakeb 15:688b3e3958fd 274 }
jakeb 15:688b3e3958fd 275
jakeb 15:688b3e3958fd 276
jakeb 15:688b3e3958fd 277 void SPI_TFT::cls (void)
jakeb 15:688b3e3958fd 278 {
jakeb 15:688b3e3958fd 279 if (backgroundimage == false) {
jakeb 15:688b3e3958fd 280 unsigned long int index=0;
jakeb 15:688b3e3958fd 281 wr_reg(0x03, 0x1030);
jakeb 15:688b3e3958fd 282 WindowMax();
jakeb 15:688b3e3958fd 283 SetCursor(0,0);
jakeb 15:688b3e3958fd 284 wr_cmd(0x22);
jakeb 15:688b3e3958fd 285 _cs = 0;
jakeb 15:688b3e3958fd 286 wr_dat_start();
jakeb 15:688b3e3958fd 287 _spi.format(16,3);
jakeb 15:688b3e3958fd 288 int num = width()*height();
jakeb 15:688b3e3958fd 289
jakeb 15:688b3e3958fd 290 for( index = 0; index<num; index++ ) {
jakeb 15:688b3e3958fd 291 _spi.fastWrite(_background);
jakeb 15:688b3e3958fd 292 }
jakeb 15:688b3e3958fd 293 _spi.clearRX();
jakeb 15:688b3e3958fd 294
jakeb 15:688b3e3958fd 295 _spi.format(8,3);
jakeb 15:688b3e3958fd 296 _cs = 1;
jakeb 15:688b3e3958fd 297 }
jakeb 15:688b3e3958fd 298 #ifndef NO_FLASH_BUFFER
jakeb 15:688b3e3958fd 299 else {
jakeb 15:688b3e3958fd 300 int _orientation=orientation;
jakeb 15:688b3e3958fd 301 set_orientation(backgroundOrientation);
jakeb 15:688b3e3958fd 302 Bitmap(0,0,width(),height(),(unsigned char*) sector_start_adress[ 25 ]);
jakeb 15:688b3e3958fd 303 set_orientation(_orientation);
jakeb 15:688b3e3958fd 304 }
jakeb 15:688b3e3958fd 305 #endif
jakeb 15:688b3e3958fd 306 }
jakeb 15:688b3e3958fd 307
jakeb 15:688b3e3958fd 308 void SPI_TFT::hline(int x0, int x1, int y, int color)
jakeb 15:688b3e3958fd 309 {
jakeb 15:688b3e3958fd 310 unsigned int index=0;
jakeb 15:688b3e3958fd 311 int w;
jakeb 15:688b3e3958fd 312 w = x1 - x0 + 1;
jakeb 15:688b3e3958fd 313 mod_orientation();
jakeb 15:688b3e3958fd 314 window(x0,y,w,1);
jakeb 15:688b3e3958fd 315 wr_cmd(0x22);
jakeb 15:688b3e3958fd 316 _cs = 0;
jakeb 15:688b3e3958fd 317 wr_dat_start();
jakeb 15:688b3e3958fd 318
jakeb 15:688b3e3958fd 319 _spi.format(16,3);
jakeb 15:688b3e3958fd 320 int num = x1-x0;
jakeb 15:688b3e3958fd 321 for( index = 0; index<num; index++ ) {
jakeb 15:688b3e3958fd 322 _spi.fastWrite(color);
jakeb 15:688b3e3958fd 323 }
jakeb 15:688b3e3958fd 324 _spi.clearRX();
jakeb 15:688b3e3958fd 325
jakeb 15:688b3e3958fd 326 _spi.format(8,3);
jakeb 15:688b3e3958fd 327 _cs = 1;
jakeb 15:688b3e3958fd 328 return;
jakeb 15:688b3e3958fd 329 }
jakeb 15:688b3e3958fd 330
jakeb 15:688b3e3958fd 331 void SPI_TFT::vline(int x, int y0, int y1, int color)
jakeb 15:688b3e3958fd 332 {
jakeb 15:688b3e3958fd 333 unsigned int index=0;
jakeb 15:688b3e3958fd 334 int h;
jakeb 15:688b3e3958fd 335 h = y1 - y0 + 1;
jakeb 15:688b3e3958fd 336 mod_orientation();
jakeb 15:688b3e3958fd 337 window(x,y0,1,h);
jakeb 15:688b3e3958fd 338 wr_cmd(0x22);
jakeb 15:688b3e3958fd 339 _cs = 0;
jakeb 15:688b3e3958fd 340 wr_dat_start();
jakeb 15:688b3e3958fd 341 _spi.format(16,3);
jakeb 15:688b3e3958fd 342 int num = y1-y0;
jakeb 15:688b3e3958fd 343 for( index = 0; index<num; index++ ) {
jakeb 15:688b3e3958fd 344 _spi.fastWrite(color);
jakeb 15:688b3e3958fd 345 }
jakeb 15:688b3e3958fd 346 _spi.clearRX();
jakeb 15:688b3e3958fd 347 _spi.format(8,3);
jakeb 15:688b3e3958fd 348 _cs = 1;
jakeb 15:688b3e3958fd 349 return;
jakeb 15:688b3e3958fd 350 }
jakeb 15:688b3e3958fd 351
jakeb 15:688b3e3958fd 352 void SPI_TFT::line(int x0, int y0, int x1, int y1, int color)
jakeb 15:688b3e3958fd 353 {
jakeb 15:688b3e3958fd 354 wr_reg(0x03, 0x1030);
jakeb 15:688b3e3958fd 355 WindowMax();
jakeb 15:688b3e3958fd 356 int dx = 0, dy = 0;
jakeb 15:688b3e3958fd 357 int dx_sym = 0, dy_sym = 0;
jakeb 15:688b3e3958fd 358 int dx_x2 = 0, dy_x2 = 0;
jakeb 15:688b3e3958fd 359 int di = 0;
jakeb 15:688b3e3958fd 360
jakeb 15:688b3e3958fd 361 dx = x1-x0;
jakeb 15:688b3e3958fd 362 dy = y1-y0;
jakeb 15:688b3e3958fd 363
jakeb 15:688b3e3958fd 364 if (dx == 0) { // vertical line
jakeb 15:688b3e3958fd 365 if (y1 > y0) vline(x0,y0,y1,color);
jakeb 15:688b3e3958fd 366 else vline(x0,y1,y0,color);
jakeb 15:688b3e3958fd 367 return;
jakeb 15:688b3e3958fd 368 }
jakeb 15:688b3e3958fd 369
jakeb 15:688b3e3958fd 370 if (dx > 0) {
jakeb 15:688b3e3958fd 371 dx_sym = 1;
jakeb 15:688b3e3958fd 372 } else {
jakeb 15:688b3e3958fd 373 dx_sym = -1;
jakeb 15:688b3e3958fd 374 }
jakeb 15:688b3e3958fd 375 if (dy == 0) { // horizontal line
jakeb 15:688b3e3958fd 376 if (x1 > x0) hline(x0,x1,y0,color);
jakeb 15:688b3e3958fd 377 else hline(x1,x0,y0,color);
jakeb 15:688b3e3958fd 378 return;
jakeb 15:688b3e3958fd 379 }
jakeb 15:688b3e3958fd 380
jakeb 15:688b3e3958fd 381 if (dy > 0) {
jakeb 15:688b3e3958fd 382 dy_sym = 1;
jakeb 15:688b3e3958fd 383 } else {
jakeb 15:688b3e3958fd 384 dy_sym = -1;
jakeb 15:688b3e3958fd 385 }
jakeb 15:688b3e3958fd 386
jakeb 15:688b3e3958fd 387 dx = dx_sym*dx;
jakeb 15:688b3e3958fd 388 dy = dy_sym*dy;
jakeb 15:688b3e3958fd 389
jakeb 15:688b3e3958fd 390 dx_x2 = dx*2;
jakeb 15:688b3e3958fd 391 dy_x2 = dy*2;
jakeb 15:688b3e3958fd 392
jakeb 15:688b3e3958fd 393 if (dx >= dy) {
jakeb 15:688b3e3958fd 394 di = dy_x2 - dx;
jakeb 15:688b3e3958fd 395 while (x0 != x1) {
jakeb 15:688b3e3958fd 396
jakeb 15:688b3e3958fd 397 pixel(x0, y0, color);
jakeb 15:688b3e3958fd 398 x0 += dx_sym;
jakeb 15:688b3e3958fd 399 if (di<0) {
jakeb 15:688b3e3958fd 400 di += dy_x2;
jakeb 15:688b3e3958fd 401 } else {
jakeb 15:688b3e3958fd 402 di += dy_x2 - dx_x2;
jakeb 15:688b3e3958fd 403 y0 += dy_sym;
jakeb 15:688b3e3958fd 404 }
jakeb 15:688b3e3958fd 405 }
jakeb 15:688b3e3958fd 406 pixel(x0, y0, color);
jakeb 15:688b3e3958fd 407 } else {
jakeb 15:688b3e3958fd 408 di = dx_x2 - dy;
jakeb 15:688b3e3958fd 409 while (y0 != y1) {
jakeb 15:688b3e3958fd 410 pixel(x0, y0, color);
jakeb 15:688b3e3958fd 411 y0 += dy_sym;
jakeb 15:688b3e3958fd 412 if (di < 0) {
jakeb 15:688b3e3958fd 413 di += dx_x2;
jakeb 15:688b3e3958fd 414 } else {
jakeb 15:688b3e3958fd 415 di += dx_x2 - dy_x2;
jakeb 15:688b3e3958fd 416 x0 += dx_sym;
jakeb 15:688b3e3958fd 417 }
jakeb 15:688b3e3958fd 418 }
jakeb 15:688b3e3958fd 419 pixel(x0, y0, color);
jakeb 15:688b3e3958fd 420 }
jakeb 15:688b3e3958fd 421 return;
jakeb 15:688b3e3958fd 422 }
jakeb 15:688b3e3958fd 423
jakeb 15:688b3e3958fd 424
jakeb 15:688b3e3958fd 425 void SPI_TFT::rect(int x0, int y0, int w, int h, int color)
jakeb 15:688b3e3958fd 426 {
jakeb 15:688b3e3958fd 427 hline(x0,x0+w,y0,color);
jakeb 15:688b3e3958fd 428 vline(x0,y0,y0+h,color);
jakeb 15:688b3e3958fd 429 hline(x0,x0+w,y0+h,color);
jakeb 15:688b3e3958fd 430 vline(x0+w,y0,y0+h,color);
jakeb 15:688b3e3958fd 431
jakeb 15:688b3e3958fd 432 return;
jakeb 15:688b3e3958fd 433 }
jakeb 15:688b3e3958fd 434
jakeb 15:688b3e3958fd 435 void SPI_TFT::fillrect(int x0, int y0, int w, int h, int color)
jakeb 15:688b3e3958fd 436 {
jakeb 15:688b3e3958fd 437 unsigned long int index=0;
jakeb 15:688b3e3958fd 438 if (w < 0) {
jakeb 15:688b3e3958fd 439 x0 = x0 + w;
jakeb 15:688b3e3958fd 440 w = -w;
jakeb 15:688b3e3958fd 441 }
jakeb 15:688b3e3958fd 442 if (h < 0) {
jakeb 15:688b3e3958fd 443 y0 = y0 + h;
jakeb 15:688b3e3958fd 444 h = -h;
jakeb 15:688b3e3958fd 445 }
jakeb 15:688b3e3958fd 446 mod_orientation();
jakeb 15:688b3e3958fd 447 window(x0,y0,w,h);
jakeb 15:688b3e3958fd 448 wr_cmd(0x22);
jakeb 15:688b3e3958fd 449 _cs = 0;
jakeb 15:688b3e3958fd 450 wr_dat_start();
jakeb 15:688b3e3958fd 451 _spi.format(16,3);
jakeb 15:688b3e3958fd 452 int num = h*w;
jakeb 15:688b3e3958fd 453 for( index = 0; index<num; index++ ) {
jakeb 15:688b3e3958fd 454 _spi.fastWrite(color);
jakeb 15:688b3e3958fd 455 }
jakeb 15:688b3e3958fd 456 _spi.clearRX();
jakeb 15:688b3e3958fd 457 _spi.format(8,3);
jakeb 15:688b3e3958fd 458 _cs = 1;
jakeb 15:688b3e3958fd 459 return;
jakeb 15:688b3e3958fd 460 }
jakeb 15:688b3e3958fd 461
jakeb 15:688b3e3958fd 462 void SPI_TFT::draw_ellipse(int xc, int yc, int a, int b, unsigned int color)
jakeb 15:688b3e3958fd 463 {
jakeb 15:688b3e3958fd 464 /* e(x,y) = b^2*x^2 + a^2*y^2 - a^2*b^2 */
jakeb 15:688b3e3958fd 465 wr_reg(0x03, 0x1030);
jakeb 15:688b3e3958fd 466 WindowMax();
jakeb 15:688b3e3958fd 467 int x = 0, y = b;
jakeb 15:688b3e3958fd 468 long a2 = (long)a*a, b2 = (long)b*b;
jakeb 15:688b3e3958fd 469 long crit1 = -(a2/4 + a%2 + b2);
jakeb 15:688b3e3958fd 470 long crit2 = -(b2/4 + b%2 + a2);
jakeb 15:688b3e3958fd 471 long crit3 = -(b2/4 + b%2);
jakeb 15:688b3e3958fd 472 long t = -a2*y; // e(x+1/2,y-1/2) - (a^2+b^2)/4
jakeb 15:688b3e3958fd 473 long dxt = 2*b2*x, dyt = -2*a2*y;
jakeb 15:688b3e3958fd 474 long d2xt = 2*b2, d2yt = 2*a2;
jakeb 15:688b3e3958fd 475
jakeb 15:688b3e3958fd 476 while (y>=0 && x<=a) {
jakeb 15:688b3e3958fd 477 pixel(xc+x, yc+y, color);
jakeb 15:688b3e3958fd 478 if (x!=0 || y!=0)
jakeb 15:688b3e3958fd 479 pixel(xc-x, yc-y, color);
jakeb 15:688b3e3958fd 480 if (x!=0 && y!=0) {
jakeb 15:688b3e3958fd 481 pixel(xc+x, yc-y, color);
jakeb 15:688b3e3958fd 482 pixel(xc-x, yc+y, color);
jakeb 15:688b3e3958fd 483 }
jakeb 15:688b3e3958fd 484 if (t + b2*x <= crit1 || // e(x+1,y-1/2) <= 0
jakeb 15:688b3e3958fd 485 t + a2*y <= crit3) // e(x+1/2,y) <= 0
jakeb 15:688b3e3958fd 486 incx();
jakeb 15:688b3e3958fd 487 else if (t - a2*y > crit2) // e(x+1/2,y-1) > 0
jakeb 15:688b3e3958fd 488 incy();
jakeb 15:688b3e3958fd 489 else {
jakeb 15:688b3e3958fd 490 incx();
jakeb 15:688b3e3958fd 491 incy();
jakeb 15:688b3e3958fd 492 }
jakeb 15:688b3e3958fd 493 }
jakeb 15:688b3e3958fd 494 }
jakeb 15:688b3e3958fd 495
jakeb 15:688b3e3958fd 496 void SPI_TFT::fill_ellipse(int xc, int yc, int a, int b, unsigned int color)
jakeb 15:688b3e3958fd 497 {
jakeb 15:688b3e3958fd 498 /* e(x,y) = b^2*x^2 + a^2*y^2 - a^2*b^2 */
jakeb 15:688b3e3958fd 499 int x = 0, y = b;
jakeb 15:688b3e3958fd 500 int rx = x, ry = y;
jakeb 15:688b3e3958fd 501 unsigned int width = 1;
jakeb 15:688b3e3958fd 502 unsigned int height = 1;
jakeb 15:688b3e3958fd 503 long a2 = (long)a*a, b2 = (long)b*b;
jakeb 15:688b3e3958fd 504 long crit1 = -(a2/4 + a%2 + b2);
jakeb 15:688b3e3958fd 505 long crit2 = -(b2/4 + b%2 + a2);
jakeb 15:688b3e3958fd 506 long crit3 = -(b2/4 + b%2);
jakeb 15:688b3e3958fd 507 long t = -a2*y; // e(x+1/2,y-1/2) - (a^2+b^2)/4
jakeb 15:688b3e3958fd 508 long dxt = 2*b2*x, dyt = -2*a2*y;
jakeb 15:688b3e3958fd 509 long d2xt = 2*b2, d2yt = 2*a2;
jakeb 15:688b3e3958fd 510
jakeb 15:688b3e3958fd 511 if (b == 0) {
jakeb 15:688b3e3958fd 512 fillrect(xc-a, yc, 2*a+1, 1, color);
jakeb 15:688b3e3958fd 513 return;
jakeb 15:688b3e3958fd 514 }
jakeb 15:688b3e3958fd 515
jakeb 15:688b3e3958fd 516 while (y>=0 && x<=a) {
jakeb 15:688b3e3958fd 517 if (t + b2*x <= crit1 || // e(x+1,y-1/2) <= 0
jakeb 15:688b3e3958fd 518 t + a2*y <= crit3) { // e(x+1/2,y) <= 0
jakeb 15:688b3e3958fd 519 if (height == 1)
jakeb 15:688b3e3958fd 520 ; // draw nothing
jakeb 15:688b3e3958fd 521 else if (ry*2+1 > (height-1)*2) {
jakeb 15:688b3e3958fd 522 fillrect(xc-rx, yc-ry, width, height-1, color);
jakeb 15:688b3e3958fd 523 fillrect(xc-rx, yc+ry+1, width, 1-height, color);
jakeb 15:688b3e3958fd 524 ry -= height-1;
jakeb 15:688b3e3958fd 525 height = 1;
jakeb 15:688b3e3958fd 526 } else {
jakeb 15:688b3e3958fd 527 fillrect(xc-rx, yc-ry, width, ry*2+1, color);
jakeb 15:688b3e3958fd 528 ry -= ry;
jakeb 15:688b3e3958fd 529 height = 1;
jakeb 15:688b3e3958fd 530 }
jakeb 15:688b3e3958fd 531 incx();
jakeb 15:688b3e3958fd 532 rx++;
jakeb 15:688b3e3958fd 533 width += 2;
jakeb 15:688b3e3958fd 534 } else if (t - a2*y > crit2) { // e(x+1/2,y-1) > 0
jakeb 15:688b3e3958fd 535 incy();
jakeb 15:688b3e3958fd 536 height++;
jakeb 15:688b3e3958fd 537 } else {
jakeb 15:688b3e3958fd 538 if (ry*2+1 > height*2) {
jakeb 15:688b3e3958fd 539 fillrect(xc-rx, yc-ry, width, height, color);
jakeb 15:688b3e3958fd 540 fillrect(xc-rx, yc+ry+1, width, -height, color);
jakeb 15:688b3e3958fd 541 } else {
jakeb 15:688b3e3958fd 542 fillrect(xc-rx, yc-ry, width, ry*2+1, color);
jakeb 15:688b3e3958fd 543 }
jakeb 15:688b3e3958fd 544 incx();
jakeb 15:688b3e3958fd 545 incy();
jakeb 15:688b3e3958fd 546 rx++;
jakeb 15:688b3e3958fd 547 width += 2;
jakeb 15:688b3e3958fd 548 ry -= height;
jakeb 15:688b3e3958fd 549 height = 1;
jakeb 15:688b3e3958fd 550 }
jakeb 15:688b3e3958fd 551 }
jakeb 15:688b3e3958fd 552
jakeb 15:688b3e3958fd 553 if (ry > height) {
jakeb 15:688b3e3958fd 554 fillrect(xc-rx, yc-ry, width, height, color);
jakeb 15:688b3e3958fd 555 fillrect(xc-rx, yc+ry+1, width, -height, color);
jakeb 15:688b3e3958fd 556 } else {
jakeb 15:688b3e3958fd 557 fillrect(xc-rx, yc-ry, width, ry*2+1, color);
jakeb 15:688b3e3958fd 558 }
jakeb 15:688b3e3958fd 559 }
jakeb 15:688b3e3958fd 560
jakeb 15:688b3e3958fd 561
jakeb 15:688b3e3958fd 562 void SPI_TFT::locate(int x, int y)
jakeb 15:688b3e3958fd 563 {
jakeb 15:688b3e3958fd 564 char_x = x;
jakeb 15:688b3e3958fd 565 char_y = y;
jakeb 15:688b3e3958fd 566 }
jakeb 15:688b3e3958fd 567
jakeb 15:688b3e3958fd 568 int SPI_TFT::columns()
jakeb 15:688b3e3958fd 569 {
jakeb 15:688b3e3958fd 570 return width() / font[1];
jakeb 15:688b3e3958fd 571 }
jakeb 15:688b3e3958fd 572
jakeb 15:688b3e3958fd 573 int SPI_TFT::rows()
jakeb 15:688b3e3958fd 574 {
jakeb 15:688b3e3958fd 575 return height() / font[2];
jakeb 15:688b3e3958fd 576 }
jakeb 15:688b3e3958fd 577
jakeb 15:688b3e3958fd 578 int SPI_TFT::_putc(int value)
jakeb 15:688b3e3958fd 579 {
jakeb 15:688b3e3958fd 580 if (value == '\n') { // new line
jakeb 15:688b3e3958fd 581 char_x = 0;
jakeb 15:688b3e3958fd 582 char_y = char_y + font[2];
jakeb 15:688b3e3958fd 583 if (char_y >= height() - font[2]) {
jakeb 15:688b3e3958fd 584 char_y = 0;
jakeb 15:688b3e3958fd 585 }
jakeb 15:688b3e3958fd 586 } else {
jakeb 15:688b3e3958fd 587 character(char_x, char_y, value);
jakeb 15:688b3e3958fd 588 }
jakeb 15:688b3e3958fd 589 return value;
jakeb 15:688b3e3958fd 590 }
jakeb 15:688b3e3958fd 591
jakeb 15:688b3e3958fd 592 void SPI_TFT::character(int x, int y, int c)
jakeb 15:688b3e3958fd 593 {
jakeb 15:688b3e3958fd 594 unsigned int hor,vert,offset,bpl,j,i,b;
jakeb 15:688b3e3958fd 595 unsigned char* bitmap_char;
jakeb 15:688b3e3958fd 596 unsigned char z,w;
jakeb 15:688b3e3958fd 597
jakeb 15:688b3e3958fd 598 if ((c < 31) || (c > 127)) return; // test char range
jakeb 15:688b3e3958fd 599
jakeb 15:688b3e3958fd 600 // read font parameter from start of array
jakeb 15:688b3e3958fd 601 offset = font[0]; // bytes / char
jakeb 15:688b3e3958fd 602 hor = font[1]; // get hor size of font
jakeb 15:688b3e3958fd 603 vert = font[2]; // get vert size of font
jakeb 15:688b3e3958fd 604 bpl = font[3]; // bytes per line
jakeb 15:688b3e3958fd 605
jakeb 15:688b3e3958fd 606 if (char_x + hor > width()) {
jakeb 15:688b3e3958fd 607 char_x = 0;
jakeb 15:688b3e3958fd 608 char_y = char_y + vert;
jakeb 15:688b3e3958fd 609 if (char_y >= height() - font[2]) {
jakeb 15:688b3e3958fd 610 char_y = 0;
jakeb 15:688b3e3958fd 611 }
jakeb 15:688b3e3958fd 612 }
jakeb 15:688b3e3958fd 613 mod_orientation();
jakeb 15:688b3e3958fd 614
jakeb 15:688b3e3958fd 615 bitmap_char = &font[((c -32) * offset) + 4]; // start of char bitmap
jakeb 15:688b3e3958fd 616 w = bitmap_char[0]; // width of actual char
jakeb 15:688b3e3958fd 617 window(char_x, char_y,hor,vert); // char box
jakeb 15:688b3e3958fd 618 wr_cmd(0x22);
jakeb 15:688b3e3958fd 619 _cs = 0;
jakeb 15:688b3e3958fd 620 wr_dat_start();
jakeb 15:688b3e3958fd 621 _spi.format(16,3);
jakeb 15:688b3e3958fd 622 for (j=0; j<vert; j++) { // vert line
jakeb 15:688b3e3958fd 623 for (i=0; i<hor; i++) { // horz line
jakeb 15:688b3e3958fd 624 z = bitmap_char[bpl * i + ((j & 0xF8) >> 3)+1];
jakeb 15:688b3e3958fd 625 b = 1 << (j & 0x07);
jakeb 15:688b3e3958fd 626 if (( z & b ) == 0x00) {
jakeb 15:688b3e3958fd 627 #ifndef NO_FLASH_BUFFER
jakeb 15:688b3e3958fd 628 if (backgroundimage==false)
jakeb 15:688b3e3958fd 629 #endif
jakeb 15:688b3e3958fd 630 _spi.fastWrite(_background);
jakeb 15:688b3e3958fd 631 #ifndef NO_FLASH_BUFFER
jakeb 15:688b3e3958fd 632 else
jakeb 15:688b3e3958fd 633 {
jakeb 15:688b3e3958fd 634 unsigned short *bitmap_ptr = (unsigned short *)sector_start_adress[ 25 ];
jakeb 15:688b3e3958fd 635 int angle = (orientation - backgroundOrientation)%4; //Get the difference in orientation between background and current
jakeb 15:688b3e3958fd 636 switch (angle) {
jakeb 15:688b3e3958fd 637 case 0: //Same orientation
jakeb 15:688b3e3958fd 638 bitmap_ptr += width() * (height()-(y+j+1))+x+i;
jakeb 15:688b3e3958fd 639 break;
jakeb 15:688b3e3958fd 640 case 1: //Rotated 1 (don't ask me which direction)
jakeb 15:688b3e3958fd 641 bitmap_ptr += height() * (width()-(x+i+1))+height()-(y+j + 1);
jakeb 15:688b3e3958fd 642 break;
jakeb 15:688b3e3958fd 643 case 2: //Upside down
jakeb 15:688b3e3958fd 644 bitmap_ptr += width() * (y+j)+width() - (x+i + 1);
jakeb 15:688b3e3958fd 645 break;
jakeb 15:688b3e3958fd 646 case 3: //Rotated 3
jakeb 15:688b3e3958fd 647 bitmap_ptr += height() * (x+i)+y+j;
jakeb 15:688b3e3958fd 648 break;
jakeb 15:688b3e3958fd 649 default:
jakeb 15:688b3e3958fd 650 break;
jakeb 15:688b3e3958fd 651 }
jakeb 15:688b3e3958fd 652
jakeb 15:688b3e3958fd 653 _spi.fastWrite(*bitmap_ptr);
jakeb 15:688b3e3958fd 654 }
jakeb 15:688b3e3958fd 655 #endif
jakeb 15:688b3e3958fd 656 } else {
jakeb 15:688b3e3958fd 657 _spi.fastWrite(_foreground);
jakeb 15:688b3e3958fd 658 }
jakeb 15:688b3e3958fd 659 }
jakeb 15:688b3e3958fd 660 }
jakeb 15:688b3e3958fd 661 _spi.clearRX();
jakeb 15:688b3e3958fd 662 _spi.format(8,3);
jakeb 15:688b3e3958fd 663 _cs = 1;
jakeb 15:688b3e3958fd 664 if ((w + 2) < hor) { // x offset to next char
jakeb 15:688b3e3958fd 665 char_x += w + 2;
jakeb 15:688b3e3958fd 666 } else char_x += hor;
jakeb 15:688b3e3958fd 667 }
jakeb 15:688b3e3958fd 668
jakeb 15:688b3e3958fd 669 void SPI_TFT::set_font(unsigned char* f)
jakeb 15:688b3e3958fd 670 {
jakeb 15:688b3e3958fd 671 font = f;
jakeb 15:688b3e3958fd 672 }
jakeb 15:688b3e3958fd 673
jakeb 15:688b3e3958fd 674
jakeb 15:688b3e3958fd 675 void SPI_TFT::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap)
jakeb 15:688b3e3958fd 676 {
jakeb 15:688b3e3958fd 677 unsigned int i,j;
jakeb 15:688b3e3958fd 678 unsigned short *bitmap_ptr = (unsigned short *)bitmap;
jakeb 15:688b3e3958fd 679 mod_orientation();
jakeb 15:688b3e3958fd 680 window(x, y, w, h);
jakeb 15:688b3e3958fd 681 wr_cmd(0x22);
jakeb 15:688b3e3958fd 682 _cs = 0;
jakeb 15:688b3e3958fd 683 wr_dat_start();
jakeb 15:688b3e3958fd 684 _spi.format(16,3);
jakeb 15:688b3e3958fd 685 bitmap_ptr += ((h - 1)*w);
jakeb 15:688b3e3958fd 686 for (j = 0; j < h; j++) { //Lines
jakeb 15:688b3e3958fd 687 for (i = 0; i < w; i++) { // copy pixel data to TFT
jakeb 15:688b3e3958fd 688 _spi.fastWrite(*bitmap_ptr); // one line
jakeb 15:688b3e3958fd 689 bitmap_ptr++;
jakeb 15:688b3e3958fd 690 }
jakeb 15:688b3e3958fd 691 bitmap_ptr -= 2*w;
jakeb 15:688b3e3958fd 692 }
jakeb 15:688b3e3958fd 693 _spi.clearRX();
jakeb 15:688b3e3958fd 694 _spi.format(8,3);
jakeb 15:688b3e3958fd 695 _cs = 1;
jakeb 15:688b3e3958fd 696 }
jakeb 15:688b3e3958fd 697
jakeb 15:688b3e3958fd 698 int SPI_TFT::Bitmap(unsigned int x, unsigned int y, const char *Name_BMP)
jakeb 15:688b3e3958fd 699 {
jakeb 15:688b3e3958fd 700 #define RGB565CONVERT(red, green, blue) (uint16_t)( (( red >> 3 ) << 11 ) | (( green >> 2 ) << 5 ) | ( blue >> 3 ))
jakeb 15:688b3e3958fd 701 mod_orientation();
jakeb 15:688b3e3958fd 702
jakeb 15:688b3e3958fd 703 bitmapData bmp = getBitmapData(Name_BMP);
jakeb 15:688b3e3958fd 704 if (bmp.return_code != 1)
jakeb 15:688b3e3958fd 705 return bmp.return_code;
jakeb 15:688b3e3958fd 706
jakeb 15:688b3e3958fd 707
jakeb 15:688b3e3958fd 708 unsigned char *line = (unsigned char *) malloc (bmp.bits/8 * bmp.width); // we need a buffer for a line
jakeb 15:688b3e3958fd 709 unsigned short *line_short = (unsigned short*) (line); // Same one, addressed as short
jakeb 15:688b3e3958fd 710
jakeb 15:688b3e3958fd 711
jakeb 15:688b3e3958fd 712 if ((bmp.height > height()+y) || (bmp.width > width()+x))
jakeb 15:688b3e3958fd 713 return -3; //Size mismatch
jakeb 15:688b3e3958fd 714
jakeb 15:688b3e3958fd 715 if (line == NULL)
jakeb 15:688b3e3958fd 716 return(-4); // error no memory
jakeb 15:688b3e3958fd 717
jakeb 15:688b3e3958fd 718
jakeb 15:688b3e3958fd 719 for (int j = bmp.height-1; j >= 0; j--) { //Lines bottom up
jakeb 15:688b3e3958fd 720 int off = j * (bmp.width * bmp.bits/8 + bmp.pad) + bmp.start_data; // start of line
jakeb 15:688b3e3958fd 721 fseek(bmp.file, off ,SEEK_SET);
jakeb 15:688b3e3958fd 722 fread(line,1,bmp.width * bmp.bits/8,bmp.file); // read a line - slow !
jakeb 15:688b3e3958fd 723
jakeb 15:688b3e3958fd 724 //If 24 bit format, convert to 565
jakeb 15:688b3e3958fd 725 if (bmp.bits == 24) {
jakeb 15:688b3e3958fd 726 for (int i = 0; i<bmp.width; i++) {
jakeb 15:688b3e3958fd 727 line_short[i] = RGB565CONVERT(line[3*i+2], line[3*i+1], line[3*i]);
jakeb 15:688b3e3958fd 728 }
jakeb 15:688b3e3958fd 729 }
jakeb 15:688b3e3958fd 730
jakeb 15:688b3e3958fd 731 window(x, y+bmp.height - 1 - j,bmp.width ,1);
jakeb 15:688b3e3958fd 732 wr_cmd(0x22);
jakeb 15:688b3e3958fd 733
jakeb 15:688b3e3958fd 734 _cs = 0;
jakeb 15:688b3e3958fd 735 wr_dat_start();
jakeb 15:688b3e3958fd 736 _spi.format(16, 3);
jakeb 15:688b3e3958fd 737 _spi.setFormat();
jakeb 15:688b3e3958fd 738
jakeb 15:688b3e3958fd 739 for (int i = 0; i < bmp.width; i++) { // copy pixel data to TFT
jakeb 15:688b3e3958fd 740 _spi.fastWrite(line_short[i]); // one line
jakeb 15:688b3e3958fd 741 }
jakeb 15:688b3e3958fd 742
jakeb 15:688b3e3958fd 743 _spi.clearRX();
jakeb 15:688b3e3958fd 744 _spi.format(8,3);
jakeb 15:688b3e3958fd 745 _cs = 1;
jakeb 15:688b3e3958fd 746
jakeb 15:688b3e3958fd 747 }
jakeb 15:688b3e3958fd 748
jakeb 15:688b3e3958fd 749
jakeb 15:688b3e3958fd 750 free (line);
jakeb 15:688b3e3958fd 751 fclose(bmp.file);
jakeb 15:688b3e3958fd 752 WindowMax();
jakeb 15:688b3e3958fd 753
jakeb 15:688b3e3958fd 754 return(1);
jakeb 15:688b3e3958fd 755 }
jakeb 15:688b3e3958fd 756
jakeb 15:688b3e3958fd 757 #ifndef NO_FLASH_BUFFER
jakeb 15:688b3e3958fd 758
jakeb 15:688b3e3958fd 759 int SPI_TFT::fileToFlash(const char *Name_BMP)
jakeb 15:688b3e3958fd 760 {
jakeb 15:688b3e3958fd 761 #define RGB565CONVERT(red, green, blue) (uint16_t)( (( red >> 3 ) << 11 ) | (( green >> 2 ) << 5 ) | ( blue >> 3 ))
jakeb 15:688b3e3958fd 762
jakeb 15:688b3e3958fd 763 mod_orientation();
jakeb 15:688b3e3958fd 764
jakeb 15:688b3e3958fd 765 bitmapData bmp = getBitmapData(Name_BMP);
jakeb 15:688b3e3958fd 766 if (bmp.return_code != 1)
jakeb 15:688b3e3958fd 767 return bmp.return_code;
jakeb 15:688b3e3958fd 768
jakeb 15:688b3e3958fd 769
jakeb 15:688b3e3958fd 770 unsigned char *line = (unsigned char *) malloc (bmp.bits/8 * bmp.width); // we need a buffer for a line
jakeb 15:688b3e3958fd 771 unsigned short *line_short = (unsigned short*) (line); // Same one, addressed as short
jakeb 15:688b3e3958fd 772
jakeb 15:688b3e3958fd 773 unsigned short *flashSector = (unsigned short *) malloc (256); //And one to send to flash
jakeb 15:688b3e3958fd 774
jakeb 15:688b3e3958fd 775 if ((bmp.height != height()) || (bmp.width != width()))
jakeb 15:688b3e3958fd 776 return -3; //Size mismatch
jakeb 15:688b3e3958fd 777
jakeb 15:688b3e3958fd 778 if ((line == NULL) || (flashSector == NULL))
jakeb 15:688b3e3958fd 779 return(-4); // error no memory
jakeb 15:688b3e3958fd 780
jakeb 15:688b3e3958fd 781
jakeb 15:688b3e3958fd 782 int flashPointer = 0, flashWrites = 0;
jakeb 15:688b3e3958fd 783
jakeb 15:688b3e3958fd 784 //Erase Flash that will be used:
jakeb 15:688b3e3958fd 785 if ( iap.blank_check( 25, 29 ) == SECTOR_NOT_BLANK ) {
jakeb 15:688b3e3958fd 786 iap.prepare( 25, 29 );
jakeb 15:688b3e3958fd 787 iap.erase( 25, 29 );
jakeb 15:688b3e3958fd 788 }
jakeb 15:688b3e3958fd 789
jakeb 15:688b3e3958fd 790 for (int j = 0; j < bmp.height; j++) { //Lines bottom up
jakeb 15:688b3e3958fd 791 int off = j * (bmp.width * bmp.bits/8 + bmp.pad) + bmp.start_data; // start of line
jakeb 15:688b3e3958fd 792 fseek(bmp.file, off ,SEEK_SET);
jakeb 15:688b3e3958fd 793 fread(line,1,bmp.width * bmp.bits/8,bmp.file); // read a line - slow !
jakeb 15:688b3e3958fd 794
jakeb 15:688b3e3958fd 795 //If 24 bit format, do some processing
jakeb 15:688b3e3958fd 796 if (bmp.bits == 24) {
jakeb 15:688b3e3958fd 797 for (int i = 0; i<bmp.width; i++) {
jakeb 15:688b3e3958fd 798 line_short[i] = RGB565CONVERT(line[3*i+2], line[3*i+1], line[3*i]);
jakeb 15:688b3e3958fd 799 }
jakeb 15:688b3e3958fd 800 }
jakeb 15:688b3e3958fd 801
jakeb 15:688b3e3958fd 802
jakeb 15:688b3e3958fd 803 for (int i = 0; i < bmp.width; i++) { // copy pixel data to TFT
jakeb 15:688b3e3958fd 804 flashSector[flashPointer] = line_short[i]; // one line
jakeb 15:688b3e3958fd 805 flashPointer++;
jakeb 15:688b3e3958fd 806
jakeb 15:688b3e3958fd 807 //If flashpointer reached the end, write to flash
jakeb 15:688b3e3958fd 808 if (flashPointer == 128) {
jakeb 15:688b3e3958fd 809 iap.prepare( 25, 29 );
jakeb 15:688b3e3958fd 810 iap.write((char *)flashSector, sector_start_adress[ 25 ] + 256 * flashWrites, 256);
jakeb 15:688b3e3958fd 811 flashPointer = 0;
jakeb 15:688b3e3958fd 812 flashWrites++;
jakeb 15:688b3e3958fd 813 if (flashWrites == 1000)
jakeb 15:688b3e3958fd 814 error("Too many flashwrites");
jakeb 15:688b3e3958fd 815 }
jakeb 15:688b3e3958fd 816 }
jakeb 15:688b3e3958fd 817 }
jakeb 15:688b3e3958fd 818 //write remaining data
jakeb 15:688b3e3958fd 819 if (flashPointer!=0) {
jakeb 15:688b3e3958fd 820 iap.prepare( 25, 29 );
jakeb 15:688b3e3958fd 821 iap.write((char*)flashSector, sector_start_adress[ 25 ] + 256 * flashWrites, 256);
jakeb 15:688b3e3958fd 822 flashPointer = 0;
jakeb 15:688b3e3958fd 823 flashWrites++;
jakeb 15:688b3e3958fd 824 if (flashWrites == 1000)
jakeb 15:688b3e3958fd 825 error("Too many flashwrites");
jakeb 15:688b3e3958fd 826 }
jakeb 15:688b3e3958fd 827
jakeb 15:688b3e3958fd 828
jakeb 15:688b3e3958fd 829
jakeb 15:688b3e3958fd 830 free (line);
jakeb 15:688b3e3958fd 831 fclose(bmp.file);
jakeb 15:688b3e3958fd 832 backgroundImage(true);
jakeb 15:688b3e3958fd 833 backgroundOrientation = orientation;
jakeb 15:688b3e3958fd 834 return(1);
jakeb 15:688b3e3958fd 835 }
jakeb 15:688b3e3958fd 836
jakeb 15:688b3e3958fd 837 void SPI_TFT::backgroundImage( bool active) {
jakeb 15:688b3e3958fd 838 backgroundimage = active;
jakeb 15:688b3e3958fd 839 }
jakeb 15:688b3e3958fd 840 #endif
jakeb 15:688b3e3958fd 841
jakeb 15:688b3e3958fd 842
jakeb 15:688b3e3958fd 843 SPI_TFT::bitmapData SPI_TFT::getBitmapData(const char *Name_BMP){
jakeb 15:688b3e3958fd 844 #define OffsetPixelWidth 18
jakeb 15:688b3e3958fd 845 #define OffsetPixelHeigh 22
jakeb 15:688b3e3958fd 846 #define OffsetFileSize 34
jakeb 15:688b3e3958fd 847 #define OffsetPixData 10
jakeb 15:688b3e3958fd 848 #define OffsetBPP 28
jakeb 15:688b3e3958fd 849
jakeb 15:688b3e3958fd 850
jakeb 15:688b3e3958fd 851 bitmapData retval;
jakeb 15:688b3e3958fd 852 retval.return_code = 1;
jakeb 15:688b3e3958fd 853 unsigned char BMP_Header[54];
jakeb 15:688b3e3958fd 854
jakeb 15:688b3e3958fd 855 retval.file = fopen(Name_BMP, "rb"); // open the bmp file
jakeb 15:688b3e3958fd 856 if (!retval.file) {
jakeb 15:688b3e3958fd 857 retval.return_code = 0;
jakeb 15:688b3e3958fd 858 return(retval); // error file not found !
jakeb 15:688b3e3958fd 859 }
jakeb 15:688b3e3958fd 860
jakeb 15:688b3e3958fd 861 fread(&BMP_Header[0],1,54,retval.file); // get the BMP Header
jakeb 15:688b3e3958fd 862
jakeb 15:688b3e3958fd 863 if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) { // check magic byte
jakeb 15:688b3e3958fd 864 fclose(retval.file);
jakeb 15:688b3e3958fd 865 retval.return_code = -1;
jakeb 15:688b3e3958fd 866 return(retval); // error not a BMP file
jakeb 15:688b3e3958fd 867 }
jakeb 15:688b3e3958fd 868
jakeb 15:688b3e3958fd 869 int BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8);
jakeb 15:688b3e3958fd 870 if (BPP_t == 0x0010)
jakeb 15:688b3e3958fd 871 retval.bits = 16;
jakeb 15:688b3e3958fd 872 else if (BPP_t == 0x0018)
jakeb 15:688b3e3958fd 873 retval.bits = 24;
jakeb 15:688b3e3958fd 874 else {
jakeb 15:688b3e3958fd 875 fclose(retval.file);
jakeb 15:688b3e3958fd 876 retval.return_code = -2;
jakeb 15:688b3e3958fd 877 return(retval); // error no 16/24 bit BMP
jakeb 15:688b3e3958fd 878 }
jakeb 15:688b3e3958fd 879
jakeb 15:688b3e3958fd 880 retval.height = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24);
jakeb 15:688b3e3958fd 881 retval.width = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24);
jakeb 15:688b3e3958fd 882 if (retval.height > height()|| retval.width > width()) {
jakeb 15:688b3e3958fd 883 fclose(retval.file);
jakeb 15:688b3e3958fd 884 retval.return_code = -3;
jakeb 15:688b3e3958fd 885 return(retval); // too big
jakeb 15:688b3e3958fd 886 }
jakeb 15:688b3e3958fd 887
jakeb 15:688b3e3958fd 888
jakeb 15:688b3e3958fd 889 retval.start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24);
jakeb 15:688b3e3958fd 890
jakeb 15:688b3e3958fd 891 // the bmp lines are padded to multiple of 4 bytes
jakeb 15:688b3e3958fd 892 retval.pad = -1;
jakeb 15:688b3e3958fd 893 do {
jakeb 15:688b3e3958fd 894 retval.pad ++;
jakeb 15:688b3e3958fd 895 } while ((retval.width * retval.bits/8 + retval.pad)%4 != 0);
jakeb 15:688b3e3958fd 896 return retval;
jakeb 15:688b3e3958fd 897
jakeb 15:688b3e3958fd 898 }