Passer un code PERL en C++ [Résolu]

Signaler
Messages postés
20
Date d'inscription
jeudi 14 août 2003
Statut
Membre
Dernière intervention
28 janvier 2006
-
Messages postés
20
Date d'inscription
jeudi 14 août 2003
Statut
Membre
Dernière intervention
28 janvier 2006
-
Bonjour à tous,

J'ai trouvé un script Perl qui correspond à une recherche que je fais
depuis un certain temps : passer du morse sur les leds du clavier. Et
je voulais savoir comment passer ce script Perl en C++ ?

Avez-vous une idée ? Existe-t-il des converttisseurs ? Merci à tous :)



#!/usr/bin/perl -w

#
# morsecat.pl
#
# Jacek Fedorynski <jfedor@jfedor.org>
# http://www.jfedor.org/
# 2001-11-15
#
# Displays a text file in Morse code using a LED on your keyboard.
#
# (as in the book Cryptonomicon by Neal Stephenson)
#
# If the DISPLAY environment variable is set we will use xset to control
# the LED. If it's not set we will use setleds (which is probably very
# Linux-specific). To make it work under X I had to change the InputDevice
# section for the keyboard in /etc/X11/XF86Config-4 to look like this
# (the side effect was that my xmodmap keyboard mappings stopped working):
#
# Section "InputDevice"
# Identifier "Keyboard0"
# Driver "keyboard"
# Option "Xleds" "1 2 3"
# Option "XkbDisable"
# EndSection
#
# If you know a way to control keyboard LEDs (or other LEDs) on other platforms
# please let me know. If you think there's something wrong with the Morse
# code this program generates also let me know.
#
# Have fun!
#

use strict;

my $UNIT_TIME = 0.12; # length of a single dot in seconds

my %code = (
'A' => [0, 1],
'B' => [1, 0, 0, 0],
'C' => [1, 0, 1, 0],
'D' => [1, 0, 0],
'E' => [0],
'F' => [0, 0, 1, 0],
'G' => [1, 1, 0],
'H' => [0, 0, 0, 0],
'I' => [0, 0],
'J' => [0, 1, 1, 1],
'K' => [1, 0, 1],
'L' => [0, 1, 0, 0],
'M' => [1, 1],
'N' => [1, 0],
'O' => [1, 1, 1],
'P' => [0, 1, 1, 0],
'Q' => [1, 1, 0, 1],
'R' => [0, 1, 0],
'S' => [0, 0, 0],
'T' => [1],
'U' => [0, 0, 1],
'V' => [0, 0, 0, 1],
'W' => [0, 1, 1],
'X' => [1, 0, 0, 1],
'Y' => [1, 0, 1, 1],
'Z' => [1, 1, 0, 0],
'0' => [1, 1, 1, 1, 1],
'1' => [0, 1, 1, 1, 1],
'2' => [0, 0, 1, 1, 1],
'3' => [0, 0, 0, 1, 1],
'4' => [0, 0, 0, 0, 1],
'5' => [0, 0, 0, 0, 0],
'6' => [1, 0, 0, 0, 0],
'7' => [1, 1, 0, 0, 0],
'8' => [1, 1, 1, 0, 0],
'9' => [1, 1, 1, 1, 0],
' ' => [2]
);

sub mysleep {
my ($time) = @_;
select(undef, undef, undef, $time);
}

sub turn {
my ($on) = (@_);
if (defined $ENV{DISPLAY}) {
system("xset ".($on ? "" : "-")."led 2");
} else {
system("setleds -L ".($on ? "+" : "-")."num");
}
}

my $buf;

my $filename = $ARGV[0] || "-";

open(INPUT, "< $filename") or die "cannot open input file";

while (sysread INPUT, $buf, 1) {
if (defined $code{uc($buf)}) {
for my $i (@{$code{uc $buf}}) {
turn(1) unless $i == 2;
if ($i == 0) {
mysleep($UNIT_TIME); # dot
} elsif ($i == 1) {
mysleep($UNIT_TIME * 3); # dash
} elsif ($i == 2) {
mysleep($UNIT_TIME * 3); # word space
}
turn(0);
mysleep($UNIT_TIME);
}
mysleep($UNIT_TIME * 2); # letter space
}
}

2 réponses

Messages postés
2070
Date d'inscription
mardi 22 avril 2003
Statut
Membre
Dernière intervention
3 juillet 2006
8
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


struct DATA
{
char c;
char* str;
};


DATA aData[] =
{
{'A', "01"},
{'B', "1000"},
{'C', "1010"},
{'D', "100"},
{'E', "0"},
{'F', "0010"},
{'G', "110"},
{'H', "0000"},
{'I', "00"},
{'J', "0111"},
{'K', "101"},
{'L', "0100"},
{'M', "11"},
{'N', "10"},
{'O', "111"},
{'P', "0110"},
{'Q', "1101"},
{'R', "010"},
{'S', "000"},
{'T', "1"},
{'U', "001"},
{'V', "0001"},
{'W', "011"},
{'X', "1001"},
{'Y', "1011"},
{'Z', "1100"},
{'0', "11111"},
{'1', "01111"},
{'2', "00111"},
{'3', "00011"},
{'4', "00001"},
{'5', "00000"},
{'6', "10000"},
{'7', "11000"},
{'8', "11100"},
{'9', "11110"},
};


#define SLEEP_TIME 120


void led(int on)
{
}


int main(int argc, char* argv[])
{
if(argc != 2)
{
printf("No input file\n");
return 0;
}


FILE* f = fopen(argv[1], "rt");
if(f == NULL)
{
printf("Unable to open input file\n");
return 0;
}
led(0);


while(!feof(f))
{
char c = fgetc(f);
if(c ' ' || c '\r' || c == '\n')
{
// word break
led(0);
Sleep(3*SLEEP_TIME);
}
else
{
for(int i = 0; i < sizeof(aData)/sizeof(aData[0]); i++)
{
if(aData[i].c == toupper(c))
{
int len = strlen(aData[i].str);
for(int j = 0; j < len; j++)
{
led(1);
if(aData[i].str[j] == '0')
Sleep(SLEEP_TIME); // Dot
else
Sleep(3*SLEEP_TIME); // Dash


led(0);
Sleep(SLEEP_TIME);
}
}
}
}


// letter break
led(0);
Sleep(2*SLEEP_TIME);
}


fclose(f);
return 0;
}

Pas testé mais doit donner un truc du genre
Il manque le code pour allumer les Leds (doit se trouver et dépend bien sur de la plateforme)
Messages postés
20
Date d'inscription
jeudi 14 août 2003
Statut
Membre
Dernière intervention
28 janvier 2006

Merci ! Je vais tester et combiner ça avec mes bouts de code en leds... à bientôt !