PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Dependents:   Sensitive

Fork of PokittoLib by Jonne Valola

Committer:
spinal
Date:
Wed Oct 18 14:47:54 2017 +0000
Revision:
15:0bbe8f6fae32
Parent:
0:e8b8f36b4505
direct lcd stuff used by sensitive

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 0:e8b8f36b4505 1 /**************************************************************************/
Pokitto 0:e8b8f36b4505 2 /*!
Pokitto 0:e8b8f36b4505 3 @file PokittoPalette.cpp
Pokitto 0:e8b8f36b4505 4 @author Jonne Valola
Pokitto 0:e8b8f36b4505 5
Pokitto 0:e8b8f36b4505 6 @section LICENSE
Pokitto 0:e8b8f36b4505 7
Pokitto 0:e8b8f36b4505 8 Software License Agreement (BSD License)
Pokitto 0:e8b8f36b4505 9
Pokitto 0:e8b8f36b4505 10 Copyright (c) 2016, Jonne Valola
Pokitto 0:e8b8f36b4505 11 All rights reserved.
Pokitto 0:e8b8f36b4505 12
Pokitto 0:e8b8f36b4505 13 Redistribution and use in source and binary forms, with or without
Pokitto 0:e8b8f36b4505 14 modification, are permitted provided that the following conditions are met:
Pokitto 0:e8b8f36b4505 15 1. Redistributions of source code must retain the above copyright
Pokitto 0:e8b8f36b4505 16 notice, this list of conditions and the following disclaimer.
Pokitto 0:e8b8f36b4505 17 2. Redistributions in binary form must reproduce the above copyright
Pokitto 0:e8b8f36b4505 18 notice, this list of conditions and the following disclaimer in the
Pokitto 0:e8b8f36b4505 19 documentation and/or other materials provided with the distribution.
Pokitto 0:e8b8f36b4505 20 3. Neither the name of the copyright holders nor the
Pokitto 0:e8b8f36b4505 21 names of its contributors may be used to endorse or promote products
Pokitto 0:e8b8f36b4505 22 derived from this software without specific prior written permission.
Pokitto 0:e8b8f36b4505 23
Pokitto 0:e8b8f36b4505 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
Pokitto 0:e8b8f36b4505 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Pokitto 0:e8b8f36b4505 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Pokitto 0:e8b8f36b4505 27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
Pokitto 0:e8b8f36b4505 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Pokitto 0:e8b8f36b4505 29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Pokitto 0:e8b8f36b4505 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
Pokitto 0:e8b8f36b4505 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Pokitto 0:e8b8f36b4505 32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Pokitto 0:e8b8f36b4505 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Pokitto 0:e8b8f36b4505 34 */
Pokitto 0:e8b8f36b4505 35 /**************************************************************************/
Pokitto 0:e8b8f36b4505 36
Pokitto 0:e8b8f36b4505 37 #include "PokittoDisplay.h"
Pokitto 0:e8b8f36b4505 38 #include "Pokitto_settings.h"
Pokitto 0:e8b8f36b4505 39 #include "GBcompatibility.h"
Pokitto 0:e8b8f36b4505 40 #include <stdio.h>
Pokitto 0:e8b8f36b4505 41 #include <string.h>
Pokitto 0:e8b8f36b4505 42
Pokitto 0:e8b8f36b4505 43 #ifndef POK_SIM
Pokitto 0:e8b8f36b4505 44 #include "HWLCD.h"
Pokitto 0:e8b8f36b4505 45 #else
Pokitto 0:e8b8f36b4505 46 #include "SimLCD.h"
Pokitto 0:e8b8f36b4505 47 #endif
Pokitto 0:e8b8f36b4505 48
Pokitto 0:e8b8f36b4505 49 using namespace Pokitto;
Pokitto 0:e8b8f36b4505 50
Pokitto 0:e8b8f36b4505 51
Pokitto 0:e8b8f36b4505 52 void Display::loadRGBPalette(const unsigned char* p) {
Pokitto 0:e8b8f36b4505 53 for (int i=0;i<PALETTE_SIZE;i++) palette[i] = RGBto565(p[i*3], p[i*3+1],p[i*3+2]);
Pokitto 0:e8b8f36b4505 54 paletteptr = palette;
Pokitto 0:e8b8f36b4505 55 }
Pokitto 0:e8b8f36b4505 56
Pokitto 0:e8b8f36b4505 57 void Display::load565Palette(const uint16_t* p) {
Pokitto 0:e8b8f36b4505 58 for (int i=0;i<PALETTE_SIZE;i++) palette[i] = p[i];
Pokitto 0:e8b8f36b4505 59 paletteptr = palette;
Pokitto 0:e8b8f36b4505 60 }
Pokitto 0:e8b8f36b4505 61
Pokitto 0:e8b8f36b4505 62 void Display::rotatePalette(int8_t step) {
Pokitto 0:e8b8f36b4505 63 uint16_t tpal[PALETTE_SIZE];
Pokitto 0:e8b8f36b4505 64 if (step == 0) return;
Pokitto 0:e8b8f36b4505 65 step = 0-step;
Pokitto 0:e8b8f36b4505 66 if (step>0) {
Pokitto 0:e8b8f36b4505 67 for (int i=step;i<PALETTE_SIZE;i++) tpal[i]=palette[i-step]; // palette revolves up, new color 1 becomes old color 0
Pokitto 0:e8b8f36b4505 68 for (int i=0; i < step; i++) tpal[i]=palette[PALETTE_SIZE-step+i]; // overflow topmost values to bottom of new palette
Pokitto 0:e8b8f36b4505 69 } else {
Pokitto 0:e8b8f36b4505 70 for (int i=0;i<PALETTE_SIZE+step;i++)
Pokitto 0:e8b8f36b4505 71 {
Pokitto 0:e8b8f36b4505 72 tpal[i]=palette[i-step];
Pokitto 0:e8b8f36b4505 73 }// palette revolves down, new color 0 becomes old color 1
Pokitto 0:e8b8f36b4505 74 for (int i=0;i<-step; i++) {
Pokitto 0:e8b8f36b4505 75 tpal[PALETTE_SIZE+step+i]=palette[i];
Pokitto 0:e8b8f36b4505 76 }
Pokitto 0:e8b8f36b4505 77 // overflow bottom values to top of new palette
Pokitto 0:e8b8f36b4505 78 }
Pokitto 0:e8b8f36b4505 79 for (int i=0; i<PALETTE_SIZE;i++) palette[i] = tpal[i];
Pokitto 0:e8b8f36b4505 80 }
Pokitto 0:e8b8f36b4505 81
Pokitto 0:e8b8f36b4505 82 uint16_t Display::RGBto565(uint8_t R,uint8_t G,uint8_t B) {
Pokitto 0:e8b8f36b4505 83 uint16_t color;
Pokitto 0:e8b8f36b4505 84 color = B>>3;
Pokitto 0:e8b8f36b4505 85 color |= ((G >> 2) << 5);
Pokitto 0:e8b8f36b4505 86 color |= ((R >> 3) << 11);
Pokitto 0:e8b8f36b4505 87 return color;
Pokitto 0:e8b8f36b4505 88 }
Pokitto 0:e8b8f36b4505 89
Pokitto 0:e8b8f36b4505 90 uint16_t Display::interpolateColor(uint16_t c1, uint16_t c2, uint8_t factor) {
Pokitto 0:e8b8f36b4505 91 int16_t R,G,B;
Pokitto 0:e8b8f36b4505 92 int16_t dR,dG,dB;
Pokitto 0:e8b8f36b4505 93 uint16_t color;
Pokitto 0:e8b8f36b4505 94
Pokitto 0:e8b8f36b4505 95 B = (c1 & 0x1F);
Pokitto 0:e8b8f36b4505 96 dB = (c2 & 0x1F)-B;
Pokitto 0:e8b8f36b4505 97 dB = (dB*factor)>>8;
Pokitto 0:e8b8f36b4505 98 B += dB;
Pokitto 0:e8b8f36b4505 99 if (B<0) B = 0;
Pokitto 0:e8b8f36b4505 100
Pokitto 0:e8b8f36b4505 101 G = ((c1>>5) & 0x3F);
Pokitto 0:e8b8f36b4505 102 dG = ((c2>>5) & 0x3F)-G;
Pokitto 0:e8b8f36b4505 103 dG = (dG*factor)>>8;
Pokitto 0:e8b8f36b4505 104 G += dG;
Pokitto 0:e8b8f36b4505 105 if (G<0) G=0;
Pokitto 0:e8b8f36b4505 106
Pokitto 0:e8b8f36b4505 107 R = (c1>>11);
Pokitto 0:e8b8f36b4505 108 dR = (c2>>11)-R;
Pokitto 0:e8b8f36b4505 109 dR = (dR*factor)>>8;
Pokitto 0:e8b8f36b4505 110 R += dR;
Pokitto 0:e8b8f36b4505 111 if (R<0) R=0;
Pokitto 0:e8b8f36b4505 112
Pokitto 0:e8b8f36b4505 113 color = B;
Pokitto 0:e8b8f36b4505 114 color |= (G << 5);
Pokitto 0:e8b8f36b4505 115 color |= (R << 11);
Pokitto 0:e8b8f36b4505 116 return color;
Pokitto 0:e8b8f36b4505 117 }
Pokitto 0:e8b8f36b4505 118
Pokitto 0:e8b8f36b4505 119 void Display::tweenPalette(uint16_t* ram_pal, const uint16_t* pal_1, const uint16_t* pal_2, uint8_t factor) {
Pokitto 0:e8b8f36b4505 120 for (uint8_t i = 0; i<16 ; i++) {
Pokitto 0:e8b8f36b4505 121 ram_pal[i] = interpolateColor(pal_1[i],pal_2[i],factor);
Pokitto 0:e8b8f36b4505 122 }
Pokitto 0:e8b8f36b4505 123 }
Pokitto 0:e8b8f36b4505 124
Pokitto 0:e8b8f36b4505 125
Pokitto 0:e8b8f36b4505 126