Ajout des evenements sur entrée DMX

This commit is contained in:
manu 2014-01-19 16:21:02 +00:00
parent 636106176a
commit 70bc875ea1

View file

@ -2,18 +2,19 @@ using System;
using System.Threading;
using System.IO.Ports;
using System.Xml;
using System.Collections.Generic;
namespace DMX2
{
public class DriverBoitierV2 : DriverDMX//, IEventProvider
public class DriverBoitierV2 : DriverDMX, IEventProvider
{
struct buttonState {
public buttonState(byte _button, bool _pressed){
button=_button; pressed=_pressed;
struct dmxState {
public dmxState(int _dmx, byte _value){
value=_value; dmx=_dmx;
}
public byte button;
public bool pressed;
public int dmx;
public byte value;
}
enum etatAutomate {
@ -313,6 +314,8 @@ namespace DMX2
if(serial.BytesToRead>0)
serial.ReadExisting ();
ProcessData();
compteErreur= 0;
} catch (Exception ex) {
Console.WriteLine(serial.BytesToRead);
@ -382,6 +385,16 @@ namespace DMX2
etat = etatAutomate.Erreur;
}
void ProcessData ()
{
lock(watchdmx)
foreach (int dmx in watchdmx) {
if( inputbuffer[dmx-1]!= lastVal[dmx]){
lastVal[dmx] = inputbuffer[dmx-1];
eventsPending.Enqueue(new dmxState(dmx,inputbuffer[dmx-1]));
}
}
}
public override void Dispose ()
{
@ -403,25 +416,33 @@ namespace DMX2
}
#endregion
/*
#region IEventProvider implementation
List<int> watchdmx = new List<int>();
Dictionary<int,byte> lastVal = new Dictionary<int, byte>();
static System.Text.RegularExpressions.Regex regexEventID = new System.Text.RegularExpressions.Regex(
@"BV2-B(?<button>\d+)?",
@"BV2-D(?<dmx>\d+)?",
System.Text.RegularExpressions.RegexOptions.Compiled);
System.Collections.Concurrent.ConcurrentQueue<buttonState> eventsPending =
new System.Collections.Concurrent.ConcurrentQueue<buttonState>();
System.Collections.Concurrent.ConcurrentQueue<dmxState> eventsPending =
new System.Collections.Concurrent.ConcurrentQueue<dmxState>();
bool IEventProvider.Bind (string eventId)
{
var res = regexEventID.Match (eventId);
if (res.Success) {
int bt = int.Parse (res.Groups ["button"].Value);
if(bt<0||bt>7) return false;
watchButtons[bt] = true;
int dmx = int.Parse (res.Groups ["dmx"].Value);
if(dmx<0||dmx>511) return false;
lock(watchdmx){
if(!watchdmx.Contains(dmx))
{
watchdmx.Add(dmx);
lastVal[dmx] = 0;
}
}
return true;
}
return false;
@ -431,9 +452,10 @@ namespace DMX2
{
var res = regexEventID.Match (eventId);
if (res.Success) {
int bt = int.Parse (res.Groups ["button"].Value);
if(bt<0||bt>7) return ;
watchButtons[bt] = false;
int dmx = int.Parse (res.Groups ["dmx"].Value);
if(dmx<1||dmx>512) return;
lock(watchdmx) watchdmx.Remove (dmx);
return;
}
return;
}
@ -442,28 +464,47 @@ namespace DMX2
{
Gtk.Menu retmenu = new Gtk.Menu ();
Gtk.MenuItem evmenuitem = new Gtk.MenuItem ("Boutons");
Gtk.MenuItem evmenuitem = new Gtk.MenuItem ("Entrée DMX");
retmenu.Add (evmenuitem);
Gtk.Menu evmenu = new Gtk.Menu ();
evmenuitem.Submenu = evmenu;
for (int i= 0; i<8;i++ ) {
Gtk.MenuItem item = new Gtk.MenuItem(string.Format("Bouton {0}",i+1));
item.Data[EventManager.EventIdKey] = string.Format("BV2-B{0}",i);
item.Data[EventManager.StateKey] = state;
item.ButtonPressEvent += handler;
evmenu.Add (item);
for (int c = 0; c<512; c+=100) {
Gtk.MenuItem citem = new Gtk.MenuItem(string.Format("De {0} à {1}", c==0?1:c, c==500?512:c+99 ));
Gtk.Menu cmenu = new Gtk.Menu();
citem.Submenu = cmenu;
evmenu.Add(citem);
for(int d=0 ; d<100;d+=10){
if(c+d>512) break;
Gtk.MenuItem ditem = new Gtk.MenuItem(string.Format("De {0} à {1}", c+d==0?1:c+d, c+d==510?512:c+d+9 ));
Gtk.Menu dmenu = new Gtk.Menu();
ditem.Submenu = dmenu;
cmenu.Add(ditem);
for(int u=0; u<10;u++){
if(c+d+u==0) continue;
if(c+d+u>512) break;
Gtk.MenuItem uitem = new Gtk.MenuItem(string.Format("Entrée DMX {0}",c+d+u));
uitem.Data[EventManager.EventIdKey] = string.Format("BV2-D{0}",c+d+u);
uitem.Data[EventManager.StateKey] = state;
uitem.ButtonPressEvent += handler;
dmenu.Add (uitem);
}
}
}
return retmenu;
}
void IEventProvider.ProcessEvents (EventManagerCallback callback)
{
buttonState bt;
dmxState dmxs;
EventData evd;
while (eventsPending.TryDequeue(out bt)) {
evd.id= string.Format("BV2-B{0}",bt.button );
evd.value = bt.pressed?(byte)0xFF:(byte)0x00;
while (eventsPending.TryDequeue(out dmxs)) {
evd.id= string.Format("BV2-D{0}",dmxs.dmx );
evd.value = dmxs.value;
callback(evd);
}
}
@ -474,7 +515,7 @@ namespace DMX2
}
}
#endregion
*/
#region implemented abstract members of DMX2.DriverDMX
public override void Save (System.Xml.XmlElement parent)
{