diff --git a/24h.ino b/24h.ino index 0ba9630..48ecfe7 100644 --- a/24h.ino +++ b/24h.ino @@ -2,7 +2,6 @@ #include "Carte.h" // Shell -#include "shell.h" #include "config.h" // Interupt @@ -11,32 +10,31 @@ // Rule Checker #include "rule_checker.h" - -// Shell commands -shell_command_t shell_commands[SHELL_COMMAND_COUNT]; +#include "utils.h" void setup(void) { - SHELL_COMMAND_DECL(0, "help", "this help", false, shell_command_help); - SHELL_COMMAND_DECL(1, "send", "send a debug static frame", true, sendDbgFrames); - SHELL_COMMAND_DECL(2, "init", "send a debug static frame", false, sendInit); - SHELL_COMMAND_DECL(3, "address", "display address", false, display_address); - SHELL_COMMAND_DECL(4, "status", "display status", false, display_status); - shell_setup(); - - io_setup(); - //rc_setup(); + Serial.begin(9600); + pinMode(dbg0_pin, OUTPUT); + pinMode(dbg1_pin, OUTPUT); carte_setup(); FlexiTimer2::set(25, 1.0/10000, tick2500us); // call every 500 1ms "ticks" // FlexiTimer2::set(500, flash); // MsTimer2 style is also supported FlexiTimer2::start(); + + //carte_update(); + //io_setup(); + pinMode(10, OUTPUT); + pinMode(11, OUTPUT); + + //carte_update(); + //io_loop(); } void loop(void) { - shell_loop(); - carte_loop(); + digitalWrite(10,HIGH); + digitalWrite(11,HIGH); + carte_update(); io_loop(); - // Please dont add delay here :( - // delay(1500); } diff --git a/Carte.cpp b/Carte.cpp index 02f6be1..0a5c6a3 100644 --- a/Carte.cpp +++ b/Carte.cpp @@ -1,111 +1,67 @@ +#include "my_def.h" #include "Arduino.h" #include "Carte.h" -#include "my_def.h" - -#define BASIC 0 -#define ALPHABET 1 -#define SEMAINE 2 - -#define RULE BASIC - -Carte my_carte(1, 1); - -Carte::Carte(int val, int state): - myval(val), - mystate(state), - Dval(2), - Gval(4), - AllG(true), - LocG(false) -{ - // FIXME: these statments do not have any effect! - this->DG[2]; - this->hasLCD; +#include "rule_checker.h" + +Carte::Carte(int myname): + name(myname), + state(0x03), + Rname(0), + Lname(0), + Rstate(0), + Lstate(0), + globSol(true), + locSol(false), + hasLCD(false){} + +boolean Carte::update_globSol(){ + boolean globSol=this->state & (1 << 0);//macro avrlib _BV + return globSol; } -//Verififier que c est les bon bits -boolean Carte::get_AllGood(int PIN,byte mystate){ - boolean AllG=this->mystate & (1 << 0);//macro avrlib _BV - return AllG; +boolean Carte::update_locSol(){ + boolean locSol=this->state & (1 << 1);//macro avrlib _BV + return locSol; } -boolean Carte::get_LocGood(int PIN, byte mystate){ - boolean AllL=this->mystate & (1 << 1);//macro avrlib _BV - return LocG; -} +//Verififier que c est les bon bits +//boolean Carte::get_globSol(){ +// return this->state & (1 << 0);//macro avrlib _BV +//} -//tester si le comp fonctionne en bytes -boolean Carte::valid(int D_val,int G_val){ - return D_val-'0'<=G_val-'0'; -} +//boolean Carte::get_globSol(){ +// return this->state & (1 << 1);//macro avrlib _BV +//} -void Carte::check(){ - byte Dval=2; - byte Gval=4; - this->DG[0]=valid(this->Dval, this->myval); - this->DG[1]=valid(this->myval, this->Gval); - } void Carte::update(){ - boolean DG[2]; - boolean DAllG, GAllG; - switch (RULE){ case (BASIC): - this->check(); - this->LocG=DG[0] && DG[1]; - - + this->locSol=basic_check(this->name, this->Lname, this->Rname); case (ALPHABET || SEMAINE): - LocG=rule_check(this->myval, this->Gval, this->Dval); + this->locSol=rule_check(this->name, this->Lname, this->Rname); } - this->check(); - this->LocG=DG[0] && DG[1]; - this->AllG=(DAllG && GAllG && LocG); -} - -void carte_setup() -{ - if (RULE != BASIC){ - rc_setup(); - } - pinMode(PIN_LED_ROUGE, OUTPUT); - pinMode(PIN_LED_JAUNE, OUTPUT); - digitalWrite(PIN_LED_ROUGE, LOW); - digitalWrite(PIN_LED_JAUNE, LOW); -// Phase 1 : Est-on en extrémité ? + this->globSol=(this->Rstate%2 && this->Lstate%2 && this->globSol); } -void carte_loop() -{ - //Serial.println(my_carte.myval); - //my_carte.update(); - - // Phase 3 : Diffusion de mon Adresse - - // Phase 4 : Apprentissage - qui sont mes voisins ? - - // Phase 5 : Détermination Etat Local et Global - - - - +void Carte::display_names(){ + Serial.println(" L | * | R"); + Serial.println("----------------"); + Serial.print(this->Lname); + Serial.print(" | "); + Serial.print(this->name); + Serial.print(" | "); + Serial.println(this->Rname); + Serial.println("________________"); } -void display_carte_props() -{ - /* - Serial.println("Droite| Moi |Gauche"); - Serial.print(my_carte.Dval); - Serial.print(" | "); - Serial.print(my_carte.myval); - Serial.print(" | "); - Serial.println(my_carte.Gval); - */ - Serial.print(my_carte.DG[0]); - Serial.print(" | "); - Serial.print("D G"); - Serial.print(" | "); - Serial.print(my_carte.DG[1]); -} +void Carte::display_states(){ + Serial.print(" Loc/Glob | "); + bool isLoc=(this->Lname<=this->name && this->name<=this->Rname)? 'Good!':'Bad'; + Serial.print(isLoc); + bool isGlob=(this->Lname<=this->name && this->name<=this->Rname)? 'Good!':'Bad'; + Serial.print(" "); + Serial.println(isGlob); + Serial.println("____________"); +} \ No newline at end of file diff --git a/Carte.h b/Carte.h index 5b6a82b..9ea05f9 100644 --- a/Carte.h +++ b/Carte.h @@ -6,24 +6,22 @@ class Carte{ public: - Carte(int val, int state); - int myval; //Nom ou adresse de la carte - int mystate; //donnees libres - int Dval; - int Gval; - boolean AllG; //All good : no error detected + Carte(int); + int name; //Nom ou adresse de la carte + int state; //donnees libres + int Rname; + int Lname; + int Rstate; + int Lstate; + boolean globSol; //All good : no error detected + boolean locSol; //All good : no error detected boolean hasLCD; //All good : no error detected - boolean LocG; //Local good: mes voisins sont bons boolean DG[2]; //validite des voisins Droit et Gauche - boolean get_AllGood(int, byte); - boolean get_LocGood(int, byte); - boolean valid(int, int); //rule checker - void check(); //update DG + boolean update_globSol(); + boolean update_locSol(); void update(); //update All Good and Locally Good - void display(); + void display_names(); + void display_states(); }; -void carte_setup(); -void carte_loop(); -void display_carte_props(); #endif diff --git a/config.h b/config.h index cd54c4e..bf3a87e 100644 --- a/config.h +++ b/config.h @@ -1,6 +1,6 @@ #ifndef __CONFIG_H__ #define __CONFIG_H__ -#define SHELL_COMMAND_COUNT 5 +#define SHELL_COMMAND_COUNT 2 #endif diff --git a/io.cpp b/io.cpp index 2afba17..f7342c0 100644 --- a/io.cpp +++ b/io.cpp @@ -3,9 +3,6 @@ #include -IODevice left(out_left, in_left); -IODevice right(out_right, in_right); - /* * Pour chaque bit dans la frame (1 + address + data): * - s'il est a 1, on met la sortie à 1 et temps = 8 (8*5 = 40 ms) @@ -16,16 +13,9 @@ IODevice right(out_right, in_right); * si fin de trame temps = 1 (5ms) */ -static byte address = 0x55; - -void display_address (const char*args) -{ - Serial.print("address: "); - Serial.print(address, HEX); - Serial.print("\r\n"); -} - -IODevice::IODevice(const int output_pin, const int input_pin) : +IODevice::IODevice(const int output_pin, int input_pin) : + _encountered_frame(false), + _unconnected(false), _tick_toogle(false), _output_frame(0x0000), _output_remainingTicks(0), @@ -35,11 +25,13 @@ IODevice::IODevice(const int output_pin, const int input_pin) : _input_received_frame_is_available(false), _input_received_frame(0x0000), _input_current_bit(9), - _input_time_at_level(0) // 0 == IDLE + _input_time_at_level(0), // 0 == IDLE + _input_name(0) { + if (input_pin==in_left) _input_state=RIGHT_TO_LEFT; + if (input_pin==in_right) _input_state=LEFT_TO_RIGHT; _output_pin = output_pin; pinMode(_output_pin, OUTPUT); - _input_pin = input_pin; } @@ -47,30 +39,31 @@ void IODevice::sendFrame(const byte address, const byte data) { while(_output_state!=IDLE) {} _output_frame = (1 << 9) | ((uint16_t)address << 2) | ((uint16_t)data & 0x03); + Serial.print("SENT frame: "); + printFrame(_output_frame); _output_currentBit = 9; _output_state = SENDING; } -void IODevice::display_status() +uint16_t IODevice::receiveFrame() { - Serial.print("status: "); - if(connection_type == LOOP) Serial.print("LOOP"); - if(connection_type == UNKOWN) Serial.print("UNKNOWN"); - if(connection_type == NORMAL) { - Serial.print("connected to "); - uint16_t address = (_input_received_frame >> 2) & 0x7f; - Serial.print((uint8_t)address, HEX); - } - Serial.print("\r\n"); + _input_received_frame_is_available = false; + _input_name=(_input_received_frame >> 2) & 0x07f; + _input_state=_input_received_frame & 0x03; + Serial.print("RECV frame: "); + printFrame(_input_received_frame); } void IODevice::tick2500us() { input_level_detect(); - // Prescale /2 _tick_toogle = ! _tick_toogle; if(_tick_toogle) tick5ms(); + _tick_big+=1; + if (_tick_big%20==0){ + tick50ms(); + } } void IODevice::tick5ms() @@ -108,6 +101,10 @@ void IODevice::tick5ms() } } +void IODevice::tick50ms() +{ + if (_encountered_frame) _unconnected=false; +} void IODevice::input_bitshift(const int8_t bit) { @@ -146,11 +143,6 @@ void IODevice::input_level_push(int _input_time_at_level) // Too long frame input_bitshift(-1); } - } else { - if(_input_time_at_level > 2) { - // Too long low - input_bitshift(-1); - } } } @@ -184,113 +176,31 @@ void IODevice::input_level_detect() } } -uint16_t IODevice::receiveFrame() -{ - _input_received_frame_is_available = false; - return _input_received_frame; -} - -// interuption toutes les 2.5ms -void tick2500us() -{ - left.tick2500us(); - right.tick2500us(); -} - -void sendDbgFrames(const char* args) -{ - Serial.println("left.sendFrame(0x55, 0x02);"); - left.sendFrame(0x55, 0x02); - while(left.state() != IDLE) {}; - - Serial.println("right.sendFrame(0x2E, 0x03)"); - right.sendFrame(0x2E, 0x03); - - while(right.state() != IDLE) {}; - Serial.println("done."); -} - -#define LEFT_TO_RIGHT 0x01 -#define RIGHT_TO_LEFT 0x00 - -void sendInit(const char* args) -{ - left.sendFrame(address, LEFT_TO_RIGHT); - right.sendFrame(address, RIGHT_TO_LEFT); -} - -void io_setup(void) +void IODevice::set_encountered_frame() { - pinMode(led_pin, OUTPUT); + _encountered_frame=true; } -void printFrame(const uint16_t frame) +void IODevice::printFrame(const uint16_t frame) { - Serial.print("RECV frame: "); for(int8_t i=9; i>=0; i--) { char c = (frame & (1< we have a loop - Serial.print("Loop detected on the left\r\n"); - left.connection_type = LOOP; - } else { - left.sendFrame(address, LEFT_TO_RIGHT); - left.connection_type = NORMAL; - } -// printFrame(leftFrame); - } - - uint16_t rightFrame; - if(right.connection_type == UNKOWN) right.sendFrame(address, RIGHT_TO_LEFT); - if(right.inputFrameAvailable()) { - rightFrame = right.receiveFrame(); - if(!(rightFrame & LEFT_TO_RIGHT)) { - // We received a frame from right expected to be on the left -> we have a loop - Serial.print("Loop detected on the right\r\n"); - right.connection_type = LOOP; - } else { - right.sendFrame(address, RIGHT_TO_LEFT); - right.connection_type = NORMAL; - } -// printFrame(rightFrame); - } - - bool left_is_ok = (left.connection_type == LOOP); - bool right_is_ok = (right.connection_type == LOOP); - if((left.connection_type != UNKOWN) && (right.connection_type != UNKOWN)) { - if(left.connection_type == NORMAL) { - uint16_t leftAddress = leftFrame >> 2; // Drop data bits - leftAddress &= 0x007F; // Drop anything except address - if (leftAddress <= address) - left_is_ok = true; - } - if(right.connection_type == NORMAL) { - uint16_t rightAddress = rightFrame >> 2; // Drop data bits - rightAddress &= 0x007F; // Drop anything except address - if (rightAddress >= address) - right_is_ok = true; - } - } - digitalWrite(led_pin, (right_is_ok && left_is_ok)); +/* +boolean isConnected(IODevice* left, IODevice* right){ + return (left._unconnected || right._unconnected) } - -void display_status(const char* args) +*/ +void io_setup(void) { - Serial.print("Left "); - left.display_status(); - Serial.print("Right "); - right.display_status(); + pinMode(loc_pin, OUTPUT); + pinMode(glob_pin, OUTPUT); + digitalWrite(loc_pin,HIGH); + digitalWrite(glob_pin,HIGH); } diff --git a/io.h b/io.h index f450107..d5f7764 100644 --- a/io.h +++ b/io.h @@ -4,7 +4,7 @@ #include typedef enum { IDLE, SENDING, END_FRAME } output_state; -typedef enum { UNKOWN, LOOP, NORMAL } connection_type_t; +typedef enum { UNKOWN, LOOP, NORMAL } connection_type_t; void io_setup(void); void io_loop(void); @@ -12,10 +12,7 @@ void io_loop(void); // interuption toutes les 2.5ms void tick2500us(); -void sendInit(const char*); -void sendDbgFrames(const char*); -void display_address(const char*); -void display_status(const char*); +void plop(const char*); class IODevice { public: @@ -26,18 +23,22 @@ class IODevice { void sendFrame(const byte address, const byte data); bool inputFrameAvailable() { return _input_received_frame_is_available; } uint16_t receiveFrame(); - - output_state state() const { return _output_state; } - + //boolean isConnected(); + output_state state() const { return _output_state;} connection_type_t connection_type; - + uint8_t input_name(){return _input_name;} + uint8_t input_state(){return _input_state;} void display_status(); + void set_encountered_frame(); private: // Tick bool _tick_toogle; + bool _encountered_frame; + bool _unconnected; + int _tick_big; void tick5ms(); - + void tick50ms(); // Output int _output_pin; volatile uint16_t _output_frame; @@ -50,12 +51,14 @@ class IODevice { volatile uint16_t _input_frame; volatile bool _input_received_frame_is_available; volatile uint16_t _input_received_frame; + volatile uint16_t _input_name; + volatile uint16_t _input_state; volatile uint8_t _input_current_bit; int16_t _input_time_at_level; void input_bitshift(int8_t); void input_level_push(int); void input_level_detect(); + void printFrame(const uint16_t); }; - -#endif +#endif \ No newline at end of file diff --git a/my_def.h b/my_def.h index 288fe12..b0d7a8f 100644 --- a/my_def.h +++ b/my_def.h @@ -8,7 +8,24 @@ #define LED_ROUGE_ON digitalWrite(PIN_LED_ROUGE,HIGH) #define LED_ROUGE_OFF digitalWrite(PIN_LED_ROUGE,LOW) +//Def for rule checkers +#define BASIC 0 +#define ALPHABET 1 +#define SEMAINE 2 +#define RULE ALPHABET + +//initial trame +#define MYNAME 'A' + +#define LEFT_TO_RIGHT 0x00 +#define RIGHT_TO_LEFT 0x02 + +const int dbg0_pin = 1; +const int dbg1_pin = 12; + const int led_pin = 13; +const int loc_pin = 10; +const int glob_pin = 11; const int out_left = 4; const int out_right = 5; diff --git a/rule_checker.cpp b/rule_checker.cpp index 1edfa11..9959407 100644 --- a/rule_checker.cpp +++ b/rule_checker.cpp @@ -1,16 +1,38 @@ - #include "rule_checker.h" +#include "rule_checker.h" + +//checked if ascii order is respected +boolean basic_check(byte name, byte Lname, byte Rname){ + return ((Lname<=name) && (name && Rname)); +} int nbEltarrConformityrule; Conformityrule arrConformityrule[NB_REGLES_MAX]; // Règle de chronologie imposée -byte rule[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'}; +//byte rule[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'}; +byte rule[] = { 'b', 'a', 'c'}; /* initialise un dictionaire stocké dans un array indiquant les voisins de droite et de gauche valides */ +//Aritrary rule based on dictionary +// vérifie si l'equipement placé du cté COTE de notre arduino possède une valeur compatible avec le RuleChecker +boolean rule_check(byte myVal, byte valGauche, byte valDroite) +{ + for (int pos=0 ; pos +#include "rule_checker.h" + +void tick2500us(); +void io_loop(); +void carte_setup(); +void carte_update(); +#endif \ No newline at end of file