Dimitar Marinov
/
Nucleo_4_encoders_w_Nokia5110
4 Rotary encoders with 5110 LCD display. For Nucleo boards
main.cpp@5:0daf19d95cc7, 2016-10-22 (annotated)
- Committer:
- triochi
- Date:
- Sat Oct 22 17:01:11 2016 +0000
- Revision:
- 5:0daf19d95cc7
- Parent:
- 4:cd50b7dfaf27
- Child:
- 6:920bfbc7a7a1
somewhat working rotary encoder
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
triochi | 0:b7e471386653 | 1 | #include "mbed.h" |
triochi | 0:b7e471386653 | 2 | #include "N5110.h" |
triochi | 1:d7d729404013 | 3 | #include "REnc.h" |
triochi | 5:0daf19d95cc7 | 4 | |
triochi | 0:b7e471386653 | 5 | |
triochi | 0:b7e471386653 | 6 | //DigitalOut red(LED1); |
triochi | 0:b7e471386653 | 7 | //DigitalOut blue(LED2); |
triochi | 0:b7e471386653 | 8 | //DigitalOut green(LED3); |
triochi | 0:b7e471386653 | 9 | int i; |
triochi | 0:b7e471386653 | 10 | |
triochi | 0:b7e471386653 | 11 | // VCC,SCE, RST, D/C, MOSI,SCLK,LED |
triochi | 0:b7e471386653 | 12 | N5110 lcd(PB_8,PA_4,PA_0,PA_1,PA_7,PA_5,PB_9); //PA_4 and PA_6 not used |
triochi | 1:d7d729404013 | 13 | REnc encoder(PC_3, PC_2); |
triochi | 1:d7d729404013 | 14 | int temperature = 50; |
triochi | 1:d7d729404013 | 15 | |
triochi | 1:d7d729404013 | 16 | void CCW_Handle(void) |
triochi | 1:d7d729404013 | 17 | { |
triochi | 1:d7d729404013 | 18 | temperature--; |
triochi | 1:d7d729404013 | 19 | } |
triochi | 1:d7d729404013 | 20 | |
triochi | 1:d7d729404013 | 21 | void CW_Handle(void) |
triochi | 1:d7d729404013 | 22 | { |
triochi | 1:d7d729404013 | 23 | temperature++; |
triochi | 1:d7d729404013 | 24 | } |
triochi | 1:d7d729404013 | 25 | |
triochi | 0:b7e471386653 | 26 | |
triochi | 0:b7e471386653 | 27 | int main() { |
triochi | 0:b7e471386653 | 28 | // first need to initialise display |
triochi | 0:b7e471386653 | 29 | lcd.init(); |
triochi | 1:d7d729404013 | 30 | encoder.setHandleCCW(CCW_Handle); |
triochi | 2:7d10aa2795c5 | 31 | encoder.setHandleCC(CW_Handle); |
triochi | 0:b7e471386653 | 32 | |
triochi | 0:b7e471386653 | 33 | // while(1) { |
triochi | 0:b7e471386653 | 34 | // for (i=1; i<7; i++) { |
triochi | 0:b7e471386653 | 35 | // red = i & 1; |
triochi | 0:b7e471386653 | 36 | // blue = i & 2; |
triochi | 0:b7e471386653 | 37 | // green = i & 4; |
triochi | 0:b7e471386653 | 38 | // wait(0.2); |
triochi | 0:b7e471386653 | 39 | // } |
triochi | 0:b7e471386653 | 40 | // } |
triochi | 0:b7e471386653 | 41 | while(1) { |
triochi | 0:b7e471386653 | 42 | |
triochi | 0:b7e471386653 | 43 | // these are default settings so not strictly needed |
triochi | 0:b7e471386653 | 44 | lcd.normalMode(); // normal colour mode |
triochi | 0:b7e471386653 | 45 | lcd.setBrightness(0.5); // put LED backlight on 50% |
triochi | 0:b7e471386653 | 46 | |
triochi | 0:b7e471386653 | 47 | // can directly print strings at specified co-ordinates |
triochi | 0:b7e471386653 | 48 | lcd.printString("Hello, World!",0,0); |
triochi | 0:b7e471386653 | 49 | |
triochi | 0:b7e471386653 | 50 | char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) |
triochi | 0:b7e471386653 | 51 | // so can display a string of a maximum 14 characters in length |
triochi | 0:b7e471386653 | 52 | // or create formatted strings - ensure they aren't more than 14 characters long |
triochi | 1:d7d729404013 | 53 | // int temperature = encoder.getVal(); |
triochi | 0:b7e471386653 | 54 | int length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer |
triochi | 0:b7e471386653 | 55 | // it is important the format specifier ensures the length will fit in the buffer |
triochi | 5:0daf19d95cc7 | 56 | // if (length <= 14) // if string will fit on display |
triochi | 5:0daf19d95cc7 | 57 | // lcd.printString(buffer,0,1); // display on screen |
triochi | 0:b7e471386653 | 58 | |
triochi | 0:b7e471386653 | 59 | float pressure = 1012.3; // same idea with floats |
triochi | 0:b7e471386653 | 60 | length = sprintf(buffer,"P = %.2f mb",pressure); |
triochi | 0:b7e471386653 | 61 | if (length <= 14) |
triochi | 0:b7e471386653 | 62 | lcd.printString(buffer,0,2); |
triochi | 0:b7e471386653 | 63 | |
triochi | 0:b7e471386653 | 64 | // can also print individual characters at specified place |
triochi | 0:b7e471386653 | 65 | lcd.printChar('X',5,3); |
triochi | 0:b7e471386653 | 66 | |
triochi | 0:b7e471386653 | 67 | // draw a line across the display at y = 40 pixels (origin top-left) |
triochi | 0:b7e471386653 | 68 | for (int i = 0; i < WIDTH; i++) { |
triochi | 0:b7e471386653 | 69 | lcd.setPixel(i,40); |
triochi | 0:b7e471386653 | 70 | } |
triochi | 0:b7e471386653 | 71 | // need to refresh display after setting pixels |
triochi | 0:b7e471386653 | 72 | lcd.refresh(); |
triochi | 0:b7e471386653 | 73 | |
triochi | 0:b7e471386653 | 74 | // can also check status of pixels using getPixel(x,y) |
triochi | 1:d7d729404013 | 75 | while(1) |
triochi | 1:d7d729404013 | 76 | { |
triochi | 1:d7d729404013 | 77 | wait(.5); |
triochi | 1:d7d729404013 | 78 | |
triochi | 1:d7d729404013 | 79 | length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer |
triochi | 1:d7d729404013 | 80 | |
triochi | 1:d7d729404013 | 81 | if (length <= 14) // if string will fit on display |
triochi | 1:d7d729404013 | 82 | lcd.printString(buffer,0,1); // display on screen |
triochi | 1:d7d729404013 | 83 | } |
triochi | 0:b7e471386653 | 84 | lcd.clear(); |
triochi | 0:b7e471386653 | 85 | |
triochi | 0:b7e471386653 | 86 | } |
triochi | 0:b7e471386653 | 87 | } |