sotsuron

Fork of linearMirrorMotion1017 by Hiromasa Oku

renderclass.cpp

Committer:
takapiasano
Date:
2013-06-28
Revision:
22:e86d63dfbbe1
Parent:
21:4095a071724d
Child:
23:ce375ac0ddfe
Child:
24:ab74d2018e6c

File content as of revision 22:e86d63dfbbe1:

#include "renderclass.h"
#include "mbed.h"
#include "laserProjectorHardware.h"
#include <vector>

Ticker translationTimer;

//for translating -----------------------------------------------------------------

inline void render::shearing(point2dl& dataBefore)
{
    dataBefore.x = dataBefore.x - vx * speedTimer.read_us()/100; //2000000;//dataBefore.x - speed *speedTimer.read_us()/1000000 ;//- speed * cos(angle+PI/2)*speedTimer.read_us()/800;
    dataBefore.y = dataBefore.y - vy * speedTimer.read_us()/2000000;//dataBefore.y ;//- speed * sin(angle+PI/2)*speedTimer.read_us()/800;
}

inline void render::rotation(point2dl& dataBefore)
{
    int x=dataBefore.x;
    dataBefore.x = x + abs((int)angle) % (2 * (CENTER_AD_MIRROR_X-200)) -  CENTER_AD_MIRROR_X+200;//x + (CENTER_AD_MIRROR_X-200) * cos(angle);//radious * cos(angle) + sin(angle)*x - cos(angle)*dataBefore.y;//
    dataBefore.y = -dataBefore.y;//radious * sin(angle) - cos(angle)*x - sin(angle)*dataBefore.y;//
    
}
//----------------------------------------------------------------------------------

void render::startRender()
{
    speedTimer.start();
    renderTimer.attach_us(this, &render::draw,RENDER_INTERVAL);
    pc.printf("render started!\n");
}

void render::stopRender()
{
    renderTimer.detach();
    translated.clear();
    currentLetters=0;
    currentPoints=0;
}

void render::setRender(vector<letter> *p)
{
    ptext = p;
    pc.printf("Size of text to render: %d/n", (*ptext).size() );
    //for(int i=0; i!=(*ptext).size(); i++){
    for(std::vector<letter>::iterator itr = (*ptext).begin(); itr != (*ptext).end(); ++itr) {
        translated.push_back(*itr);
    }                                            //copy the liblary to the buffer.


    currentLetters=0;
    currentPoints=0;
    shearingSwitch = 1;
    radious = 1800;
}


void render::updateSpeed(float gspeed, float gangle, float vvx, float vvy)
{
    speed = gspeed;
    angle = gangle;

    vx = vvx;//gspeed * radious * cos(angle+PI/2);
    vy = vvy;//gspeed * radious * sin(angle+PI/2);


    //speed=sqrt(vx*vx+vy*vy);//*factorSpeed;
    //angle=atan2(vy,vx);
}

void render::draw()
{
    if(translated.size()>0) {
        point2dl drawPoint=translated[currentLetters].letpoints[currentPoints];
        transform(drawPoint);               // transform it with the current speed and angle
        IO.writeOutXY(CENTER_AD_MIRROR_X + (int)drawPoint.x , CENTER_AD_MIRROR_Y + (int)drawPoint.y);
        
        
        
        ////laser switch
        if(currentPoints>0) IO.setRedPower(translated[currentLetters].letpoints[currentPoints-1].laserSwitch);
        else                IO.setRedPower(0);

        currentPoints++;

        if(currentPoints >= translated[currentLetters].letpoints.size() ) {
            currentLetters++;
            currentPoints = 0;
            //renderTimer.detach();
 
            wait_us(RENDER_INTERVAL);
            IO.setRedPower(0);
            //wait_us(RENDER_INTERVAL);
            
            //renderTimer.attach_us(this, &render::draw,RENDER_INTERVAL);
            speedTimer.reset();
            //}
        }
        if(currentLetters >= translated.size()) {
            currentLetters = 0;
            currentPoints = 0;
            //renderTimer.detach();
            //wait_us(RENDER_INTERVAL);
            IO.setRedPower(0);
            //wait_us(RENDER_INTERVAL*20);
            //renderTimer.attach_us(this, &render::draw,RENDER_INTERVAL);
            speedTimer.reset();
        }
    }
}

void render::transform(point2dl& mypoint)
{
    rotation(mypoint);
    if(shearingSwitch == 1) shearing(mypoint);
}