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

Committer:
Pokitto
Date:
Tue Jan 30 10:41:47 2018 +0000
Revision:
31:f4b9b85c7b62
Sound output improvements added:  louder, clearer, faster!

Who changed what in which revision?

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