morse_code_transcoder/transcodeur/morse.c

182 lines
No EOL
4.5 KiB
C

/*
* morse.c
*
* Created: 18/05/2021 15:47:14
* Author: yboujon1
*/
#include "main.h"
#include "morse.h"
unsigned char morse_library[LETTRES][7]= { {'a',POINT,TRAIT,'\0'},
{'b',TRAIT,POINT,POINT,POINT,'\0'},
{'c',TRAIT,POINT,TRAIT,POINT,'\0'},
{'d',TRAIT,POINT,POINT,'\0'},
{'e',POINT,'\0'},
{'f',POINT,POINT,TRAIT,POINT,'\0'},
{'g',TRAIT,TRAIT,POINT,'\0'},
{'h',POINT,POINT,POINT,POINT,'\0'},
{'i',POINT,POINT,'\0'},
{'j',POINT,TRAIT,TRAIT,TRAIT,'\0'},
{'k',TRAIT,POINT,TRAIT,'\0'},
{'l',POINT,TRAIT,POINT,POINT,'\0'},
{'m',TRAIT,TRAIT,'\0'},
{'n',TRAIT,POINT,'\0'},
{'o',TRAIT,TRAIT,TRAIT,'\0'},
{'p',POINT,TRAIT,TRAIT,POINT,'\0'},
{'q',TRAIT,TRAIT,POINT,TRAIT,'\0'},
{'r',POINT,TRAIT,POINT,'\0'},
{'s',POINT,POINT,POINT,'\0'},
{'t',TRAIT,'\0'},
{'u',POINT,POINT,TRAIT,'\0'},
{'v',POINT,POINT,POINT,TRAIT,'\0'},
{'w',POINT,TRAIT,TRAIT,'\0'},
{'x',TRAIT,POINT,POINT,TRAIT,'\0'},
{'y',TRAIT,POINT,TRAIT,TRAIT,'\0'},
{'z',TRAIT,TRAIT,POINT,POINT,'\0'},
{'0',TRAIT,TRAIT,TRAIT,TRAIT,TRAIT,'\0'},
{'1',POINT,TRAIT,TRAIT,TRAIT,TRAIT,'\0'},
{'2',POINT,POINT,TRAIT,TRAIT,TRAIT,'\0'},
{'3',POINT,POINT,POINT,TRAIT,TRAIT,'\0'},
{'4',POINT,POINT,POINT,POINT,TRAIT,'\0'},
{'5',POINT,POINT,POINT,POINT,POINT,'\0'},
{'6',TRAIT,POINT,POINT,POINT,POINT,'\0'},
{'7',TRAIT,TRAIT,POINT,POINT,POINT,'\0'},
{'8',TRAIT,TRAIT,TRAIT,POINT,POINT,'\0'},
{'9',TRAIT,TRAIT,TRAIT,TRAIT,POINT,'\0'}
};
void envoyer_signe_morse(unsigned char signe_morse)
{
switch(signe_morse){
case POINT:
start_timer0();
start_timer1(ONEMS);
wait_ocf1a_timer1();
stop_timer0();
wait_ocf1a_timer1();
break;
case TRAIT:
start_timer0();
start_timer1(3*ONEMS);
wait_ocf1a_timer1();
stop_timer0();
start_timer1(ONEMS);
wait_ocf1a_timer1();
break;
case INTERLETTRE:
start_timer1(2*ONEMS);
wait_ocf1a_timer1();
break;
case INTERMOT:
start_timer1(10*ONEMS);
wait_ocf1a_timer1();
break;
case FIN:
resume_timer1();
stop_timer1();
break;
default:
break;
};
}
void maj2min_convert(char *tab, int n)
{
int i;
for(i=0; i<=n; i++)
{
if(tab[i]>=65 && tab[i]<=90) //si c'est une majuscule
{
tab[i]=tab[i]+32; //la met en minuscule
}
else
{
tab[i]=tab[i]; //sinon rien
};
};
}
void latin2morse(char *tab, int n)
{
int i=0; //varie la chaine de caractere
int j=0; //varie la librarie
do
{
switch(j)
{
case 0:
lcd_char(tab,&i);
maj2min_convert(tab,i);
default:
if(tab[i]==morse_library[j][0]) //verifie si le caractere correspond ? la librarie
{
library_checker(tab,&i,&j); //traduit dans la librairie et renvoie les deux variables pour continuer
}
else //si ce n'est pas dans la librarie
{
simple_terms(tab,&i,&j); //traduit directement les termes plus simples
};
break;
};
}while(i<n); //tant qu'on arrive ? la fin du tableau
}
void library_checker(char *tab,int* i,int* j)
{
morselibrary_output(*j); //traduit en morse
*j=0; //on revient au d?but de la librairie
*i=*i+1; //on avance dans la chaine
if(tab[*i]!=32 && tab[*i]!='\0') //s'il n'y a pas d'espace ou de fin de mot apr?s, INTERLETTRE
{
envoyer_signe_morse(INTERLETTRE);
};
}
void simple_terms(char *tab,int* i,int* j)
{
switch(tab[*i]) //en fonction du caractere
{
case 32: //si c'est un espace INTERMOT
envoyer_signe_morse(INTERMOT);
*j=0; //on revient au d?but de la librairie
*i=*i+1; //on avance dans la chaine
break;
case '\0':
envoyer_signe_morse(FIN); //si c'est la fin de la chaine, FIN
*j=0; //on revient au d?but de la librairie
*i=*i+1; //on avance dans la chaine
break;
default: //dans d'autres cas continuer l'analyse de la chaine
*j=*j+1; //on avance dans la tableau
};
}
void morselibrary_output(int id)
{
int y=1; //on commence a la 2eme colonne
do
{
if(etat==0){
envoyer_signe_morse(morse_library[id][y]); //fonction qui envoie un trait ou un point en fonction de la librarie
y++;
}
else{
//nothing
};
}
while(morse_library[id][y]!='\0'); //jusqu'a la fin de la traduction dans la librairie
}
void lcd_char(char *tab,int* i)
{
switch(*i)
{
case 0:
LCD_Command(SECOND_LINE);
printf("%c",tab[*i]);
break;
default:
printf("%c",tab[*i]);
};
}