(DA) Internet of Things and Smart Electronics- ELE3006M2122
/
18689004Coursework
Coursework
main.cpp@7:ce70a873aa70, 2022-01-06 (annotated)
- Committer:
- sesa514652
- Date:
- Thu Jan 06 11:00:39 2022 +0000
- Revision:
- 7:ce70a873aa70
- Parent:
- 6:f7f30e0e3bed
- Child:
- 8:770d168713cc
String working, Addition of class type to code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sesa514652 | 0:1f799c7cce2b | 1 | #include "mbed.h" |
sesa514652 | 0:1f799c7cce2b | 2 | #include "Joystick.h" |
sesa514652 | 0:1f799c7cce2b | 3 | #include "N5110.h" |
sesa514652 | 0:1f799c7cce2b | 4 | #include "hcsr04.h" |
sesa514652 | 3:6ecb75a2675c | 5 | #include "Piezo.h" |
sesa514652 | 7:ce70a873aa70 | 6 | #include<string> |
sesa514652 | 5:9b2c976ca318 | 7 | // y x button |
sesa514652 | 0:1f799c7cce2b | 8 | Joystick joystick(PTB10,PTB11,PTC16); |
sesa514652 | 0:1f799c7cce2b | 9 | HCSR04 sensor(D14, D15); |
sesa514652 | 3:6ecb75a2675c | 10 | Piezo Buzzer(PTC10); |
sesa514652 | 0:1f799c7cce2b | 11 | |
sesa514652 | 5:9b2c976ca318 | 12 | //rows,cols |
sesa514652 | 0:1f799c7cce2b | 13 | int sprite[8][5] = { |
sesa514652 | 0:1f799c7cce2b | 14 | { 0,0,1,0,0 }, |
sesa514652 | 0:1f799c7cce2b | 15 | { 0,1,1,1,0 }, |
sesa514652 | 0:1f799c7cce2b | 16 | { 0,0,1,0,0 }, |
sesa514652 | 0:1f799c7cce2b | 17 | { 0,1,1,1,0 }, |
sesa514652 | 0:1f799c7cce2b | 18 | { 1,1,1,1,1 }, |
sesa514652 | 0:1f799c7cce2b | 19 | { 1,1,1,1,1 }, |
sesa514652 | 0:1f799c7cce2b | 20 | { 1,1,0,1,1 }, |
sesa514652 | 0:1f799c7cce2b | 21 | { 1,1,0,1,1 }, |
sesa514652 | 5:9b2c976ca318 | 22 | }; |
sesa514652 | 6:f7f30e0e3bed | 23 | |
sesa514652 | 4:167ce930c9d5 | 24 | //Test Void after research C++ lanuage 05/01/22 |
sesa514652 | 4:167ce930c9d5 | 25 | int cube(int num){ |
sesa514652 | 4:167ce930c9d5 | 26 | int result; |
sesa514652 | 4:167ce930c9d5 | 27 | result = num*num; |
sesa514652 | 4:167ce930c9d5 | 28 | return result; |
sesa514652 | 5:9b2c976ca318 | 29 | }; |
sesa514652 | 4:167ce930c9d5 | 30 | |
sesa514652 | 5:9b2c976ca318 | 31 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
sesa514652 | 5:9b2c976ca318 | 32 | |
sesa514652 | 0:1f799c7cce2b | 33 | int main() { |
sesa514652 | 5:9b2c976ca318 | 34 | //initialise Joystic |
sesa514652 | 5:9b2c976ca318 | 35 | joystick.init(); |
sesa514652 | 5:9b2c976ca318 | 36 | |
sesa514652 | 5:9b2c976ca318 | 37 | //first need to initialise display |
sesa514652 | 5:9b2c976ca318 | 38 | lcd.init(); |
sesa514652 | 5:9b2c976ca318 | 39 | |
sesa514652 | 5:9b2c976ca318 | 40 | //change set contrast in range 0.0 to 1.0 |
sesa514652 | 5:9b2c976ca318 | 41 | //0.5 appears to be a good starting point |
sesa514652 | 5:9b2c976ca318 | 42 | lcd.setContrast(0.5); |
sesa514652 | 5:9b2c976ca318 | 43 | |
sesa514652 | 5:9b2c976ca318 | 44 | //Buzzer.period(10.0f); |
sesa514652 | 5:9b2c976ca318 | 45 | //Buzzer.pulsewidth(1); |
sesa514652 | 6:f7f30e0e3bed | 46 | //Test Struct after research 05/01/22 |
sesa514652 | 6:f7f30e0e3bed | 47 | struct ObjectDefine{ |
sesa514652 | 7:ce70a873aa70 | 48 | int qwer; |
sesa514652 | 6:f7f30e0e3bed | 49 | float Distance; |
sesa514652 | 7:ce70a873aa70 | 50 | char te; |
sesa514652 | 7:ce70a873aa70 | 51 | double dubs; |
sesa514652 | 7:ce70a873aa70 | 52 | string namestruct; |
sesa514652 | 7:ce70a873aa70 | 53 | string ; |
sesa514652 | 6:f7f30e0e3bed | 54 | }; |
sesa514652 | 7:ce70a873aa70 | 55 | // Test Class 06/01/22 |
sesa514652 | 7:ce70a873aa70 | 56 | class myClass{ |
sesa514652 | 7:ce70a873aa70 | 57 | public: |
sesa514652 | 7:ce70a873aa70 | 58 | string nameclass ; |
sesa514652 | 7:ce70a873aa70 | 59 | double dubss; |
sesa514652 | 7:ce70a873aa70 | 60 | int inty; |
sesa514652 | 7:ce70a873aa70 | 61 | }; |
sesa514652 | 7:ce70a873aa70 | 62 | myClass Class1; |
sesa514652 | 7:ce70a873aa70 | 63 | Class1.nameclass ="firstclass"; |
sesa514652 | 6:f7f30e0e3bed | 64 | ObjectDefine Object1; |
sesa514652 | 7:ce70a873aa70 | 65 | Object1.qwer = 12; |
sesa514652 | 6:f7f30e0e3bed | 66 | Object1.Distance = 34; |
sesa514652 | 7:ce70a873aa70 | 67 | Object1.te = 'A'; |
sesa514652 | 7:ce70a873aa70 | 68 | Object1.dubs = 23; |
sesa514652 | 7:ce70a873aa70 | 69 | Object1.namestruct = "name"; |
sesa514652 | 0:1f799c7cce2b | 70 | while(1) { |
sesa514652 | 5:9b2c976ca318 | 71 | //Tune(Buzzer,So6,8); |
sesa514652 | 5:9b2c976ca318 | 72 | //Stop_tunes(Buzzer); |
sesa514652 | 5:9b2c976ca318 | 73 | Buzzer.play(200,120); |
sesa514652 | 5:9b2c976ca318 | 74 | wait_ms(5); |
sesa514652 | 5:9b2c976ca318 | 75 | Buzzer.play(200,120); |
sesa514652 | 1:757728321abd | 76 | long distanced = sensor.distance(); |
sesa514652 | 1:757728321abd | 77 | if (distanced >= 400 || distanced <= 2) |
sesa514652 | 5:9b2c976ca318 | 78 | { |
sesa514652 | 1:757728321abd | 79 | printf("Out of range"); |
sesa514652 | 5:9b2c976ca318 | 80 | //Calling dummy function 05/01/22 |
sesa514652 | 4:167ce930c9d5 | 81 | int answer = cube(2); |
sesa514652 | 5:9b2c976ca318 | 82 | printf("%f\n",answer); |
sesa514652 | 1:757728321abd | 83 | wait(1.0); |
sesa514652 | 5:9b2c976ca318 | 84 | } |
sesa514652 | 5:9b2c976ca318 | 85 | else |
sesa514652 | 5:9b2c976ca318 | 86 | { |
sesa514652 | 5:9b2c976ca318 | 87 | printf("Distance : %d cm",distanced); |
sesa514652 | 5:9b2c976ca318 | 88 | wait(1.0); // 1 sec |
sesa514652 | 1:757728321abd | 89 | } |
sesa514652 | 5:9b2c976ca318 | 90 | lcd.drawCircle(WIDTH/2,HEIGHT/2,distanced,FILL_BLACK); |
sesa514652 | 5:9b2c976ca318 | 91 | //lcd.refresh must be used to update the lcd Display |
sesa514652 | 5:9b2c976ca318 | 92 | lcd.refresh(); |
sesa514652 | 5:9b2c976ca318 | 93 | wait(5.0); |
sesa514652 | 5:9b2c976ca318 | 94 | //these are default settings so not strictly needed |
sesa514652 | 0:1f799c7cce2b | 95 | lcd.normalMode(); // normal colour mode |
sesa514652 | 5:9b2c976ca318 | 96 | lcd.setBrightness(0.5); // put LED backlight on 50% |
sesa514652 | 0:1f799c7cce2b | 97 | lcd.clear(); |
sesa514652 | 0:1f799c7cce2b | 98 | lcd.printString("Hello, World!",0,0); |
sesa514652 | 0:1f799c7cce2b | 99 | Vector2D coord = joystick.get_coord(); |
sesa514652 | 0:1f799c7cce2b | 100 | lcd.refresh(); |
sesa514652 | 0:1f799c7cce2b | 101 | printf("Coord = %f,%f\n",coord.x,coord.y); |
sesa514652 | 0:1f799c7cce2b | 102 | char buffer[14]; |
sesa514652 | 0:1f799c7cce2b | 103 | Vector2D mapped_coord = joystick.get_mapped_coord(); |
sesa514652 | 0:1f799c7cce2b | 104 | printf("Mapped coord = %f,%f\n",mapped_coord.x,mapped_coord.y); |
sesa514652 | 0:1f799c7cce2b | 105 | |
sesa514652 | 5:9b2c976ca318 | 106 | int length = sprintf(buffer,"T = %2f",coord.x); |
sesa514652 | 5:9b2c976ca318 | 107 | //it is important the format specifier ensures the length will fit in the buffer |
sesa514652 | 0:1f799c7cce2b | 108 | if (length <= 14) // if string will fit on display (assuming printing at x=0) |
sesa514652 | 5:9b2c976ca318 | 109 | lcd.printString(buffer,0,1); // display on screen |
sesa514652 | 0:1f799c7cce2b | 110 | lcd.refresh(); |
sesa514652 | 5:9b2c976ca318 | 111 | lcd.clear(); |
sesa514652 | 5:9b2c976ca318 | 112 | //times by 50 to try get the point moving across screen |
sesa514652 | 0:1f799c7cce2b | 113 | float x50 = mapped_coord.x*5; |
sesa514652 | 0:1f799c7cce2b | 114 | float y50 = mapped_coord.y*8; |
sesa514652 | 5:9b2c976ca318 | 115 | //to see what the values are |
sesa514652 | 5:9b2c976ca318 | 116 | printf(" *50 = %f,%f\n",x50,y50); |
sesa514652 | 5:9b2c976ca318 | 117 | //example of how to draw circles |
sesa514652 | 1:757728321abd | 118 | lcd.printChar('o',WIDTH/2,HEIGHT/2); |
sesa514652 | 5:9b2c976ca318 | 119 | lcd.refresh(); |
sesa514652 | 0:1f799c7cce2b | 120 | float mag = joystick.get_mag(); |
sesa514652 | 0:1f799c7cce2b | 121 | float angle = joystick.get_angle(); |
sesa514652 | 0:1f799c7cce2b | 122 | printf("Mag = %f Angle = %f\n",mag,angle); |
sesa514652 | 0:1f799c7cce2b | 123 | Direction d = joystick.get_direction(); |
sesa514652 | 0:1f799c7cce2b | 124 | printf("Direction = %i\n",d); |
sesa514652 | 0:1f799c7cce2b | 125 | if (joystick.button_pressed() ) { |
sesa514652 | 0:1f799c7cce2b | 126 | printf("Button Pressed\n"); |
sesa514652 | 0:1f799c7cce2b | 127 | } |
sesa514652 | 0:1f799c7cce2b | 128 | } |
sesa514652 | 0:1f799c7cce2b | 129 | } |
sesa514652 | 5:9b2c976ca318 | 130 |