Takashi Asano
/
linearMirrorMotion1017
sotsuron
Fork of linearMirrorMotion1017 by
renderclass.cpp@24:ab74d2018e6c, 2013-07-02 (annotated)
- Committer:
- takapiasano
- Date:
- Tue Jul 02 08:26:45 2013 +0000
- Revision:
- 24:ab74d2018e6c
- Parent:
- 22:e86d63dfbbe1
- Child:
- 25:423050363215
draw function betterized
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hiromasaoku | 16:6dd2e60bc5bc | 1 | #include "renderclass.h" |
hiromasaoku | 16:6dd2e60bc5bc | 2 | #include "mbed.h" |
hiromasaoku | 16:6dd2e60bc5bc | 3 | #include "laserProjectorHardware.h" |
hiromasaoku | 16:6dd2e60bc5bc | 4 | #include <vector> |
hiromasaoku | 16:6dd2e60bc5bc | 5 | |
hiromasaoku | 16:6dd2e60bc5bc | 6 | Ticker translationTimer; |
hiromasaoku | 16:6dd2e60bc5bc | 7 | |
hiromasaoku | 16:6dd2e60bc5bc | 8 | //for translating ----------------------------------------------------------------- |
hiromasaoku | 16:6dd2e60bc5bc | 9 | |
hiromasaoku | 18:6f86abfae754 | 10 | inline void render::shearing(point2dl& dataBefore) |
hiromasaoku | 18:6f86abfae754 | 11 | { |
takapiasano | 22:e86d63dfbbe1 | 12 | 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; |
takapiasano | 22:e86d63dfbbe1 | 13 | dataBefore.y = dataBefore.y - vy * speedTimer.read_us()/2000000;//dataBefore.y ;//- speed * sin(angle+PI/2)*speedTimer.read_us()/800; |
hiromasaoku | 16:6dd2e60bc5bc | 14 | } |
hiromasaoku | 16:6dd2e60bc5bc | 15 | |
hiromasaoku | 18:6f86abfae754 | 16 | inline void render::rotation(point2dl& dataBefore) |
hiromasaoku | 18:6f86abfae754 | 17 | { |
hiromasaoku | 18:6f86abfae754 | 18 | int x=dataBefore.x; |
takapiasano | 22:e86d63dfbbe1 | 19 | 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;// |
takapiasano | 22:e86d63dfbbe1 | 20 | dataBefore.y = -dataBefore.y;//radious * sin(angle) - cos(angle)*x - sin(angle)*dataBefore.y;// |
takapiasano | 24:ab74d2018e6c | 21 | |
hiromasaoku | 16:6dd2e60bc5bc | 22 | } |
hiromasaoku | 16:6dd2e60bc5bc | 23 | //---------------------------------------------------------------------------------- |
hiromasaoku | 16:6dd2e60bc5bc | 24 | |
hiromasaoku | 18:6f86abfae754 | 25 | void render::startRender() |
hiromasaoku | 18:6f86abfae754 | 26 | { |
hiromasaoku | 20:8475768fc2f7 | 27 | speedTimer.start(); |
hiromasaoku | 19:8e9fe7d25b9c | 28 | renderTimer.attach_us(this, &render::draw,RENDER_INTERVAL); |
hiromasaoku | 19:8e9fe7d25b9c | 29 | pc.printf("render started!\n"); |
hiromasaoku | 16:6dd2e60bc5bc | 30 | } |
hiromasaoku | 16:6dd2e60bc5bc | 31 | |
hiromasaoku | 18:6f86abfae754 | 32 | void render::stopRender() |
hiromasaoku | 18:6f86abfae754 | 33 | { |
hiromasaoku | 16:6dd2e60bc5bc | 34 | renderTimer.detach(); |
takapiasano | 24:ab74d2018e6c | 35 | speedTimer.stop(); |
hiromasaoku | 16:6dd2e60bc5bc | 36 | } |
hiromasaoku | 16:6dd2e60bc5bc | 37 | |
hiromasaoku | 18:6f86abfae754 | 38 | void render::setRender(vector<letter> *p) |
hiromasaoku | 18:6f86abfae754 | 39 | { |
takapiasano | 24:ab74d2018e6c | 40 | translated.clear(); |
takapiasano | 24:ab74d2018e6c | 41 | |
hiromasaoku | 16:6dd2e60bc5bc | 42 | ptext = p; |
hiromasaoku | 19:8e9fe7d25b9c | 43 | pc.printf("Size of text to render: %d/n", (*ptext).size() ); |
hiromasaoku | 16:6dd2e60bc5bc | 44 | //for(int i=0; i!=(*ptext).size(); i++){ |
takapiasano | 24:ab74d2018e6c | 45 | int count=0; |
hiromasaoku | 18:6f86abfae754 | 46 | for(std::vector<letter>::iterator itr = (*ptext).begin(); itr != (*ptext).end(); ++itr) { |
takapiasano | 24:ab74d2018e6c | 47 | letter translatedLetter; |
takapiasano | 24:ab74d2018e6c | 48 | for(int i=0; i<(*itr).letpoints.size(); i++) { |
takapiasano | 24:ab74d2018e6c | 49 | point2dl tranPoint; |
takapiasano | 24:ab74d2018e6c | 50 | tranPoint.x=itr->letpoints[i].x+560*count; |
takapiasano | 24:ab74d2018e6c | 51 | tranPoint.y=itr->letpoints[i].y; |
takapiasano | 24:ab74d2018e6c | 52 | tranPoint.laserSwitch=itr->letpoints[i].laserSwitch; |
takapiasano | 24:ab74d2018e6c | 53 | translatedLetter.letpoints.push_back(tranPoint); |
takapiasano | 24:ab74d2018e6c | 54 | |
hiromasaoku | 16:6dd2e60bc5bc | 55 | } //copy the liblary to the buffer. |
takapiasano | 24:ab74d2018e6c | 56 | translated.push_back(translatedLetter); |
takapiasano | 24:ab74d2018e6c | 57 | count++; |
takapiasano | 24:ab74d2018e6c | 58 | } |
takapiasano | 21:4095a071724d | 59 | |
takapiasano | 24:ab74d2018e6c | 60 | shearingSwitch = 1; |
takapiasano | 24:ab74d2018e6c | 61 | radious = 1800; |
takapiasano | 21:4095a071724d | 62 | |
hiromasaoku | 18:6f86abfae754 | 63 | currentLetters=0; |
takapiasano | 24:ab74d2018e6c | 64 | |
takapiasano | 24:ab74d2018e6c | 65 | firstPointCounter=0; |
takapiasano | 24:ab74d2018e6c | 66 | lastPointCounter=0; |
takapiasano | 24:ab74d2018e6c | 67 | |
takapiasano | 24:ab74d2018e6c | 68 | rendererState=FIRST_POINT; |
hiromasaoku | 16:6dd2e60bc5bc | 69 | } |
hiromasaoku | 16:6dd2e60bc5bc | 70 | |
hiromasaoku | 18:6f86abfae754 | 71 | |
takapiasano | 22:e86d63dfbbe1 | 72 | void render::updateSpeed(float gspeed, float gangle, float vvx, float vvy) |
takapiasano | 21:4095a071724d | 73 | { |
hiromasaoku | 19:8e9fe7d25b9c | 74 | speed = gspeed; |
hiromasaoku | 19:8e9fe7d25b9c | 75 | angle = gangle; |
takapiasano | 21:4095a071724d | 76 | |
takapiasano | 22:e86d63dfbbe1 | 77 | vx = vvx;//gspeed * radious * cos(angle+PI/2); |
takapiasano | 22:e86d63dfbbe1 | 78 | vy = vvy;//gspeed * radious * sin(angle+PI/2); |
takapiasano | 22:e86d63dfbbe1 | 79 | |
takapiasano | 22:e86d63dfbbe1 | 80 | |
hiromasaoku | 19:8e9fe7d25b9c | 81 | //speed=sqrt(vx*vx+vy*vy);//*factorSpeed; |
hiromasaoku | 19:8e9fe7d25b9c | 82 | //angle=atan2(vy,vx); |
hiromasaoku | 18:6f86abfae754 | 83 | } |
hiromasaoku | 20:8475768fc2f7 | 84 | |
hiromasaoku | 18:6f86abfae754 | 85 | void render::draw() |
hiromasaoku | 18:6f86abfae754 | 86 | { |
takapiasano | 21:4095a071724d | 87 | if(translated.size()>0) { |
takapiasano | 24:ab74d2018e6c | 88 | |
takapiasano | 24:ab74d2018e6c | 89 | switch(rendererState) { |
takapiasano | 24:ab74d2018e6c | 90 | |
takapiasano | 24:ab74d2018e6c | 91 | case NORMAL_POINT: |
takapiasano | 24:ab74d2018e6c | 92 | point2dl drawPoint=translated[currentLetters].letpoints[currentPoints]; |
takapiasano | 24:ab74d2018e6c | 93 | transform(drawPoint); // transform it with the current speed and angle |
takapiasano | 24:ab74d2018e6c | 94 | IO.writeOutXY(CENTER_AD_MIRROR_X + (int)drawPoint.x , CENTER_AD_MIRROR_Y + (int)drawPoint.y); |
takapiasano | 24:ab74d2018e6c | 95 | IO.setRedPower(translated[currentLetters].letpoints[currentPoints].laserSwitch); |
takapiasano | 21:4095a071724d | 96 | |
takapiasano | 24:ab74d2018e6c | 97 | // move to the next point |
takapiasano | 24:ab74d2018e6c | 98 | currentPoints++; |
takapiasano | 24:ab74d2018e6c | 99 | |
takapiasano | 24:ab74d2018e6c | 100 | //Should we change the state? |
takapiasano | 24:ab74d2018e6c | 101 | if(currentPoints >= translated[currentLetters].letpoints.size() ) { |
takapiasano | 24:ab74d2018e6c | 102 | rendererState=AFTER_LAST_POINT; |
takapiasano | 24:ab74d2018e6c | 103 | } |
takapiasano | 24:ab74d2018e6c | 104 | break; |
hiromasaoku | 18:6f86abfae754 | 105 | |
takapiasano | 24:ab74d2018e6c | 106 | case AFTER_LAST_POINT: |
takapiasano | 24:ab74d2018e6c | 107 | // just wait! |
takapiasano | 24:ab74d2018e6c | 108 | if (lastPointCounter<WAITING_LAST) { |
takapiasano | 24:ab74d2018e6c | 109 | IO.setRedPower(0); |
takapiasano | 24:ab74d2018e6c | 110 | lastPointCounter++; |
takapiasano | 24:ab74d2018e6c | 111 | } |
takapiasano | 24:ab74d2018e6c | 112 | else { |
takapiasano | 24:ab74d2018e6c | 113 | lastPointCounter=0; |
takapiasano | 24:ab74d2018e6c | 114 | currentPoints = 0; |
takapiasano | 24:ab74d2018e6c | 115 | // switch the laser off and move to the first point of the next letter: |
takapiasano | 24:ab74d2018e6c | 116 | |
takapiasano | 24:ab74d2018e6c | 117 | currentLetters++; |
takapiasano | 24:ab74d2018e6c | 118 | if(currentLetters >= translated.size()) { |
takapiasano | 24:ab74d2018e6c | 119 | currentLetters = 0; |
takapiasano | 24:ab74d2018e6c | 120 | speedTimer.reset(); |
takapiasano | 24:ab74d2018e6c | 121 | } |
takapiasano | 24:ab74d2018e6c | 122 | rendererState=FIRST_POINT; |
takapiasano | 24:ab74d2018e6c | 123 | } |
takapiasano | 24:ab74d2018e6c | 124 | break; |
takapiasano | 24:ab74d2018e6c | 125 | |
takapiasano | 24:ab74d2018e6c | 126 | case FIRST_POINT: |
takapiasano | 24:ab74d2018e6c | 127 | if (firstPointCounter<WAITING_FIRST) { |
takapiasano | 24:ab74d2018e6c | 128 | firstPointCounter++; |
takapiasano | 24:ab74d2018e6c | 129 | |
takapiasano | 24:ab74d2018e6c | 130 | // show first point! |
takapiasano | 24:ab74d2018e6c | 131 | point2dl drawPoint=translated[currentLetters].letpoints[0]; |
takapiasano | 24:ab74d2018e6c | 132 | transform(drawPoint); // transform it with the current speed and angle |
takapiasano | 24:ab74d2018e6c | 133 | IO.writeOutXY(CENTER_AD_MIRROR_X + (int)drawPoint.x , CENTER_AD_MIRROR_Y + (int)drawPoint.y); |
takapiasano | 24:ab74d2018e6c | 134 | //IO.setRedPower(translated[currentLetters].letpoints[currentPoints].laserSwitch); |
takapiasano | 24:ab74d2018e6c | 135 | |
takapiasano | 24:ab74d2018e6c | 136 | } else { |
takapiasano | 24:ab74d2018e6c | 137 | firstPointCounter=0; |
takapiasano | 24:ab74d2018e6c | 138 | currentPoints=1; |
takapiasano | 24:ab74d2018e6c | 139 | rendererState=NORMAL_POINT; |
takapiasano | 24:ab74d2018e6c | 140 | } |
takapiasano | 24:ab74d2018e6c | 141 | break; |
takapiasano | 24:ab74d2018e6c | 142 | } |
hiromasaoku | 20:8475768fc2f7 | 143 | } |
hiromasaoku | 16:6dd2e60bc5bc | 144 | } |
hiromasaoku | 18:6f86abfae754 | 145 | |
takapiasano | 24:ab74d2018e6c | 146 | void render::transform(point2dl& mypoint) { |
takapiasano | 24:ab74d2018e6c | 147 | rotation(mypoint); |
takapiasano | 24:ab74d2018e6c | 148 | if(shearingSwitch == 1) shearing(mypoint); |
takapiasano | 24:ab74d2018e6c | 149 | } |
hiromasaoku | 16:6dd2e60bc5bc | 150 |