tim010 tim010
/
LV7-Grupa1-Tim010
Pajić Enil, Jakić Irma
main.cpp@0:7fd4fdb57d7e, 2014-04-28 (annotated)
- Committer:
- tim010
- Date:
- Mon Apr 28 06:44:04 2014 +0000
- Revision:
- 0:7fd4fdb57d7e
LV7-Grupa1-Tim010
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tim010 | 0:7fd4fdb57d7e | 1 | #include "mbed.h" |
tim010 | 0:7fd4fdb57d7e | 2 | #include "N5110.h" |
tim010 | 0:7fd4fdb57d7e | 3 | //By Enil Pajić, 27.04.2014, ETF PAI |
tim010 | 0:7fd4fdb57d7e | 4 | DigitalOut ENBL (dp14); |
tim010 | 0:7fd4fdb57d7e | 5 | N5110 DISP (dp4, dp24, dp23, dp25, dp2, dp6, dp18); |
tim010 | 0:7fd4fdb57d7e | 6 | InterruptIn BTN (dp9); |
tim010 | 0:7fd4fdb57d7e | 7 | AnalogIn X (dp11), Y (dp10); //InterruptIn? |
tim010 | 0:7fd4fdb57d7e | 8 | Ticker Refresh, Wait; |
tim010 | 0:7fd4fdb57d7e | 9 | Timer Dbnc; |
tim010 | 0:7fd4fdb57d7e | 10 | int eX = 2, eY = 2, pX = 0, pY = 0; |
tim010 | 0:7fd4fdb57d7e | 11 | int Xs[12] = {1, 0, -1, -2, -2, -2, -1, 0, 1, 2, 2, 2}; |
tim010 | 0:7fd4fdb57d7e | 12 | int Ys[12] = {2, 2, 2, 1, 0, -1, -2, -2, -2, -1, 0, 1}; |
tim010 | 0:7fd4fdb57d7e | 13 | bool DrawIt = true; |
tim010 | 0:7fd4fdb57d7e | 14 | void Draw (int x, int y) |
tim010 | 0:7fd4fdb57d7e | 15 | { |
tim010 | 0:7fd4fdb57d7e | 16 | if (x < 2 || x > 81) return; // [0, 84) |
tim010 | 0:7fd4fdb57d7e | 17 | if (y < 2 || y > 45) return; // [0, 48) // y > 38 Or y > 30 /(-1) |
tim010 | 0:7fd4fdb57d7e | 18 | // if (x == pX && y == pY) return; //Da ne cistimo, refreshujemo i crtamo krug ako je na istim koordinatama |
tim010 | 0:7fd4fdb57d7e | 19 | pX = x, pY = y; |
tim010 | 0:7fd4fdb57d7e | 20 | DISP.clear(); |
tim010 | 0:7fd4fdb57d7e | 21 | DISP.refresh(); |
tim010 | 0:7fd4fdb57d7e | 22 | DISP.setXYAddress (0, 0); |
tim010 | 0:7fd4fdb57d7e | 23 | for (int a = 0; a < 12; ++a) |
tim010 | 0:7fd4fdb57d7e | 24 | DISP.setPixel (x + Xs[a], y + Ys[a]); |
tim010 | 0:7fd4fdb57d7e | 25 | DISP.refresh(); |
tim010 | 0:7fd4fdb57d7e | 26 | } |
tim010 | 0:7fd4fdb57d7e | 27 | double Zaokruzi (double broj) //Na 2 dec |
tim010 | 0:7fd4fdb57d7e | 28 | {return double(int(broj * 100.0 + .5) / 100.0); } |
tim010 | 0:7fd4fdb57d7e | 29 | void DajCifre (double N, int &Cio, int &PrvaDec, int &DrugaDec) //broj u #.## formatu |
tim010 | 0:7fd4fdb57d7e | 30 | { |
tim010 | 0:7fd4fdb57d7e | 31 | double dec = (N - (int)N); |
tim010 | 0:7fd4fdb57d7e | 32 | dec = Zaokruzi(dec * 100.); |
tim010 | 0:7fd4fdb57d7e | 33 | DrugaDec = (int)dec % 10; |
tim010 | 0:7fd4fdb57d7e | 34 | PrvaDec = ((int)dec / 10) % 10; |
tim010 | 0:7fd4fdb57d7e | 35 | Cio = (int)N; |
tim010 | 0:7fd4fdb57d7e | 36 | } |
tim010 | 0:7fd4fdb57d7e | 37 | void DoIt () |
tim010 | 0:7fd4fdb57d7e | 38 | { |
tim010 | 0:7fd4fdb57d7e | 39 | double vx = X, vy = Y; |
tim010 | 0:7fd4fdb57d7e | 40 | if (vx <= .2) eX = (eX <= 2 ? 2 : eX - 2); |
tim010 | 0:7fd4fdb57d7e | 41 | else if (vx <= .4) eX = (eX <= 2 ? 2 : eX - 1); |
tim010 | 0:7fd4fdb57d7e | 42 | else if (vx <= .6); |
tim010 | 0:7fd4fdb57d7e | 43 | else if (vx <= .8) eX = (eX >= 81 ? 81 : eX + 1); |
tim010 | 0:7fd4fdb57d7e | 44 | else eX = (eX >= 81 ? 81 : eX + 2); //Malo muzikanja :P ako jako povucemo, brze se pomjera |
tim010 | 0:7fd4fdb57d7e | 45 | if (vy <= .2) eY = (eY <= 2 ? 2 : eY - 2); //Ako slabije povucemo, sporije se pomjera |
tim010 | 0:7fd4fdb57d7e | 46 | else if (vy <= .4) eY = (eY <= 2 ? 2 : eY - 1); |
tim010 | 0:7fd4fdb57d7e | 47 | else if (vy <= .6); |
tim010 | 0:7fd4fdb57d7e | 48 | else if (vy <= .8) eY = (eY >= 45 ? 45 : eY + 1); |
tim010 | 0:7fd4fdb57d7e | 49 | else eY = (eY >= 45 ? 45 : eY + 2); |
tim010 | 0:7fd4fdb57d7e | 50 | Draw (eX, eY); |
tim010 | 0:7fd4fdb57d7e | 51 | if (!DrawIt) return; |
tim010 | 0:7fd4fdb57d7e | 52 | int xCio = 0, xPD = 0, xDD = 0, yCio = 0, yPD = 0, yDD = 0; |
tim010 | 0:7fd4fdb57d7e | 53 | DajCifre (vx, xCio, xPD, xDD); |
tim010 | 0:7fd4fdb57d7e | 54 | DajCifre (vy, yCio, yPD, yDD); |
tim010 | 0:7fd4fdb57d7e | 55 | char ch[13] = "(0.00, 0.00)"; |
tim010 | 0:7fd4fdb57d7e | 56 | ch[1] = 48 + xCio; ch[3] = 48 + xPD; ch[4] = 48 + xDD; |
tim010 | 0:7fd4fdb57d7e | 57 | ch[7] = 48 + yCio; ch[9] = 48 + yPD; ch[10] = 48 + yDD; |
tim010 | 0:7fd4fdb57d7e | 58 | char ph [9] = "(00, 00)"; |
tim010 | 0:7fd4fdb57d7e | 59 | ph[1] = 48 + eX / 10; ph[2] = 48 + eX % 10; |
tim010 | 0:7fd4fdb57d7e | 60 | ph[5] = 48 + eY / 10; ph[6] = 48 + eY % 10; |
tim010 | 0:7fd4fdb57d7e | 61 | DISP.printString (ch, 0, 5); |
tim010 | 0:7fd4fdb57d7e | 62 | DISP.printString (ph, 0, 4); // DISP.printString (ph, 0, 4); |
tim010 | 0:7fd4fdb57d7e | 63 | } |
tim010 | 0:7fd4fdb57d7e | 64 | void Enable() {DrawIt = true; Wait.detach();} //Provjeriti !? |
tim010 | 0:7fd4fdb57d7e | 65 | void Pressed() |
tim010 | 0:7fd4fdb57d7e | 66 | { |
tim010 | 0:7fd4fdb57d7e | 67 | if (Dbnc.read() > .2) |
tim010 | 0:7fd4fdb57d7e | 68 | { |
tim010 | 0:7fd4fdb57d7e | 69 | if (!DrawIt) return; |
tim010 | 0:7fd4fdb57d7e | 70 | DrawIt = false; |
tim010 | 0:7fd4fdb57d7e | 71 | Wait.attach (&Enable, 5.0); |
tim010 | 0:7fd4fdb57d7e | 72 | } |
tim010 | 0:7fd4fdb57d7e | 73 | Dbnc.reset(); |
tim010 | 0:7fd4fdb57d7e | 74 | } |
tim010 | 0:7fd4fdb57d7e | 75 | int main() |
tim010 | 0:7fd4fdb57d7e | 76 | { |
tim010 | 0:7fd4fdb57d7e | 77 | ENBL = 1; |
tim010 | 0:7fd4fdb57d7e | 78 | BTN.mode (PullUp); |
tim010 | 0:7fd4fdb57d7e | 79 | Dbnc.start(); |
tim010 | 0:7fd4fdb57d7e | 80 | BTN.rise (&Pressed); |
tim010 | 0:7fd4fdb57d7e | 81 | DISP.init(); |
tim010 | 0:7fd4fdb57d7e | 82 | Refresh.attach (&DoIt, 0.05); //Regulirati: 0.2, 0.1, 0.05, 0.01 |
tim010 | 0:7fd4fdb57d7e | 83 | while(1); //While kakvog profesor želi :P |
tim010 | 0:7fd4fdb57d7e | 84 | } |