#include #include #include #include "core_simulation.h" // class BoardException int BoardException::get(){return num;} string BoardException::text(){ string s; switch(num){ case SPEED : s=string("mauvaise vitesse de la laison terminal");break; case INOUT : s=string("mauvaise utilisation du sens de l'entree/sortie"); break; case ADDRESS : s=string("mauvaise adresse de la pin"); break; case SIZE : s=string("taille erronee"); break; case EMPTY: s=string("zone vide"); break; default: s=string("erreur inconnue"); } return s; } // classe terminale void Terminal::begin(int speed){ if (speed!=9600){ cerr << "wrong speed\n"; throw BoardException(SPEED); } } void Terminal::println(string s){ cout <<"Serial: "<< s<=0)&&(addr=MAX_I2C_DEVICES)) throw BoardException(ADDRESS); if ((size<0)||(size>I2C_BUFFER_SIZE)) throw BoardException(SIZE); tabmutex[addr].lock(); memcpy(registre[addr],bytes,size*sizeof(char)); vide[addr]=false; tabmutex[addr].unlock(); return size; } int I2C::requestFrom(int addr, char* bytes, int size){ int result =0; if ((addr<0)||(addr>=MAX_I2C_DEVICES)) throw BoardException(ADDRESS); if ((size<0)||(size>I2C_BUFFER_SIZE)) throw BoardException(SIZE); if (vide[addr]==false){ tabmutex[addr].lock(); memcpy(bytes,registre[addr],size*sizeof(char)); vide[addr]=true; tabmutex[addr].unlock(); result =size; } return result; } char * I2C::getRegistre(int addr){ if ((addr<0)||(addr>=MAX_I2C_DEVICES)) throw BoardException(ADDRESS); return (registre[addr]); } bool* I2C::getVide(int addr){ if ((addr<0)||(addr>=MAX_I2C_DEVICES)) throw BoardException(ADDRESS); return (&vide[addr]); } // classe generique reprenstant un capteur/actionneur Device::Device(){ ptrtype=NULL; ptrmem=NULL; i2caddr=-1; i2cbus=NULL; } void Device::run(){ while(1){ cout << "empty device\n"; sleep(3); } } void Device::setPinMem(unsigned short* ptr,enum typeio *c){ ptrtype=c; ptrmem=ptr; } void Device::setI2CAddr(int addr, I2C * bus){ i2caddr=addr; i2cbus=bus; } // classe representant une carte arduino void Board::run(){ try{ setup(); while(1) loop(); } catch(BoardException e){ cout <<"exception: "<=MAX_I2C_DEVICES)) throw BoardException(ADDRESS); dev.setI2CAddr(addr,&bus); tabthreadbus[addr]=new thread(&Device::run,&dev); }