From 095ee07bd28b13778ed4d5f1db1a4bb10483ff75 Mon Sep 17 00:00:00 2001 From: tzim Date: Wed, 27 Nov 2013 00:24:24 +0000 Subject: [PATCH] * DMX-2.0.sln: ajout code arduino * UniversDMX.cs: bug... * DriverBoitierV1.cs: gestion par automate * dmx512_v2_ino.c: --- DMX-2.0.sln | 7 ++ DMX-2.0/DriverBoitierV1.cs | 125 ++++++++++++------- DMX-2.0/UniversDMX.cs | 2 +- dmx512_v2_ino.c | 246 +++++++++++++++++++++++++++++++++++++ 4 files changed, 336 insertions(+), 44 deletions(-) create mode 100644 dmx512_v2_ino.c diff --git a/DMX-2.0.sln b/DMX-2.0.sln index 328ac05..afb5690 100644 --- a/DMX-2.0.sln +++ b/DMX-2.0.sln @@ -3,6 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 11.00 # Visual Studio 2010 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DMX-2.0", "DMX-2.0\DMX-2.0.csproj", "{2CB55300-0A5B-4DFA-8984-B7EC4C455962}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{EBEAADCF-456B-43AB-A1ED-E81459AD7DDF}" + ProjectSection(SolutionItems) = preProject + dmx512_v2_ino.c = dmx512_v2_ino.c + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x86 = Debug|x86 @@ -14,6 +19,8 @@ Global {2CB55300-0A5B-4DFA-8984-B7EC4C455962}.Release|x86.ActiveCfg = Release|x86 {2CB55300-0A5B-4DFA-8984-B7EC4C455962}.Release|x86.Build.0 = Release|x86 EndGlobalSection + GlobalSection(NestedProjects) = preSolution + EndGlobalSection GlobalSection(MonoDevelopProperties) = preSolution StartupItem = DMX-2.0\DMX-2.0.csproj Policies = $0 diff --git a/DMX-2.0/DriverBoitierV1.cs b/DMX-2.0/DriverBoitierV1.cs index 7c1a739..fd017e4 100644 --- a/DMX-2.0/DriverBoitierV1.cs +++ b/DMX-2.0/DriverBoitierV1.cs @@ -13,6 +13,15 @@ namespace DMX2 } public byte button; public bool pressed; + } + + enum etatAutomate { + Deconnecte, + Transmission, + Erreur, + Reset, + Parametrage, + Fin } bool[] buttons = new bool[8]; @@ -25,7 +34,6 @@ namespace DMX2 //Thread de boucle Thread loopthread=null; - volatile bool running=true; UniversDMX patch=null; @@ -45,86 +53,117 @@ namespace DMX2 void Start () { - OpenPort(); if (loopthread == null) { loopthread = new Thread(new ThreadStart(MainLoop)); loopthread.Start(); } } - void OpenPort () + void Connection () { if (serial != null) { serial.Close(); + serial.Dispose(); } serial = new SerialPort(portname, 460800,Parity.None,8,StopBits.One); serial.DtrEnable = false; serial.ReadTimeout = 15; serial.WriteTimeout = 200; serial.Open(); + etat = etatAutomate.Transmission; } - bool CheckPortStatus () + volatile etatAutomate etat = etatAutomate.Deconnecte; + DateTime finAttente = DateTime.Now; + + void MainLoop() { - if(serial.IsOpen) return true; - - OpenPort(); - - return false; - } - - - void MainLoop () - { - DateTime prochainEnvoi= DateTime.Now; - TimeSpan sleeptime; - while (running) { - lock(Conduite.Courante) - { - patch.CalculUnivers(outputbuffer,5,255); + while(etat != etatAutomate.Fin) + { + switch (etat) { + case etatAutomate.Deconnecte: + Connection(); + Attente(DateTime.Now.AddMilliseconds(200)); + break; + case etatAutomate.Transmission: + finAttente = DateTime.Now.AddMilliseconds (22); + EnvoiTrame(); + Reception(); + Attente(finAttente); + break; + case etatAutomate.Erreur: + Deconnecte(); + Attente(DateTime.Now.AddSeconds(2)); + break; +// case etatAutomate.Parametrage: +// EnvoiParam(); +// break; +// case etatAutomate.Reset: +// EnvoiReset(); +// break; } - - if(!CheckPortStatus()) - { - Thread.Sleep(1000); - continue; - } - - sleeptime = prochainEnvoi - DateTime.Now; - if(sleeptime.TotalMilliseconds>1) - Thread.Sleep(sleeptime); - - prochainEnvoi = DateTime.Now.AddMilliseconds(22); - Envoi(); - Reception(); - } + Deconnecte(); } - void Envoi () + void Attente (DateTime date) + { + int sleeptime = (int) (date - DateTime.Now).TotalMilliseconds; + if(sleeptime>2) + Thread.Sleep(sleeptime); + + while (DateTime.Now start code) + sbi(UCSR0B, UDRIE0); // activation de l'intéruption registre emmission + + } + // Esc 'C' réinit + if (c==67) {etat_input_pc=0;index_input_pc=0; + + pe=tab_input_pc+514; + for(pb=tab_input_pc;pb 512) {// on se prépare à emmetre vers le pc + etat_input_pc=0; + digitalWrite(ledPin, HIGH); + + } + break; + case 10: // 1er parametre : nb de circuits / 2 - 1 ( de 2 a 512 ) + etat_input_pc=11; nb_circuits= ((int)c)*2 + 2; + break; + case 11: // 2nd parametre : duree du break + etat_input_pc=12; + brk_timer_start = 255 - (c/2) +1; + break; + case 12: // 3eme parametre : duree du mab + etat_input_pc=0; + mab_timer_start = 255 - (c/2) +1; + break; + default: + // on fait rien + break; + } + +} + + +// vecteur d'intéruption pour reception dmx +ISR(USART1_RX_vect) +{ + char c,r; + r = UCSR1A; + c = UDR1; + if (r & (1< break + index_output_dmx=-1; +} +// vecteur : USART 1 transmission registre vide +ISR(USART1_UDRE_vect) +{ + if (index_output_dmx>=0) { //si index >=0 caratère suivant + UDR1 = max(tab_input_pc[index_output_dmx],tab_input_dmx[index_output_dmx]); + index_output_dmx++; + } + // si 512 transmits => mise en attente de fin de transmission + // desactivation de l'intéruption sur le registre + if (index_output_dmx>nb_circuits) {index_output_dmx=-2;cbi(UCSR1B, UDRIE1);} + +} + +// vecteur d'intéruption pour transmission vers le pc sur serial 0 +// vecteur registre de transmission +ISR(USART0_UDRE_vect) +{ + //caratère suivant + UDR0 = tab_input_dmx[index_output_pc]; + index_output_pc++; + if (index_output_pc>532) {cbi(UCSR0B, UDRIE0); +} +} + +ISR(TIMER2_OVF_vect) { +if (index_output_dmx==-2) { + digitalWrite(tx1pin, HIGH); // on met la broche 18 à 1 pendant 10 µs + //digitalWrite(ledPin, HIGH); + index_output_dmx=-3; + TCNT2 = mab_timer_start ;// RAZ compteur timer + TIFR2 = 0 ; //Clear Flags timer + return; +} +if (index_output_dmx==-3) { + sbi(UCSR1B, TXEN1); // on redémarre la transmission + index_output_dmx=0; // on se prépare à émmettre à partir du stat code ( 513 octets ) + sbi(UCSR1B, UDRIE1); // on réactive l'intéruption du registre + TIMSK2 = 0 ; //desactivation intéruption A &B + TCCR2B = 0; + +} +} + + +void setup() { + // initialisation du stat code à 0 + tab_input_pc[0]=0; + + // initialisation de la liaison série pc à 1MB/s + Serial.begin(460800); + + // initialisation à 250k de serial 1 + // baudrate + UCSR1A = 1 << U2X1; + UBRR1H=0; + UBRR1L = 7; + + // 2 bit de stop; pas de parité; 8 bits de données + UCSR1C = 14; + + // activation transmission et intéruption serial 1 + sbi(UCSR1B, RXEN1); //Reception + sbi(UCSR1B, TXEN1); //Transmission + sbi(UCSR1B, RXCIE1); //Interruption sur reception + sbi(UCSR1B, TXCIE1); //Interruption pour fin de transmission +sbi(UCSR0B, RXEN0); //Reception +sbi(UCSR0B, RXCIE0); //Interruption sur reception +sbi(UCSR0B, TXEN0); //Transmission + // Init convertion analogique pour le premier canal pin0 + ADCSRB = (ADCSRB & ~(1 << MUX5)); + ADMUX = 96; //01100000 reférence sur le 5v et alignement pour lecture sur 8 bits + sbi(ADCSRA, ADSC); + + // init timer break + TIMSK2 = 0; + TCCR2B = 0; + TCCR2A = 0; // 00000000 + ASSR &= ~(1< 2µs + } // fin du break + + // suite du code + tab_input_dmx[513]= PINA; + tab_input_dmx[514]= PINC; + tab_input_dmx[515]=0; + tab_input_dmx[516]=0; + if ( ! bit_is_set(ADCSRA, ADSC)) { + //digitalWrite(ledPin, HIGH); + tab_input_dmx[517+cpt_ana++]=ADCH; + if (cpt_ana>15) cpt_ana=0; + ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((cpt_ana >> 3) & 0x01) << MUX5); + ADMUX = 96 | (cpt_ana & 0x07); + sbi(ADCSRA, ADSC); + } +} + +