final

Dependencies:   BSP_DISCO_F746NG LCD_DISCO_F746NG TS_DISCO_F746NG

Committer:
abp123
Date:
Thu Oct 25 12:32:13 2018 +0000
Revision:
0:af94a5725e48
final version;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
abp123 0:af94a5725e48 1 //Klasser fra biblioteker
abp123 0:af94a5725e48 2 Thread thread;
abp123 0:af94a5725e48 3 InterruptIn button(USER_BUTTON);
abp123 0:af94a5725e48 4 DigitalOut led(LED1);
abp123 0:af94a5725e48 5 AnalogIn temp(A3);
abp123 0:af94a5725e48 6 AnalogIn lyd(A1);
abp123 0:af94a5725e48 7 AnalogIn LightSensor(A0);
abp123 0:af94a5725e48 8 LCD_DISCO_F746NG lcd;
abp123 0:af94a5725e48 9 TS_DISCO_F746NG ts;
abp123 0:af94a5725e48 10
abp123 0:af94a5725e48 11 //Variabler
abp123 0:af94a5725e48 12 const int R0 = 100000;
abp123 0:af94a5725e48 13 const int B=4275;
abp123 0:af94a5725e48 14
abp123 0:af94a5725e48 15 bool btnPress = true;
abp123 0:af94a5725e48 16
abp123 0:af94a5725e48 17 double temperature;
abp123 0:af94a5725e48 18 double fahrenheit;
abp123 0:af94a5725e48 19
abp123 0:af94a5725e48 20 //Variabler Til display & Info fra user input
abp123 0:af94a5725e48 21 int Bnr;
abp123 0:af94a5725e48 22 int Lnr;
abp123 0:af94a5725e48 23 int inp;
abp123 0:af94a5725e48 24 //variabler til data iteration
abp123 0:af94a5725e48 25 int i;
abp123 0:af94a5725e48 26 //antal er sat til 1, for at tage 1 sæt af gangen
abp123 0:af94a5725e48 27 int antal = 1;
abp123 0:af94a5725e48 28 char cAntal[5];
abp123 0:af94a5725e48 29 //lys
abp123 0:af94a5725e48 30 float LVal;
abp123 0:af94a5725e48 31 //lyd
abp123 0:af94a5725e48 32 int lydstyrke;
abp123 0:af94a5725e48 33 int cPut;
abp123 0:af94a5725e48 34 //til display, unsigned int 8 bit(255 i decimanl)
abp123 0:af94a5725e48 35 uint8_t text[30];
abp123 0:af94a5725e48 36
abp123 0:af94a5725e48 37 //Voids
abp123 0:af94a5725e48 38 //Denne funktion bruger parameter overførsel for at håndtere vores indput fra putty/console
abp123 0:af94a5725e48 39 void Num(char ln[], int M)
abp123 0:af94a5725e48 40 {
abp123 0:af94a5725e48 41 //i2 for at have sin egen itererings variabel, da i, er brugt andre steder til for loops
abp123 0:af94a5725e48 42 int i2 = 0;
abp123 0:af94a5725e48 43 char c;
abp123 0:af94a5725e48 44 //imens i2 variablen er mindre end max værdien og vores input ikke er i begyndelsen af linjen,
abp123 0:af94a5725e48 45 //tilføjes en char til vores array(string)
abp123 0:af94a5725e48 46 while (i2 < M && (c = getchar()) != '\r')
abp123 0:af94a5725e48 47 //Tilføjer alle chars med optælling
abp123 0:af94a5725e48 48 ln[i2++] = c;
abp123 0:af94a5725e48 49 ln[i2] = '\0';
abp123 0:af94a5725e48 50 }
abp123 0:af94a5725e48 51 //Parameter overfører bygnings og lokale nummer til display
abp123 0:af94a5725e48 52 void DisplayVoid(int B, int L)
abp123 0:af94a5725e48 53 {
abp123 0:af94a5725e48 54 lcd.Clear(LCD_COLOR_DARKBLUE);
abp123 0:af94a5725e48 55 lcd.SetBackColor(LCD_COLOR_DARKBLUE);
abp123 0:af94a5725e48 56 lcd.SetTextColor(LCD_COLOR_WHITE);
abp123 0:af94a5725e48 57 sprintf((char*)text, "Bygning: %d", B);
abp123 0:af94a5725e48 58 lcd.DisplayStringAt(0, LINE(1), (uint8_t *)&text, LEFT_MODE);
abp123 0:af94a5725e48 59 sprintf((char*)text, "Lokale: %d", L);
abp123 0:af94a5725e48 60 lcd.DisplayStringAt(0, LINE(2), (uint8_t *)&text, LEFT_MODE);
abp123 0:af94a5725e48 61 }
abp123 0:af94a5725e48 62 //Denne void bruges til Tråd
abp123 0:af94a5725e48 63 void InputThread()
abp123 0:af94a5725e48 64 {
abp123 0:af94a5725e48 65 while (1) {
abp123 0:af94a5725e48 66 wait(0.5);
abp123 0:af94a5725e48 67 //getchar() skal defineres til int for at kunne parses. Chars i c programmering parses som int
abp123 0:af94a5725e48 68 inp = getchar();
abp123 0:af94a5725e48 69 //input skal være lig den definerede værdi og vil ikke returnere noget hvis det er tomt/null
abp123 0:af94a5725e48 70 if (inp == '1' && inp != (int)"\0")
abp123 0:af94a5725e48 71 printf("Temperatur i hex: %x\n", (int)temp.read_u16());
abp123 0:af94a5725e48 72 else if (inp == '2' && inp != (int)"\0")
abp123 0:af94a5725e48 73 printf("Lyd: %d\n", lydstyrke);
abp123 0:af94a5725e48 74 else if (inp == '3' && inp != (int)"\0")
abp123 0:af94a5725e48 75 printf("Lys: %f\n", LVal);
abp123 0:af94a5725e48 76 //returnerer intet, hvis ovenstående statements ikke bliver mødt
abp123 0:af94a5725e48 77 else
abp123 0:af94a5725e48 78 printf("");
abp123 0:af94a5725e48 79 }
abp123 0:af94a5725e48 80 }
abp123 0:af94a5725e48 81
abp123 0:af94a5725e48 82 void TempVoid()
abp123 0:af94a5725e48 83 {
abp123 0:af94a5725e48 84 //Defineres ud i et, uden del-variabler(undtagen konstanter), så værdien bliver dynamisk
abp123 0:af94a5725e48 85 temperature=1.0/(log(R0*(65535.0/((double)temp.read_u16())-1.0)/R0)/B+1/298.15)-273.15;
abp123 0:af94a5725e48 86 fahrenheit = temperature * 1.8 + 32;
abp123 0:af94a5725e48 87 if(btnPress == true) {
abp123 0:af94a5725e48 88 sprintf((char*)text, "Temperatur: %f", temperature);
abp123 0:af94a5725e48 89 lcd.DisplayStringAt(0, LINE(5), (uint8_t *)&text, CENTER_MODE);
abp123 0:af94a5725e48 90 } else if(btnPress == false) {
abp123 0:af94a5725e48 91 sprintf((char*)text, "Fahrenheit: %f", fahrenheit );
abp123 0:af94a5725e48 92 lcd.DisplayStringAt(0, LINE(5), (uint8_t *)&text, CENTER_MODE);
abp123 0:af94a5725e48 93 }
abp123 0:af94a5725e48 94 }
abp123 0:af94a5725e48 95
abp123 0:af94a5725e48 96 void LydVoid()
abp123 0:af94a5725e48 97 {
abp123 0:af94a5725e48 98 lydstyrke = (int)lyd.read_u16();
abp123 0:af94a5725e48 99 if(lydstyrke != 0 && lydstyrke > 25000) {
abp123 0:af94a5725e48 100 sprintf((char*)text, "Sound is too high");
abp123 0:af94a5725e48 101 lcd.DisplayStringAt(0, LINE(6), (uint8_t *)&text, CENTER_MODE);
abp123 0:af94a5725e48 102 wait(2.00);
abp123 0:af94a5725e48 103 } else if(lydstyrke != 0 && lydstyrke <25000) {
abp123 0:af94a5725e48 104 sprintf((char*)text, "Sound level is ok");
abp123 0:af94a5725e48 105 lcd.DisplayStringAt(0, LINE(6), (uint8_t *)&text, CENTER_MODE);
abp123 0:af94a5725e48 106 wait(0.25);
abp123 0:af94a5725e48 107 }
abp123 0:af94a5725e48 108 }
abp123 0:af94a5725e48 109
abp123 0:af94a5725e48 110 void LysVoid()
abp123 0:af94a5725e48 111 {
abp123 0:af94a5725e48 112 LVal = (float)(1023-LightSensor.read())*10/LightSensor.read();
abp123 0:af94a5725e48 113 if(LVal <= 55000) {
abp123 0:af94a5725e48 114 sprintf((char*)text, "It's bright now ");
abp123 0:af94a5725e48 115 lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
abp123 0:af94a5725e48 116 } else if(LVal > 55000) {
abp123 0:af94a5725e48 117 sprintf((char*)text, "It's getting dark ");
abp123 0:af94a5725e48 118 lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
abp123 0:af94a5725e48 119 }
abp123 0:af94a5725e48 120 }
abp123 0:af94a5725e48 121 //Til user button
abp123 0:af94a5725e48 122 void pressed()
abp123 0:af94a5725e48 123 {
abp123 0:af94a5725e48 124 btnPress = false;
abp123 0:af94a5725e48 125 }
abp123 0:af94a5725e48 126 void pressed2()
abp123 0:af94a5725e48 127 {
abp123 0:af94a5725e48 128 btnPress = true;
abp123 0:af94a5725e48 129 }