heightmap eeprom save and load

This commit is contained in:
interfisch 2019-04-07 16:07:55 +02:00
parent 6c39c1560e
commit bf26f6301c
5 changed files with 52 additions and 151 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
backup/

View File

@ -5,6 +5,9 @@
#include <avr/power.h> #include <avr/power.h>
#endif #endif
#include <EEPROM.h>
#define EEPROMSTARTADDRESS_HEIGHTMAP 8
#include <vector> #include <vector>
#include "wagon.h" #include "wagon.h"
#include "effect.h" #include "effect.h"
@ -57,6 +60,8 @@ Effect* effect = NULL;
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
EEPROM.begin(4096); //set eeprom size, between 4 and 4096 bytes.
strip.begin(); strip.begin();
strip.setBrightness(BRIGHTNESS_RUN); //150 strip.setBrightness(BRIGHTNESS_RUN); //150
strip.show(); // Initialize all pixels to 'off' strip.show(); // Initialize all pixels to 'off'
@ -65,46 +70,7 @@ void setup() {
resetHeightmap(); resetHeightmap();
//fixed heightmap //fixed heightmap
heightraw[0]=182; loadHeightmapRaw();
heightraw[51]=100;
heightraw[91]=57;
heightraw[121]=100;
heightraw[147]=100;
heightraw[170]=75;
heightraw[184]=71;
heightraw[216]=100;
heightraw[228]=103;
heightraw[257]=101;
heightraw[305]=107;
heightraw[316]=100;
heightraw[349]=78;
heightraw[386]=100;
heightraw[420]=83;
heightraw[438]=78;
heightraw[460]=83;
heightraw[489]=95;
heightraw[515]=92;
heightraw[566]=100;
heightraw[580]=103;
heightraw[599]=89;
interpolateHeightValues(); interpolateHeightValues();
@ -312,6 +278,10 @@ void checkSerial(){
configmode=true; configmode=true;
}else if (serialstring.equals("print")){ }else if (serialstring.equals("print")){
printHeightmapRaw(); printHeightmapRaw();
}else if (serialstring.equals("save")){
saveHeightmapRaw(); //save to eeprom
}else if (serialstring.equals("load")){
loadHeightmapRaw(); //load from eeprom
}else if (serialstring.equals("remove")){ }else if (serialstring.equals("remove")){
removeAllWagons(); removeAllWagons();
}else if (serialstring.equals("clear")){ }else if (serialstring.equals("clear")){
@ -553,3 +523,25 @@ uint32_t Wheel(byte WheelPos) {
WheelPos -= 170; WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} }
void loadHeightmapRaw(){
int c_eepromaddress=EEPROMSTARTADDRESS_HEIGHTMAP;
uint8_t lastvalue=0;
for (int i=0;i<NUMPIXELS;i++){
lastvalue = EEPROM.read(c_eepromaddress);
heightraw[i]=lastvalue;
c_eepromaddress++;
}
interpolateHeightValues();
}
void saveHeightmapRaw(){
int c_eepromaddress=EEPROMSTARTADDRESS_HEIGHTMAP;
for (int i=0;i<NUMPIXELS;i++){
EEPROM.write(c_eepromaddress, heightraw[i]); //address, value
c_eepromaddress++;
}
EEPROM.commit(); //write changes to eeprom. EEPROM.end() will also commit and release the ram copy of eeprom contents
}

View File

@ -76,6 +76,13 @@ void setup() {
.setPosition(10, 80+60+5) .setPosition(10, 80+60+5)
.setSize(100, 10); .setSize(100, 10);
cp5.addButton("load_heightmap")
.setPosition(10, 80+60+5+15)
.setSize(100, 10);
cp5.addButton("save_heightmap")
.setPosition(10, 80+60+5+15*2)
.setSize(100, 10);
cp5.addButton("Remove") cp5.addButton("Remove")
.setPosition(100, 80) .setPosition(100, 80)
.setSize(50, 50); .setSize(50, 50);
@ -144,6 +151,7 @@ void setup() {
.setSize(100, 20); .setSize(100, 20);
myPort = new Serial(this, "/dev/ttyUSB0", 115200); myPort = new Serial(this, "/dev/ttyUSB0", 115200);
} }
@ -157,6 +165,16 @@ void ClearHeightmap(){
myPort.write(writeserial); myPort.write(writeserial);
} }
void load_heightmap(){
String writeserial="load\n";
myPort.write(writeserial);
}
void save_heightmap(){
String writeserial="save\n";
myPort.write(writeserial);
}
void Debug() { void Debug() {
String writeserial="debug\n"; String writeserial="debug\n";
myPort.write(writeserial); myPort.write(writeserial);

View File

@ -1,84 +0,0 @@
#include "fx_scanner.h"
FX_Scanner::FX_Scanner(int numpixels,Adafruit_NeoPixel *strip,uint8_t *height,int startpos,float scannervel,uint32_t scannercolor)
{
_numpixels=numpixels;
_pos = startpos;
_strip=strip;
_height=height;
_vel=scannervel;
_scannercolor=scannercolor;
}
FX_Scanner::FX_Scanner()
{
_numpixels=0;
_pos = -1; //for active false
_strip=0;
_height=0;
_vel=0;
_scannercolor=0;
}
void FX_Scanner::updateRoutine(float updatedelayms)
{
_pos+=_vel*updatedelayms/1000.0;
}
void FX_Scanner::updateGraphics()
{
#define FEATHERDISTANCE 3 //in both directions
for(int i=0;i<_numpixels;i++){
float heightdistfromlaser=abs(_height[i]-_pos);
if (heightdistfromlaser<=FEATHERDISTANCE) {
uint8_t _r = _scannercolor >> 16;
uint8_t _g = _scannercolor >> 8;
uint8_t _b = _scannercolor;
float distmult=1.0-(heightdistfromlaser/FEATHERDISTANCE);
_r*=distmult;
_g*=distmult;
_b*=distmult;
uint32_t _pxcolor=_strip->getPixelColor(i); //get current color of that pixel
uint8_t _pxr = _pxcolor >> 16;
uint8_t _pxg = _pxcolor >> 8;
uint8_t _pxb = _pxcolor;
uint16_t _tmpr=_pxr+_r; //add colors
uint16_t _tmpg=_pxg+_g;
uint16_t _tmpb=_pxb+_b;
if (_tmpr>255){ //clamp
_tmpr=255;
}
if (_tmpg>255){
_tmpg=255;
}
if (_tmpb>255){
_tmpb=255;
}
_strip->setPixelColor(i,_tmpr,_tmpg,_tmpb); //draw pixel
}
}
}
bool FX_Scanner::active()
{
if (_pos>256 || _pos<0){
return false;
}
return true;
}
uint32_t FX_Scanner::Wheel(byte WheelPos,float brightness) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return _strip->Color(255 - WheelPos * 3*brightness, 0, WheelPos * 3*brightness);
}
if(WheelPos < 170) {
WheelPos -= 85;
return _strip->Color(0, WheelPos * 3*brightness, 255 - WheelPos * 3*brightness);
}
WheelPos -= 170;
return _strip->Color(WheelPos * 3*brightness, 255 - WheelPos * 3*brightness, 0);
}

View File

@ -1,26 +0,0 @@
#ifndef FX_SCANNER_H
#define FX_SCANNER_H
#include <Adafruit_NeoPixel.h>
#include <math.h>
class FX_Scanner
{
public:
FX_Scanner(int numpixels,Adafruit_NeoPixel *strip,uint8_t *height,int startpos,float scannervel,uint32_t scannercolor);
FX_Scanner();
void updateRoutine(float updatedelayms);
void updateGraphics();
uint32_t Wheel(byte WheelPos,float brightness);
bool active();
private:
int _numpixels;
Adafruit_NeoPixel *_strip;
float _pos;
float _vel;
uint8_t *_height;
uint32_t _scannercolor;
};
#endif