TO CHECKOUT

Dependencies:   mbed

Revision:
0:785c2b62c4d1
Child:
1:6a77fc8e1389
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lab.cpp	Sat Nov 29 17:48:10 2014 +0000
@@ -0,0 +1,303 @@
+/***************************************************************************
+* Titel        : LAB.C
+*
+* Description  : Labormodul "DIBI"
+*
+* Revision List:
+  --------------------------------------------------------------------------
+  Date     | Author          | Change
+  --------------------------------------------------------------------------
+  15.11.13 | J. Altenburg    | Ersterstellung
+  --------------------------------------------------------------------------
+           |                 |
+  --------------------------------------------------------------------------
+****************************************************************************/
+
+/***************************************************************************
+* HEADER-FILES (Only those that are needed in this file)
+****************************************************************************/
+/* Own header */
+#include "lab.h"
+
+/* Foreign headerfiles */
+#include "lcd.h"
+#include "picture.h"
+#include "mbed.h"
+
+DigitalOut      bLed2(LED4);
+DigitalIn       bBtn1(p8);
+AnalogIn        fpAnalog(p15);     /* Analogeingang */
+
+#define PROF
+
+/***************************************************************************
+*   Variablen
+****************************************************************************/
+byte abPicture[160][120];  /*
+Description:  Bildspeicher fuer Grauwertbild
+*/
+
+word awHisto[64] = {0}; /*
+Description : Histogramm-Vektor
+*/
+
+lcd_t myLCD; /*
+* Description : Zugriffstruktur für LCD
+*/
+
+spi_t mySPI; /*
+* Description : dito. fuer SPI
+*/
+
+float fpAdcValue; /*
+* Description : analoger Messwert
+*/
+
+/***************************************************************************
+*   lokale Funktionsprototypen
+***************************************************************************/
+
+
+/***************************************************************************
+*   Defines und Makros                                                       *
+***************************************************************************/
+
+
+/***************************************************************************
+*   lokale Funktionen
+****************************************************************************/
+/* Umrechnen eines Farbwertes in einen Grauwert */
+byte bRGB565ToGray(word wColor){
+    byte bGray = 0;
+    byte r,g,b;
+    r = (wColor & 0xf800)>>11;
+    g = (wColor & 0x07E0) >>5;
+    b = (wColor & 0x001F) ;
+    
+    bGray = ((r+b)*2 + g)/3; 
+         /*
+     .... hier eigenen Code eintragen
+     */
+    return bGray;
+    }
+
+/* Umrechnen eines Grauwert in einen "unbunten" RGB565-Wert */
+word wGrayToRGB565(byte bGray){
+     word wNoColor;
+     byte r,g,b;
+     r = bGray /2;
+     g= bGray;
+     b = bGray / 2;
+     wNoColor = (r <<11) + (g << 5) + b;
+    return wNoColor;
+    }
+
+
+/* Umrechnen des Farbbildes in ein Grauwertbild
+   Parameterübergabe erfolgt über Zeiger
+   */
+void vColorToGray(word *pColor, byte *pGray){
+    word w;
+    for(w = 0; w < 19200; w++){
+        *pGray = bRGB565ToGray(*pColor);
+        pGray++;
+        pColor++;
+        }
+    }
+
+/* Grauwertbild in Sektor 2 plazieren */
+void vGrayToScreen2(byte *pGray){
+    word wColor, w, v;
+    byte x, y;
+    x = y = 0;
+    for(v = 0; v < 19200; v++){
+        wColor = wGrayToRGB565(*pGray);
+        pGray++;
+        lcd_setPixel(&myLCD, 120 + y, 160 + x, wColor);
+        y++;
+        if(y > 119){
+            y = 0;
+            x++;
+            }
+        }
+    }
+
+/* Grauwertbild in Sektor 3 plazieren */
+void vGrayToScreen3(byte *pGray){
+ 
+  word wColor, w, v;
+    byte x, y;
+    x = y = 0;
+    for(v = 0; v < 19200; v++){
+        wColor = wGrayToRGB565(*pGray);
+        pGray++;
+        lcd_setPixel(&myLCD, y, 160 + x, wColor);
+        y++;
+        if(y > 119){
+            y = 0;
+            x++;
+            }
+        }
+}
+
+
+
+/* Histogramm in Sektor 4 zeichnen */
+void vDrawHisto(byte *pGray){
+    word v, y, wMax = 0;
+    byte i, j, n;
+    /* Histogrammvektor initialisieren ... ihre Sache */
+
+    /* Histogrammvektor berechnen      ...  bitte tun */
+    /* Werte normieren                 ...  wenn notwendig */
+    
+    //word awHisto[64] = {0}; /*
+    //byte abPicture[160][120];  /*
+    
+    for(i=0;i<64;i++) awHisto[i] = 0; 
+    
+    
+    for(i=0;i<120;i++){
+        for(j=0;j<160;j++)awHisto[abPicture[i][j]] +=1;
+    }
+    
+    
+    /**ERmittlung des Maximalwertes */
+    wMax = awHisto[0];
+    for(i=1;i<64;i++){
+        if( awHisto[i] > wMax)wMax = awHisto[i];
+    }
+    
+    /* ab hier Bildaufbau */
+    LCD_vFillRectangle(&myLCD, 10, 140, 128, 84, GRAY1);    /* grauer Hintergrund */
+                //LCD_vSetPixel(&myLCD, 200,300, BRIGHT_RED)
+                //                       y   x  
+                
+    for(i = 0; i < 128; i = i + 16){
+        LCD_vDrawVline(&myLCD, 20+i, 140, 84, GRAY2);
+        }
+    for(i = 0; i < 64; i++){                                /* die möglichen Werte darstellen */  
+        n = i*2;
+        y = i;                                              // erzeugt einen Keil 
+        for(j = 0; j <  ( awHisto[i] * 64 / wMax)   ; j++){
+            LCD_vSetPixel(&myLCD, 20+j, 10+n, BRIGHT_RED);
+           // LCD_vSetPixel(&myLCD, 20+j, 11+n, BRIGHT_RED);
+            }
+        }
+    
+        
+   // for(i = 0; i < 128; i = i + 16){
+    //    LCD_vDrawVline(&myLCD, 20+i, 140, 84, GRAY2);
+     //   }
+    for(i = 0; i < 128; i++){
+        LCD_vSetPixel(&myLCD, 84, 10+i, BLUE);
+        }
+    }
+
+
+
+/* Grauwerte manipulieren
+   *pGray zeigt auf die Grauwerte
+   bValue bestimmt die Größenmanipulation
+   */
+void vChangeGray(byte *pGray, byte bValue){
+       byte bGMin, bGMax, wMax;
+    word x,y;
+    //fpAdcValue
+     bGMin = 20;
+     bGMax = 40;
+   //  wMax = 64 * fpAdcValue;
+          wMax = 255 * fpAdcValue;
+
+     
+       byte *ab=  &abPicture[0][0];
+    #if 0
+      for(x = 0; x < 100; x++){
+        if( ( ( *(ab + x) - bGMin) * (wMax/(bGMax+bGMin)) ) >=0) 
+              *(pGray + x) = ( *(ab + x) - bGMin) * (wMax/(bGMax+bGMin));
+              else 
+            *(pGray+x) =0;
+         }
+        
+    #else
+    for(x = 0; x < 100; x++){
+        if( ( ( *(ab + x) - bGMin) * (wMax/(bGMax+bGMin)) ) >=0) 
+              *(ab + x) = ( *(ab + x) - bGMin) * (wMax/(bGMax+bGMin));
+              else 
+            *(ab+x) =0;
+         }
+    #endif
+      /*  for(y=0;y<120;y++)
+            for(x=0;x<160;x++){
+              if( ( (abPicture[y] - bGMin) * (wMax/(bGMax+bGMin)) ) >=0) 
+              pGray[y] = (byte)(abPicture[[x] - bGMin) * (wMax/(bGMax+bGMin));
+              else 
+              pGray[y] =0;
+             
+        }*/
+    
+    
+    
+    /*
+    ...   Sie sind dran
+    */
+    }
+
+
+/***************************************************************************
+*    Initialisierung
+*    angeschlossene Hardware aktivieren
+*    Bildsektoren 1, 2 initialsieren
+*    Histogramm aufbauen
+****************************************************************************/
+void LAB_vInit( void ){
+    spi_init(&mySPI, p11, p12, p13, NC);
+    spi_format(&mySPI, 8, 0, 0);
+    spi_frequency(&mySPI, 25000000);
+    LCD_vInit(&myLCD, &mySPI, p22, p23);
+    LCD_vSetMode(&myLCD, LANDSCAPE);
+    /* Blume in Sektor 1 zeichnen */
+    LCD_vFillSprite(&myLCD, 0, 0, 159, 119, (short int*)&awBlume[0]);
+    /* Grauwerte aus Farbbild ermitteln */
+    vColorToGray((word*)&awBlume[0], &abPicture[0][0]);
+    /* Grauwertbild in Sektor 2 zeichnen */
+    vGrayToScreen2(&abPicture[0][0]);
+    /* Histogramm in Sektor 4 zeichen */
+    vDrawHisto(&abPicture[0][0]);
+    }
+
+
+
+
+/***************************************************************************
+*    MMI-Main
+*    diese Funktion wird alle 100 ms aufgerufen
+****************************************************************************/
+void LAB_vMain( void ){
+    bLed2 = !bLed2;
+   // float bADCDATA;
+    byte i;
+ 
+    /* heir die Anzeige des Analogwertes einbauen */
+    byte abPictureCorrektet[120][160];
+    
+     fpAdcValue = fpAnalog.read();
+     lcd_drawFloat(&myLCD,fpAdcValue*3.3, 3, 20, 125, 1, RED);
+      if(!bBtn1) {
+      
+              vChangeGray(&abPictureCorrektet[0][0], 4);
+              #if 0
+              vGrayToScreen3(&abPictureCorrektet[0][0]);
+                #else
+                              vGrayToScreen3(&abPicture[0][0]);
+
+                #endif
+        }
+   }
+
+
+/***************************************************************************
+* EOF: TEMPLATE.C
+****************************************************************************/
+
+