// UTS 2000 - Fichier debug_'RS232.cpp' Affiche l'écran de debug avec la carte.
// V3.6c - Le 01/09/2015
/*****************************************************************************/
/* DEBUG BOARD SCREEN                                                        */
/*****************************************************************************/
#include  <windows.h>
#include  <string.h>
#include  <stdlib.h>
#include  <stdio.h>
#include "Train_UTS.h"
#include  "serial_port.h"

#define _CRT_SECURE_NO_WARNINGS  // Limite les messages de warning dans Visual C++

extern int can_to_cons(int, int);
extern void EasyTextOut(int , int , char* );
extern void EasyLine(int, int, int, int);
extern cSerial serial;
extern cSerialPort serialPort;
extern int x_mouse,y_mouse,touche,touche_char,x_mouse_move,y_mouse_move, screen_mode, screen_mode_retour;

extern HBRUSH  bmp_pinceau_blanc, bmp_pinceau_gris192, bmp_pinceau_vert, bmp_pinceau_jaune, bmp_pinceau_rouge, bmp_pinceau_bleu, bmp_pinceau_cyan, bmp_pinceau_jaune_clair;
extern HDC hMemDC;
extern HFONT     bmp_fonte;
extern HPEN      bmp_stylo_noir, bmp_stylo_gris128, bmp_stylo_gris192, bmp_stylo_blanc, bmp_stylo_jaune ;
extern HPEN      bmp_stylo_jaune_clair;

int rs232_debug_link[NB_CARTE][NB_CANTON]={{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,},{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,},{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,},{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,}};
int cfg_canton_inv[NB_CARTE][NB_CANTON]={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}; // =0 ou =1 pour inverser le sens d'alimentation d'un canton.

extern int vio_det[NB_CARTE][NB_CANTON];
extern int vio_canton_vit[NB_CARTE][NB_CANTON];      //UU: n'en garder qu'un et mettre en debug total
extern int cfg_canton_inv[NB_CARTE][NB_CANTON];
int serial_debug_canton_vit[NB_CARTE][NB_CANTON];			// 0-255, valeurs manuelles de rempalcement dans l'écran RSR232 pour les sorties vers les cantons
int serial_debug_select_canton_vit[NB_CARTE][NB_CANTON];	// 0-1, Utilisé dans l'écran RS232,si = 1 les valeurs de remplacement les valeurs pour les sorties vers les cantons.

extern int vio_in[NB_CARTE][NB_IN];

extern int vio_out[NB_CARTE][NB_OUT];
extern int serial_debug_out[NB_CARTE][NB_OUT];
extern int serial_debug_select_out[NB_CARTE][NB_OUT];

extern int vio_led[NB_CARTE][NB_LED];
int serial_debug_led[NB_CARTE][NB_LED];			// 0-1, valeurs manuelles saisies dans l'écran RSR232 (on/off).
extern int vio_led_cli[NB_CARTE][NB_LED];
extern int vio_led_phase[NB_CARTE][NB_LED];
int serial_debug_led_cli[NB_CARTE][NB_LED];		// 0-1, valeurs manuelles saisies dans l'écran RSR232 (cli ou non).
int serial_debug_led_phase[NB_CARTE][NB_LED];	// 0-1, valeurs manuelles saisies dans l'écran RSR232 (opp phase ou non).
int serial_debug_select_led[NB_CARTE][NB_LED];	// 0-1, Utilisé dans l'écran RS232,si = 1 les valeurs saisies remplacent les valeurs pour les leds.

extern int vio_can[NB_CARTE][NB_CAN];
extern int cfg_can_inv[NB_CARTE][NB_CAN];

extern int vio_srv[NB_CARTE][NB_SERVO];
int serial_debug_srv[NB_CARTE][NB_SERVO];			// 0-255, valeurs manuelles saisies dans l'écran RSR232, pour les 5 servos-moteur.
int serial_debug_select_srv[NB_CARTE][NB_SERVO];	// 0-1, Utilisé dans l'écran RS232,si = 1 les valeurs saisies remplacent les valeurs pour les 5 servos-moteur.

extern int serial_error; //autres
extern int serial_tab_cmd[4][5];
extern int serial_tab_adresse[4][5];
extern int serial_tab_data[4][5];
extern int tab_auto_read[4][4];

extern int timer_prg;
extern int debug_tic_nb;
extern int cfg_show_cards_found;
extern int touche_char;
extern int cfg_enable_leds[NB_CARTE];
extern int connecte;								// 1 = Une carte au moins de connectée.

#ifndef VERSION_UTS_V3 // Ne pas toucher à cette ligne !!!
extern int cfg_enable_servos[NB_CARTE][NB_SERVO];
#endif

int selected_card=0;
int selected_can_and_card=-1;

int atoh(char ch[])
{
    int i=0;
    int val=0;
    while(ch[i])
    {
        val=val<<4;
        if(ch[i]>='0' && ch[i]<='9') val=val+ch[i]-'0';
        if(ch[i]>='a' && ch[i]<='f') val=val+10+ch[i]-'a';
        if(ch[i]>='A' && ch[i]<='F') val=val+10+ch[i]-'A';
        i++;
    }
    return val;
}

void serial_debug(int action)
{
    int i,j,k;
    static int redraw=0;
    int val;
    char ch[200];
    static int cpt=0;
    int x,y;
    static char tab_cmd_ch[4]="000";
    static int tab_cmd_colone=-1;
    static int tab_cmd_ligne=0;
    static int srv_debug_cmd=-1;
    static int selected_pot=-1;
	if(action==DEBUG_TICK) { //occured even when debug screen is not selected
        for(j=0;j<NB_CARTE;j++) for(i=0;i<NB_CANTON;i++) if(serial_debug_select_canton_vit[j][i]) if ((val=rs232_debug_link[j][i])!=-1) {
            serial_debug_canton_vit[j][i]=can_to_cons(vio_can[val/100][val%100],0);
        }
        return; // return because other part is only executed when debug screen is selected
    }
    cpt++;
    if(cpt==40) {cpt=0;redraw=1;}
	// Pour rendre actif/inactif l'activation des Leds en sortie de carte en cliquant sur : Leds [On/Off] ou en tapant sur la touche 'L'.
    if(action==DEBUG_KEY) {
        if(touche_char=='l' || touche_char=='L') { redraw=1; if(cfg_enable_leds[selected_card])      cfg_enable_leds[selected_card]=0;      else cfg_enable_leds[selected_card]=1;      }
		#ifndef VERSION_UTS_V3 // Ne pas toucher à cette ligne !!!
        if(touche_char=='g' || touche_char=='G') { redraw=1; if(cfg_enable_servos[selected_card][0]) cfg_enable_servos[selected_card][0]=0; else cfg_enable_servos[selected_card][0]=1; }
        if(touche_char=='h' || touche_char=='H') { redraw=1; if(cfg_enable_servos[selected_card][1]) cfg_enable_servos[selected_card][1]=0; else cfg_enable_servos[selected_card][1]=1; }
		#endif
    }

	////////////
    SelectObject(hMemDC,bmp_fonte);
    SetBkMode(hMemDC,TRANSPARENT);
    SetTextColor(hMemDC,RGB(255,0,0));
    //if(action==DEBUG_TOTAL_PAINT || redraw) { // Test supprimé, il faut à chaque fois tout redéssiner pour le double buffer
        redraw=0;
        SelectObject(hMemDC,bmp_pinceau_jaune); SelectObject(hMemDC,bmp_stylo_gris192);
        Rectangle(hMemDC,0,0,800,700);
		SelectObject(hMemDC,bmp_pinceau_gris192); SelectObject(hMemDC,bmp_stylo_gris192); Rectangle(hMemDC,800-15,15+30,800-2,2+30); // Rectangle gris pour fermer la fenêtre.
		SelectObject(hMemDC,bmp_stylo_noir); EasyLine(800-14,3+30,800-3,14+30); EasyLine(800-14,13+30,800-3,2+30); // Croix noire.
		SelectObject(hMemDC,bmp_stylo_noir);  EasyLine(800-2,1+30,800-2,16+30); EasyLine(800-16,15+30,800-1,15+30); // Trait noir.
		SelectObject(hMemDC,bmp_stylo_gris128); EasyLine(800-3,2+30,800-3,15+30); EasyLine(800-15,14+30,800-2,14+30); // Trait gris foncé.
		SelectObject(hMemDC,bmp_pinceau_blanc); SelectObject(hMemDC,bmp_stylo_gris192);
    //}
    // Affiche l'état des cartes, mais si aucune caret n'est présente, la carte 0 est considérée comme active.
    SelectObject(hMemDC,bmp_stylo_noir); SetTextColor(hMemDC,RGB(0,0,0));
    y=30; x=320;
	EasyTextOut(x-2,y+20,"Rouge=Présente, Vert=Séléctionnée");
	for (i = 0; i < NB_CARTE; i++) {
		SelectObject(hMemDC, bmp_pinceau_gris192);
		j = 0;
		if (connecte != 0) {  // V3.6c on vérifie que l'on est connecté à au moins une carte UTS.
			if (i == 0) if (serialPort.card0_ok) { SelectObject(hMemDC, bmp_pinceau_rouge); j = 1; }
			if (i == 1) if (serialPort.card1_ok) { SelectObject(hMemDC, bmp_pinceau_rouge); j = 1; }
			if (i == 2) if (serialPort.card2_ok) { SelectObject(hMemDC, bmp_pinceau_rouge); j = 1; }
			if (i == 3) if (serialPort.card3_ok) { SelectObject(hMemDC, bmp_pinceau_rouge); j = 1; }
			if (i == selected_card)             { SelectObject(hMemDC, bmp_pinceau_vert); j = 1; }
		}

        Rectangle(hMemDC,x+70*i,y,x+70*i+54,y+17);
		if(j==0) {EasyLine(x+70*i,y,x+70*i+54,y+16);EasyLine(x+70*i,y+16,x+70*i+54,y);} // Pas de carte UTS = Rectange barré
        sprintf_s(ch,"carte%d",i); EasyTextOut(x+70*i+3,y+2,ch);
    }
    if(action==DEBUG_MOUSE) {
		if ((x_mouse > 800-15 && x_mouse < 800-2) && (y_mouse > 2+30 && y_mouse < 15+30)) {
			screen_mode= screen_mode_retour; 
			return;	// On ferme la fenêtre.
		}
        for(i=0;i<=NB_CARTE;i++)
        if( (i==0 && serialPort.card0_ok) || (i==1 && serialPort.card1_ok) || (i==2 && serialPort.card2_ok) || (i==3 && serialPort.card3_ok) )
			if(x_mouse>(x+70*i) && x_mouse<(x+70*i+54)) if(y_mouse>y && y_mouse<(y+17)) {selected_card=i;redraw=1;}
    }
    // Affichage des sorties.
    SelectObject(hMemDC,bmp_stylo_noir);
    SetTextColor(hMemDC,RGB(0,0,0));
	EasyTextOut(318,72,"[X]=UTS, [ ]=Manuel, changer de mode par un clic-droit");
	EasyTextOut(320,84,"Sorties en Vert =allumée, en Rouge =éteinte");

	sprintf_s(ch,"%d",timer_prg+15);
	EasyTextOut(545,06,"Durée du Timer 'tic':     msec"); EasyTextOut(720,06,ch);
	//EasyTextOut(670,32,"(QUITTER = ESC)");
	EasyTextOut(800-140,32,"(QUITTER = ESC)");
	y=100;
    for(j=0;j<=NB_CARTE;j++) for(i=0;i<8;i++) {
        if(j>=0 && j<=2) x=5+155*(j+2)+18*i;
        if(j>=3 && j<=4) { x=5+155*(j-3)+18*i; if(cfg_enable_leds[selected_card]) continue; }
        if(      serial_debug_select_out[selected_card][8*j+i]==0 && vio_out[selected_card][8*j+i]         ) SelectObject(hMemDC,bmp_pinceau_vert);
        else if( serial_debug_select_out[selected_card][8*j+i]    && serial_debug_out[selected_card][8*j+i]) SelectObject(hMemDC,bmp_pinceau_vert);
        else                                                                                                 SelectObject(hMemDC,bmp_pinceau_rouge);
        Rectangle(hMemDC,x,y,x+17,y+17);
        if(serial_debug_select_out[selected_card][8*j+i]==0) {		// Affiche un rectangle barré.
            EasyLine(x   ,y,x+17,y+17);
            EasyLine(x+16,y,x-1 ,y+17);
        }
        sprintf_s(ch,"%d",8*j+i);
        if((8*j+i)<10) EasyTextOut(x+4,y+2,ch); else EasyTextOut(x+0,y+2,ch); // Affiche les rectangles de sorties (0-39).
    }
    if(action==DEBUG_MOUSE) {
        for(j=0;j<=NB_CARTE;j++) for(i=0;i<8;i++) {
            if(j>=0 && j<=2) x=5+155*(j+2)+18*i;
            if(j>=3 && j<=4) x=5+155*(j-3)+18*i;
            if(x_mouse>x && x_mouse<(x+17) && y_mouse>y && y_mouse<(y+17))
                if(serial_debug_select_out[selected_card][8*j+i])
                    serial_debug_out[selected_card][8*j+i]^=1;
        }
    }
    if(action==DEBUG_MOUSE_RIGHT) {
        for(j=0;j<=NB_CARTE;j++) for(i=0;i<8;i++) {
            if(j>=0 && j<=2) x=5+155*(j+2)+18*i;
            if(j>=3 && j<=4) x=5+155*(j-3)+18*i;
            if(x_mouse>x && x_mouse<(x+17) && y_mouse>y && y_mouse<(y+17)) {
                serial_debug_select_out[selected_card][8*j+i]^=1;
                if(serial_debug_select_out[selected_card][8*j+i]) serial_debug_out[selected_card][8*j+i]=vio_out[selected_card][8*j+i];
            }
        }
    }
// Affichage des nouvelles sorties en V3.3 (sortie n° 40 à n° 79)  Affiché sous les sorties n°1 à n°39
    SelectObject(hMemDC,bmp_stylo_noir);
    SetTextColor(hMemDC,RGB(0,0,0));
    y=120;
    for(j=0;j<=NB_CARTE;j++) for(i=0;i<8;i++) {
        if(j>=0 && j<=4) x=5+155*(j)+18*i;
        if(      serial_debug_select_out[selected_card][8*j+i+40]==0 && vio_out[selected_card][8*j+i+40]         ) SelectObject(hMemDC,bmp_pinceau_vert);
        else if( serial_debug_select_out[selected_card][8*j+i+40]    && serial_debug_out[selected_card][8*j+i+40]) SelectObject(hMemDC,bmp_pinceau_vert);
        else                                                                                                       SelectObject(hMemDC,bmp_pinceau_rouge);
        Rectangle(hMemDC,x,y,x+17,y+17);
        if(serial_debug_select_out[selected_card][8*j+i+40]==0) {		// Affiche un rectangle barré.
            EasyLine(x   ,y,x+17,y+17);
            EasyLine(x+16,y,x-1 ,y+17);
        }
        sprintf_s(ch,"%d",8*j+i+40);
		EasyTextOut(x+0,y+2,ch);		// Affiche les rectangles de sorties (40-79).
    }
    if(action==DEBUG_MOUSE) {
        for(j=0;j<=4;j++) for(i=0;i<8;i++) {
            if(j>=0 && j<=4) x=5+155*(j)+18*i;
            if(x_mouse>x && x_mouse<(x+17) && y_mouse>y && y_mouse<(y+17))
                if(serial_debug_select_out[selected_card][8*j+i+40]) serial_debug_out[selected_card][8*j+i+40]^=1;
        }
    }
    if(action==DEBUG_MOUSE_RIGHT) {
        for(j=0;j<=4;j++) for(i=0;i<8;i++) {
            if(j>=0 && j<=4) x=5+155*(j)+18*i;
            if(x_mouse>x && x_mouse<(x+17) && y_mouse>y && y_mouse<(y+17)) {
                serial_debug_select_out[selected_card][8*j+i+40]^=1;
                if(serial_debug_select_out[selected_card][8*j+i+40]) serial_debug_out[selected_card][8*j+i+40]=vio_out[selected_card][8*j+i+40];
            }
        }
    }

// Affichage des leds
    if(cfg_enable_leds[selected_card]) {
        SelectObject(hMemDC,bmp_stylo_noir);
        SetTextColor(hMemDC,RGB(0,0,0));
		EasyTextOut(5,06,"Sorties LEDS multipléxées");
		EasyTextOut(5,18,"Rouge= OFF, Vert= ON, Jaune= Clignote");
		EasyTextOut(5,30,"Bleu= Clignote en opposition au jaune");
        for(j=0;j<NB_CARTE;j++) for(i=0;i<12;i++) {
            if(i<8) x=5+18*i;
            else    x=5+155+18*(i-8);
            y=100-18*(3-j);
			if(serial_debug_select_led[selected_card][12*j+i]==0) {		//normal
                if(vio_led[selected_card][12*j+i]==0) SelectObject(hMemDC,bmp_pinceau_rouge);
                else {
                    if(vio_led_cli[selected_card][12*j+i]==0) SelectObject(hMemDC,bmp_pinceau_vert);
                    else {
                        if(vio_led_phase[selected_card][12*j+i]==0) SelectObject(hMemDC,bmp_pinceau_jaune);
                        else                                        SelectObject(hMemDC,bmp_pinceau_bleu);
                    }
                }
                Rectangle(hMemDC,x,y,x+17,y+17);		// Affiche un rectangle barré.
                EasyLine(x   ,y,x+17,y+17);
                EasyLine(x+16,y,x-1 ,y+17);
            }
			if(serial_debug_select_led[selected_card][12*j+i]==1) {		//debug
                if(serial_debug_led[selected_card][12*j+i]==0) SelectObject(hMemDC,bmp_pinceau_rouge);
                else {
                    if(serial_debug_led_cli[selected_card][12*j+i]==0) SelectObject(hMemDC,bmp_pinceau_vert);
                    else {
                        if(serial_debug_led_phase[selected_card][12*j+i]==0) SelectObject(hMemDC,bmp_pinceau_jaune);
                        else                                                 SelectObject(hMemDC,bmp_pinceau_bleu);
                    }
                }
                Rectangle(hMemDC,x,y,x+17,y+17);
            }
			// Affiche les n° de leds dans un rectangle de couleur.
            sprintf_s(ch,"%d",12*j+i);
	        if(strlen(ch)==1) EasyTextOut(x+4,y+2,ch); else EasyTextOut(x,y+2,ch);
		}

		SelectObject(hMemDC,bmp_pinceau_gris192);
        for(i=0;i<4;i++) {x=5+155+18*(4+i);Rectangle(hMemDC,x,y,x+17,y+17);}

        if(action==DEBUG_MOUSE) {
            for(j=0;j<=NB_CARTE;j++) for(i=0;i<12;i++) {
                if(i<8) x=5+18*i;
                else    x=5+155+18*(i-8);
                y=100-18*(3-j);
                if(x_mouse>x && x_mouse<(x+17) && y_mouse>y && y_mouse<(y+17))
                if(serial_debug_select_led[12*j+i]) {
                    if(serial_debug_led[selected_card][12*j+i]==0) {
                        serial_debug_led[selected_card][12*j+i]=1;
                        serial_debug_led_cli[selected_card][12*j+i]=0;
                        serial_debug_led_phase[selected_card][12*j+i]=0;
                    }
                    else if(serial_debug_led_cli[selected_card][12*j+i]==0) {
                        serial_debug_led_cli[selected_card][12*j+i]=1;
                        serial_debug_led_phase[selected_card][12*j+i]=0;
                    }
                    else if(serial_debug_led_phase[selected_card][12*j+i]==0) {
                        serial_debug_led_phase[selected_card][12*j+i]=1;
                    }
                    else {
                        serial_debug_led[selected_card][12*j+i]=0;
                        serial_debug_led_cli[selected_card][12*j+i]=0;
                        serial_debug_led_phase[selected_card][12*j+i]=0;
                    }
                }
            }
        }

        if(action==DEBUG_MOUSE_RIGHT) {
            for(j=0;j<=NB_CARTE;j++) for(i=0;i<12;i++) {
                if(i<8) x=5+18*i;
                else    x=5+155+18*(i-8);
                y=100-18*(3-j);
                if(x_mouse>x && x_mouse<(x+17) && y_mouse>y && y_mouse<(y+17)) {
                    serial_debug_select_led[selected_card][12*j+i]^=1;
                    if(serial_debug_select_led[selected_card][12*j+i]) {
                        serial_debug_led[selected_card][12*j+i]=vio_led[selected_card][12*j+i];
                        serial_debug_led_cli[selected_card][12*j+i]=vio_led_cli[selected_card][12*j+i];
                        serial_debug_led_phase[selected_card][12*j+i]=vio_led_phase[selected_card][12*j+i];
                    }
                }
            }
        }
    }

    // Affiche les entrées CAN.
    SelectObject(hMemDC,bmp_stylo_noir);
    SetTextColor(hMemDC,RGB(0,0,0));
	EasyTextOut(210,255+18,"Entrées  can");
    for(i=0;i<5;i++) {
        x=210+20*i;
        val=vio_can[selected_card][i];
        SelectObject(hMemDC,bmp_pinceau_gris192);
        Rectangle(hMemDC,x,290,x+17,290+255-val);
        SelectObject(hMemDC,bmp_pinceau_cyan);
        Rectangle(hMemDC,x,290+255,x+17,290+255-val);
        sprintf_s(ch,"%d",i);EasyTextOut(x+5,255,ch);
        if(val>=100) {sprintf_s(ch,"%d",val/100);EasyTextOut(x+5,500,ch);}
        if(val>=10 ) {sprintf_s(ch,"%d",(val/10)%10);EasyTextOut(x+5,515,ch);}
        sprintf_s(ch,"%d",val%10);EasyTextOut(x+5,530,ch);
		// Affiche les rectangles [D] vert ou [I] rouge en bas des entrées CAN. Entrée Directe ou Inversée.
        y=560;
        if(cfg_can_inv[selected_card][i]==0) { SelectObject(hMemDC,bmp_pinceau_vert);  sprintf_s(ch,"D"); }
        else                                 { SelectObject(hMemDC,bmp_pinceau_rouge); sprintf_s(ch,"I"); }
        Rectangle(hMemDC,x,y,x+17,y+17); EasyTextOut(x+4,y+2,ch);
    }
	// Affiche les rectangles [L] vert ou [1] rouge tout en bas des entrées CAN. Permet d'affecter une entrée CAN à la commande d'un canton.
    for(i=0;i<5;i++) {
        x=210+20*i;
        y=580;
        if(selected_can_and_card==100*selected_card+i) { SelectObject(hMemDC,bmp_pinceau_vert);  sprintf_s(ch,"L"); }
        else                                           { SelectObject(hMemDC,bmp_pinceau_rouge); sprintf_s(ch,"l"); }
        Rectangle(hMemDC,x,y,x+17,y+17); EasyTextOut(x+4,y+2,ch);
    }

    if(action==DEBUG_MOUSE){
        for(i=0;i<5;i++) {
            x=210+20*i; y=560;
            if(x_mouse>x && x_mouse<(x+17) && y_mouse>=y && y_mouse<=(y+17)){
                cfg_can_inv[selected_card][i]^=1;
            }
            x=210+20*i; y=580;
            if(x_mouse>x && x_mouse<(x+17) && y_mouse>=y && y_mouse<=(y+17)){
                if(selected_can_and_card==(100*selected_card+i)) {
                    //for(j=0;j<4;j++) for(k=0;k<16;k++) if(rs232_debug_link[j][k]==(100*selected_card+i)) rs232_debug_link[j][k]=-1;
                    selected_can_and_card=-1;
                }
                else{
                    selected_can_and_card=100*selected_card+i;
                }
            }
        }
    }
    // Affiche les détections de canton & pot
    SelectObject(hMemDC,bmp_stylo_noir);
    SetTextColor(hMemDC,RGB(0,0,0));
    for(j=0;j<NB_CARTE;j++) for(i=0;i<4;i++) {
        x=620-100*j+60+20;
		// Affiche des rectangles verts ou rouges si le canton est occupés.
        if(vio_det[selected_card][4*j+i]) SelectObject(hMemDC,bmp_pinceau_vert);
        else                              SelectObject(hMemDC,bmp_pinceau_rouge);
        Rectangle(hMemDC,x-20*i,255,x-20*i+17,255+17);
        sprintf_s(ch,"%d",4*j+i);
        if((4*j+i)<10) EasyTextOut(x-20*i+4,257,ch);		// Affiche le n° du canton au dessus.
        else           EasyTextOut(x-20*i+0,257,ch);
        SelectObject(hMemDC,bmp_pinceau_gris192);
        Rectangle(hMemDC,x-20*i,290,x-20*i+17,290+255);
		val=0;
        if(serial_debug_select_canton_vit[selected_card][4*j+i]) val=serial_debug_canton_vit[selected_card][4*j+i];
        else val=vio_canton_vit[selected_card][4*j+i];
        SelectObject(hMemDC,bmp_pinceau_rouge);
        if(selected_pot==(4*j+i)) if(serial_debug_select_canton_vit[selected_card][4*j+i]) SelectObject(hMemDC,bmp_pinceau_vert);
        Rectangle(hMemDC,x-20*i,290+128-2*val-2,x-20*i+17,290+128-2*val+2);
        if(val<0) {EasyTextOut(x-20*i+5,500,"-"); val=-val;}
        else       EasyTextOut(x-20*i+5,500,"+");
        if(val>=10 ) {sprintf_s(ch,"%d",val/10);EasyTextOut(x-20*i+5,515,ch);}
        sprintf_s(ch,"%d",val%10);EasyTextOut(x-20*i+5,530,ch);
        if(serial_debug_select_canton_vit[selected_card][4*j+i]==0) {
                EasyLine(x-20*i,290   ,x-20*i+16,290+20);
                EasyLine(x-20*i,290+20,x-20*i+16,290   );
        }
		// Affiche les rectangles [D] vert ou [I] rouge en bas des cantons.Commande Directe ou Inverse des cantons (sens de parcours suivant alimentation).
		y=560;
        if(cfg_canton_inv[selected_card][4*j+i]==0) { SelectObject(hMemDC,bmp_pinceau_vert);  sprintf_s(ch,"D"); }
        else                                        { SelectObject(hMemDC,bmp_pinceau_rouge); sprintf_s(ch,"I"); }
        Rectangle(hMemDC,x-20*i,y,x-20*i+17,y+17); EasyTextOut(x-20*i+4,y+2,ch);
        y=580;
        if(serial_debug_select_canton_vit[selected_card][4*j+i]==0) rs232_debug_link[selected_card][4*j+i]=-1;
        val=rs232_debug_link[selected_card][4*j+i];
		// Affiche les rectangles [1] ou [0] en rouge tout en bas des cantons. [0] à [4] pour le n° de l'entrée CAN choisie.
        if(val!=-1) { SelectObject(hMemDC,bmp_pinceau_vert);  sprintf_s(ch,"%d%d",val/100,val%100); }
        else        { SelectObject(hMemDC,bmp_pinceau_rouge); sprintf_s(ch,"l"); }
        Rectangle(hMemDC,x-20*i,y,x-20*i+17,y+17);
        if(strlen(ch)==1) EasyTextOut(x-20*i+4,y+2,ch); else EasyTextOut(x-20*i,y+2,ch);
    }
	SetTextColor(hMemDC,RGB(0,0,0));
	EasyTextOut(718,243,"Vert=");
	EasyTextOut(718,253," Occupé");
	EasyTextOut(718,263,"Rouge=");
	EasyTextOut(718,273," Libre");

	EasyTextOut(718,290,"[X]=UTS");
	EasyTextOut(718,300,"[ ]=Man-");
	EasyTextOut(718,310,"    uel");

	EasyTextOut(718,350,"Manuel +");
	EasyTextOut(718,360,"Sélectio");
	EasyTextOut(718,370,"curseur+");
	EasyTextOut(718,390,"'*' Plus");
	EasyTextOut(718,400,"'+' plus");
	EasyTextOut(718,410,"'0' <<0>>");
	EasyTextOut(718,420,"'-' moins");
	EasyTextOut(718,430,"'/' Moins");

	EasyTextOut(718,450,"Attention");
	EasyTextOut(718,460,"un train");
	EasyTextOut(718,470,"est peut");
	EasyTextOut(718,480,"être en");
    EasyTextOut(718,490,"mode ");
	EasyTextOut(718,500,"sélectif");

	EasyTextOut(718,556,"[D]irect");
	EasyTextOut(718,566,"[I]nverse");

    EasyTextOut(210,600,"Pour associer un potentiomètre à un canton, dé-sélectionner un canton [X]");
	EasyTextOut(210,612,"devient [ ], sélectionner un potentiomètre [1] devient [L]");
	EasyTextOut(210,624,"Sélectionner le canton [1] devient [00]");

    SelectObject(hMemDC,bmp_pinceau_jaune);     //erase outside cursors
    SelectObject(hMemDC,bmp_stylo_jaune);
    Rectangle(hMemDC,230,290-1-3,717,290-1);
    Rectangle(hMemDC,230,290+255+1,717,290+255+1+5);
    if(action==DEBUG_MOUSE)
    {
        selected_pot=-1;
        for(j=0;j<NB_CARTE;j++) for(i=0;i<4;i++)
        {
            x=620-100*j+60+20;
            if(serial_debug_select_canton_vit[selected_card][4*j+i])
            if(x_mouse>(x-20*i) && x_mouse<(x-20*i+17) && y_mouse>=290 && y_mouse<=(290+255))
            {
                if(serial_debug_select_canton_vit[selected_card][4*j+i])
                    serial_debug_canton_vit[selected_card][4*j+i]=-(y_mouse-(290+128))/2;
                selected_pot=4*j+i;
            }
            if(x_mouse>(x-20*i) && x_mouse<(x-20*i+17) && y_mouse>=560 && y_mouse<=(560+17))
            {
                cfg_canton_inv[selected_card][4*j+i]^=1;
            }
            if(x_mouse>(x-20*i) && x_mouse<(x-20*i+17) && y_mouse>=580 && y_mouse<=(580+17))
            {
                if(rs232_debug_link[selected_card][4*j+i]!=-1) rs232_debug_link[selected_card][4*j+i]=-1;
                else if(selected_can_and_card!=-1)
                {
                    rs232_debug_link[selected_card][4*j+i]=selected_can_and_card;
                }
            }
        }
    }
    if(action==DEBUG_MOUSE_RIGHT)
    {
        for(j=0;j<4;j++) for(i=0;i<4;i++)
        {
            x=620-100*j+60+20;
            if(x_mouse>(x-20*i) && x_mouse<(x-20*i+17) && y_mouse>290 && y_mouse<(290+255))
            {
                serial_debug_select_canton_vit[selected_card][4*j+i]^=1;
                if(serial_debug_select_canton_vit[selected_card][4*j+i]==1) serial_debug_canton_vit[selected_card][4*j+i]=vio_canton_vit[selected_card][4*j+i];
            }
        }
    }
    if(action==DEBUG_KEY) if(selected_pot!=-1)
    {
        if(touche_char=='*') serial_debug_canton_vit[selected_card][selected_pot]+=10;
		if(touche_char=='+') serial_debug_canton_vit[selected_card][selected_pot]++;
        if(touche_char=='0') serial_debug_canton_vit[selected_card][selected_pot]=0;
		if(touche_char=='-') serial_debug_canton_vit[selected_card][selected_pot]--;
        if(touche_char=='/') serial_debug_canton_vit[selected_card][selected_pot]-=10;
        if(serial_debug_canton_vit[selected_card][selected_pot]>64 ) serial_debug_canton_vit[selected_card][selected_pot]=64;
        if(serial_debug_canton_vit[selected_card][selected_pot]<-64) serial_debug_canton_vit[selected_card][selected_pot]=-64;
    }
    // Affichage des entrées multiplexées

    SelectObject(hMemDC,bmp_stylo_noir);
    SetTextColor(hMemDC,RGB(0,0,0));
    for(j=0;j<9;j++) for(i=0;i<4;i++)
    {
        x=-32+55+20*i;
        y=290+20*j;
        if(vio_in[selected_card][4*j+i]) SelectObject(hMemDC,bmp_pinceau_vert);
        else                             SelectObject(hMemDC,bmp_pinceau_rouge);
        Rectangle(hMemDC,x,y,x+17,y+17);
        sprintf_s(ch,"%d",4*j+i);
        if((4*j+i)<10) EasyTextOut(x+4,y+2,ch); else EasyTextOut(x+0,y+2,ch);
    }
    x=8;
    y=290;
    EasyTextOut(x,y    ,"A");
    EasyTextOut(x,y+ 20,"B");
    EasyTextOut(x,y+ 40,"C");
    EasyTextOut(x,y+ 60,"D");
    EasyTextOut(x,y+ 80,"E");
    EasyTextOut(x,y+100,"F");
    EasyTextOut(x,y+120,"G");
    EasyTextOut(x,y+140,"H");
    EasyTextOut(x,y+160,"I");
    EasyTextOut( 60-30,255,"0");
    EasyTextOut( 80-30,255,"1");
    EasyTextOut(100-30,255,"2");
    EasyTextOut(120-30,255,"3");
    EasyTextOut(x,y-18,"Entrées mux");

// Affichage des entrées supplémentaire n°40 à n°79 en V3.3

	SelectObject(hMemDC,bmp_stylo_noir);
    SetTextColor(hMemDC,RGB(0,0,0));
    for(j=0;j<5;j++) for(i=0;i<8;i++)
    {
        x=106+20*j;
        y=290+20*i;
        if(vio_in[selected_card][40+8*j+i]) SelectObject(hMemDC,bmp_pinceau_vert);
        else                                SelectObject(hMemDC,bmp_pinceau_rouge);
        Rectangle(hMemDC,x,y,x+17,y+17);
        sprintf_s(ch,"%d",8*j+i+40);
        if((40+8*j+i)<10) EasyTextOut(x+4,y+2,ch); else EasyTextOut(x+0,y+2,ch);
    }
    x=100;
    y=290;
    EasyTextOut(x+3,y-18," Entrées [2]");

	// aff debug serial error
    SelectObject(hMemDC,bmp_pinceau_jaune);
    SelectObject(hMemDC,bmp_stylo_jaune);
    SetTextColor(hMemDC,RGB(0,0,0));
    Rectangle(hMemDC,25,500,150,550+5*20);
    sprintf_s(ch,"Err RS232: %d",serial_error); EasyTextOut(8,480,ch);
    sprintf_s(ch,"En leds:");                   EasyTextOut(8,498,ch);
	sprintf_s(ch,"Servo:0,1,2,3,4   Clic+");    EasyTextOut(8,516,ch);
	sprintf_s(ch,"G=OnOff");                    EasyTextOut(153,534,ch);
	sprintf_s(ch,"D=Manue");                    EasyTextOut(153,552,ch);
    sprintf_s(ch,"Tic nb   : %d",debug_tic_nb); EasyTextOut(8,571,ch);
	sprintf_s(ch,"G+Valeu");                    EasyTextOut(153,565,ch);
	sprintf_s(ch,"r+Entré");                    EasyTextOut(153,578,ch);
	sprintf_s(ch,"[0-255]");                    EasyTextOut(153,591,ch);

//  EasyTextOut(165,520,"'L'");
//  EasyTextOut(165,540,"'G'");
//	EasyTextOut(165,560,"'H'");

	SelectObject(hMemDC,bmp_pinceau_blanc);
    SelectObject(hMemDC,bmp_stylo_noir);
    SetTextColor(hMemDC,RGB(0,0,0));		// Texte en noir.
    x=88; y=498;
    if(cfg_enable_leds[selected_card]) { SelectObject(hMemDC,bmp_pinceau_vert);  Rectangle(hMemDC,x,y,x+25,y+17); EasyTextOut(x+4,y+3,"On" ); }
    else                               { SelectObject(hMemDC,bmp_pinceau_rouge); Rectangle(hMemDC,x,y,x+25,y+17); EasyTextOut(x+1,y+3,"Off"); }
    x=8; y=534;
    
	for(i=0;i<5;i++) {
		#ifndef VERSION_UTS_V3 // Ne pas toucher à cette ligne !!!
		Rectangle(hMemDC,x+30*i,y,x+20+30*i,y+17);
		if(cfg_enable_servos[selected_card][i]==0) { SelectObject(hMemDC,bmp_pinceau_rouge); Rectangle(hMemDC,x+30*i,y,x+25+30*i,y+17); EasyTextOut(x+2+30*i,y+1,"Off"); }
		else {
		#endif
			SelectObject(hMemDC,bmp_pinceau_vert);  Rectangle(hMemDC,x+30*i,y,x+25+30*i,y+17); EasyTextOut(x+2+30*i,y+1,"On");
//			On place la suite des éléments en dessous en : y = y+20 ;
			SelectObject(hMemDC,bmp_pinceau_blanc);
			Rectangle(hMemDC,x+30*i,y+20,x+25+30*i,y+17+20);		// Affiche les rectangles blancs avec la valeurs des servos
			if(serial_debug_select_srv[selected_card][i]) val=serial_debug_srv[selected_card][i]; else val=vio_srv[selected_card][i]>>2; //Passe de 1023 à 255 max en affichage
			sprintf_s(ch,"%d",val); EasyTextOut(x+30*i,y+20,ch);
			if(serial_debug_select_srv[selected_card][i]==0)
			{
				EasyLine(x+30*i,y+20   ,x+25+30*i,y+20+17);		// Affiche les rectangles blancs avec la valeurs des servos
				EasyLine(x+30*i,y+20+17,x+25+30*i   ,y+20);		// ou une croix
			}
		#ifndef VERSION_UTS_V3 // Ne pas toucher à cette ligne !!!
		}
		#endif
	}

	if(action==DEBUG_MOUSE) { // Rectangle de sélection 'LEDS'
        x=88; y=498;
		if(x_mouse>x && x_mouse<(x+25) && y_mouse>y && y_mouse<(y+17)) { cfg_enable_leds[selected_card]^=1; redraw=1; }

		#ifndef VERSION_UTS_V3 // Ne pas toucher à cette ligne !!!
	    x=8; y=534;
		for(i=0;i<NB_SERVO;i++) {  // Rectangles de sélection 'Servos' 'on/off'
			if(x_mouse>x+30*i && x_mouse<(x+25+30*i) && y_mouse>y && y_mouse<(y+17)) { cfg_enable_servos[selected_card][i]^=1; redraw=1; }
		}
		#endif
    }

    if(action==DEBUG_MOUSE_RIGHT) {
        x=8; y=552;
		for(i=0;i<5;i++) {
	        if(x_mouse>x+30*i && x_mouse<(x+25+30*i) && y_mouse>y && y_mouse<(y+17))
			{
				serial_debug_select_srv[selected_card][i]^=1;
	            if(serial_debug_select_srv[selected_card][i]) serial_debug_srv[selected_card][i]=vio_srv[selected_card][i]>>2;
			}
		}
    }



    // Affichage d'une liste de valeur de débugage (adresses mémoires du PIC)
    y=120;
    SelectObject(hMemDC,bmp_stylo_noir);
    SelectObject(hMemDC,bmp_pinceau_blanc);
    Rectangle(hMemDC,100,y+20,690,y+41);
    EasyLine(200,y+20,200,y+40); // Ligne du haut du tableau
    EasyLine(300,y+20,300,y+40);
    EasyLine(400,y+20,400,y+40);
    EasyLine(500,y+20,500,y+40);
    EasyLine(600,y+20,600,y+40);
    SetTextColor(hMemDC,RGB(0,0,0));		// Texte en noir.
    EasyTextOut(100+5,y+23,"Adresse_déc");
    EasyTextOut(200+5,y+23,"Adresse_hex");
    EasyTextOut(300+5,y+23,"Données_déc");
    EasyTextOut(400+5,y+23,"Données_hex");
    EasyTextOut(500+5,y+23,"Données_bin");
    EasyTextOut(600+5,y+23," Actions");
	EasyLine(191,y+39,200,y+30); // Trait en diagonal en bas à droite.
    EasyLine(291,y+39,300,y+30);
    EasyLine(391,y+39,400,y+30);
    EasyLine(491,y+39,500,y+30);
    EasyLine(681,y+39,690,y+30);

	SetTextColor(hMemDC,RGB(0,0,255));		// Texte en bleu.
	for(i=0;i<4;i++)
    {
        SelectObject(hMemDC,bmp_pinceau_blanc); // Dessine le tableau
        Rectangle(hMemDC,100,y+40+20*i,690,y+61+20*i);
        EasyLine(200,y+40+20*i,200,y+60+20*i);	// Trace les lignes verticales.
        EasyLine(300,y+40+20*i,300,y+60+20*i);
        EasyLine(400,y+40+20*i,400,y+60+20*i);
        EasyLine(500,y+40+20*i,500,y+60+20*i);
        EasyLine(600,y+40+20*i,600,y+60+20*i);
        EasyLine(630,y+40+20*i,630,y+60+20*i);
        EasyLine(660,y+40+20*i,660,y+60+20*i);
        sprintf_s(ch,"%4d",serial_tab_adresse[selected_card][i]); EasyTextOut(155,y+43+20*i,ch);
        sprintf_s(ch,"%4x",serial_tab_adresse[selected_card][i]); EasyTextOut(255,y+43+20*i,ch);
        sprintf_s(ch,"%4d",serial_tab_data[selected_card][i]);    EasyTextOut(355,y+43+20*i,ch);
        sprintf_s(ch,"%4x",serial_tab_data[selected_card][i]);    EasyTextOut(455,y+43+20*i,ch);
        for(j=1,k=7;j<=0x80;j=j*2,k--) {if(serial_tab_data[selected_card][i]&j) ch[k]='1'; else ch[k]='0';}
        ch[8]='\0';                             EasyTextOut(520,y+45+20*i,ch);
                                                EasyTextOut(607,y+45+20*i,"W");
                                                EasyTextOut(637,y+45+20*i,"R");
        if(tab_auto_read[selected_card][i])     EasyTextOut(667,y+45+20*i,"X");
        else                                    EasyTextOut(667,y+45+20*i,"A");
    }

    SetTextColor(hMemDC,RGB(0,0,0));		// texte en noir.
	EasyTextOut(692,148,"Clic + taper");
    EasyTextOut(692,160,"l'adresse");
    EasyTextOut(692,172,"'W'=Ecriture");
    EasyTextOut(692,184,"'R'=Lecture");
    EasyTextOut(692,196,"'A'=Lecture-");
	EasyTextOut(692,208,"'   continue");
	EasyTextOut(692,220,"'X'=Arrêt LC");

	EasyTextOut(2,160,"Clic sur une");
	EasyTextOut(2,172,"case blanche");
	EasyTextOut(2,184,"adr,taper un");
	EasyTextOut(2,196,"nombre puis");
	EasyTextOut(2,208,"'Entrée'");
	EasyTextOut(2,220,"puis W,R,A");
 
    if(action==DEBUG_MOUSE)
    {
        strcpy(tab_cmd_ch,"000");
        tab_cmd_colone=-1;
        srv_debug_cmd=-1;
        if(x_mouse>100 && x_mouse<500) if(y_mouse>(y+40) && y_mouse<(y+40+4*20))
        {
              tab_cmd_colone=(x_mouse-100)/100;
              tab_cmd_ligne =((y_mouse-y-40)/20);
        }
        if(x_mouse>600 && x_mouse<690) if(y_mouse>(y+40) && y_mouse<(y+40+4*20))
        {
              tab_cmd_colone=(x_mouse-600)/30;
              tab_cmd_ligne =((y_mouse-y-40)/20);
              if(tab_cmd_colone==0) serial_tab_cmd[selected_card][tab_cmd_ligne]='w';
              if(tab_cmd_colone==1) serial_tab_cmd[selected_card][tab_cmd_ligne]='r';
              if(tab_cmd_colone==2)
              {
                  tab_auto_read[selected_card][tab_cmd_ligne]^=1;
                  if(tab_auto_read[selected_card][tab_cmd_ligne]) serial_tab_cmd[selected_card][tab_cmd_ligne]='a';
                  else                                            serial_tab_cmd[selected_card][tab_cmd_ligne]='x';
              }
              tab_cmd_colone=-1;
        }

		// Nouveau en V3.3, 5 servos
		x=8; y=552;
		for(i=0;i<5;i++) {
			   if(x_mouse>x+30*i && x_mouse<(x+25+30*i)) if(y_mouse>y && y_mouse<(y+17))     srv_debug_cmd=i;
			// if(x_mouse>135 && x_mouse<(135+30))      if(y_mouse>540 && y_mouse<(540+17)) srv_debug_cmd=0;
			//if(x_mouse>135 && x_mouse<(135+30)) if(y_mouse>560 && y_mouse<(560+17)) srv_debug_cmd=1;
		}
    }
    if(action==DEBUG_KEY)
    {
        if(touche_char!=0xd) //'\n'==0x10 !!!!
        {
            tab_cmd_ch[0]=tab_cmd_ch[1];
            tab_cmd_ch[1]=tab_cmd_ch[2];
            tab_cmd_ch[2]=touche_char;		// Converion de 'int' en 'char'.
        }
        else
        {
            if(tab_cmd_colone==0) serial_tab_adresse[selected_card][tab_cmd_ligne]=atoi(tab_cmd_ch);
            if(tab_cmd_colone==1) serial_tab_adresse[selected_card][tab_cmd_ligne]=atoh(tab_cmd_ch);
            if(tab_cmd_colone==2) serial_tab_data   [selected_card][tab_cmd_ligne]=atoi(tab_cmd_ch);
            if(tab_cmd_colone==3) serial_tab_data   [selected_card][tab_cmd_ligne]=atoh(tab_cmd_ch);
            if(srv_debug_cmd==0) { if(serial_debug_select_srv[selected_card][0]) serial_debug_srv[selected_card][0]=atoi(tab_cmd_ch); }
            if(srv_debug_cmd==1) { if(serial_debug_select_srv[selected_card][1]) serial_debug_srv[selected_card][1]=atoi(tab_cmd_ch); }
            if(srv_debug_cmd==2) { if(serial_debug_select_srv[selected_card][2]) serial_debug_srv[selected_card][2]=atoi(tab_cmd_ch); }
            if(srv_debug_cmd==3) { if(serial_debug_select_srv[selected_card][3]) serial_debug_srv[selected_card][3]=atoi(tab_cmd_ch); }
            if(srv_debug_cmd==4) { if(serial_debug_select_srv[selected_card][4]) serial_debug_srv[selected_card][4]=atoi(tab_cmd_ch); }
            strcpy(tab_cmd_ch,"000");
        }
    }
}
/*****************************************************************************/
/* FIN DEBUG BOARD SCREEN                                                    */
/*****************************************************************************/