Update
[anarch.git] / main_espboy.ino
bloba87f4daeebb9711fe61e6de64d256a6cad8a501d
1 /**
2   @file main_espboy.ino
4   This is ESPboy implementation of the game front end, using Arduino
5   libraries.
7   by Miloslav Ciz (drummyfish), 2021
9   Sadly compiling can't be done with any other optimization flag than -Os.
11   Released under CC0 1.0 (https://creativecommons.org/publicdomain/zero/1.0/)
12   plus a waiver of all other intellectual property. The goal of this work is to
13   be and remain completely in the public domain forever, available for any use
14   whatsoever.
17 #define SFG_CAN_SAVE 1 // for version without saving, set this to 0
19 #define SFG_FOV_HORIZONTAL 240
21 #define SFG_SCREEN_RESOLUTION_X 128
22 #define SFG_SCREEN_RESOLUTION_Y 128
23 #define SFG_FPS 22
24 #define SFG_RAYCASTING_MAX_STEPS 20  
25 #define SFG_RAYCASTING_SUBSAMPLE 2
26 #define SFG_DIMINISH_SPRITES 1
27 #define SFG_CAN_EXIT 0
28 #define SFG_DITHERED_SHADOW 1
30 #define SFG_AVR 1
32 #include <Arduino.h>
33 #include <Adafruit_MCP23017.h>
34 #include <Adafruit_MCP4725.h>
35 #include <TFT_eSPI.h>
37 #if SFG_CAN_SAVE
38   #include <ESP_EEPROM.h>
39   #define SAVE_VALID_VALUE 173
40   EEPROMClass eeprom;
41 #endif
42     
43 #define MCP23017address 0
44 #define MCP4725address  0x60
46 Adafruit_MCP23017 mcp;
47 Adafruit_MCP4725 dac;
48 TFT_eSPI tft;
50 #include "game.h"
52 #define SAVE_VALID_VALUE 73
54 uint8_t gamescreen[SFG_SCREEN_RESOLUTION_X * SFG_SCREEN_RESOLUTION_Y];
55 uint8_t keys;
57 void SFG_setPixel(uint16_t x, uint16_t y, uint8_t colorIndex)
59   gamescreen[y * SFG_SCREEN_RESOLUTION_X + x] = colorIndex;
62 void SFG_sleepMs(uint16_t timeMs)
66 uint32_t SFG_getTimeMs()
68   return millis();
71 int8_t SFG_keyPressed(uint8_t key)
73   switch (key)
74   {
75     case SFG_KEY_UP:    return keys & 0x02; break;
76     case SFG_KEY_DOWN:  return keys & 0x04; break;
77     case SFG_KEY_RIGHT: return keys & 0x08; break;
78     case SFG_KEY_LEFT:  return keys & 0x01; break;
79     case SFG_KEY_A:     return keys & 0x10; break;
80     case SFG_KEY_B:     return keys & 0x20; break;
81     case SFG_KEY_C:     return keys & 0x80; break;
82     case SFG_KEY_MAP:   return keys & 0x40; break;
83     default: return 0; break;
84   }
87 void SFG_getMouseOffset(int16_t *x, int16_t *y)
91 void SFG_setMusic(uint8_t value)
95 void SFG_save(uint8_t data[SFG_SAVE_SIZE])
97 #if SFG_CAN_SAVE
98   eeprom.write(0,SAVE_VALID_VALUE);
100   for (uint8_t i = 0; i < SFG_SAVE_SIZE; ++i)
101       eeprom.write(i + 1,data[i]);
103   eeprom.commit();
104 #endif
107 void SFG_processEvent(uint8_t event, uint8_t data)
111 uint8_t SFG_load(uint8_t data[SFG_SAVE_SIZE])
113 #if SFG_CAN_SAVE
114   if (eeprom.read(0) == SAVE_VALID_VALUE)
115     for (uint8_t i = 0; i < SFG_SAVE_SIZE; ++i)
116       data[i] = eeprom.read(i + 1);
118   return 1;
119 #else
120   return 0;
121 #endif
124 void SFG_playSound(uint8_t soundIndex, uint8_t volume)
126   int freq = 400;
127   int dur = 75;
129   switch (soundIndex)
130   {
131     case 0: freq = 120; dur = 250; break; // shot
132     case 1: freq = 200; dur = 260; break; // door
133     case 2: freq = 80;  dur = 200; break; // explosion
134     case 3: freq = 220; dur = 50; break;  // click
135     case 4: freq = 180; dur = 200; break; // plasma
136     case 5: freq = 300; dur = 100; break; // monster
137     default: break;
138   }
140   tone(D3,freq,dur);
143 void setup()
145   dac.begin(MCP4725address);
146   delay (100);
147   dac.setVoltage(0,false);
148   mcp.begin(MCP23017address);
149   delay(100);
151   // buttons
152   for (uint8_t i = 0; i < 8; i++)
153   {
154     mcp.pinMode(i,INPUT);
155     mcp.pullUp(i,HIGH);
156   }
158   mcp.pinMode(8,OUTPUT);
159   mcp.digitalWrite(8,LOW);
160   
161   tft.begin();
162   delay(100);
163   tft.setRotation(0);
165   tft.setAddrWindow(0,128,0,128);
166   tft.fillScreen(TFT_BLACK);
168   dac.setVoltage(4095,true); // backlight
170   pinMode(D3,OUTPUT); // sound
172 #if SFG_CAN_SAVE
173   eeprom.begin(SFG_SAVE_SIZE + 1);
174 #endif
176   SFG_init();
179 void loop()
181   keys = ~mcp.readGPIOAB() & 255;
182   
183   SFG_mainLoopBody();
185   uint8_t *pixel = gamescreen;
187   for (int i = 0; i < SFG_SCREEN_RESOLUTION_X * SFG_SCREEN_RESOLUTION_Y; ++i)
188   {
189     tft.pushColor(pgm_read_word(paletteRGB565 + *pixel));
190     pixel++;
191   }