Hello,
I’m new to this forum but I have been tinkering with the Arduino for a while.
I have built a library that deals with Ethernet and UDP, and when I try to initialize the class the setup function won’t run. Here’s a small test I ran to figure out whats going wrong:
If memory serves me right it does not work to call functions like UDP.begin() or pinMode() from your constructor because the hardware is not completely initialized when the constructor is called. Search the forums, this is a well-worked topic.
Perhaps if you add a .begin() method and do the work there...
Ok, I’ve created a new method in the class “.start” to solve the multiple Udp initializations, and Ive commented out the two “for” loops in the class, and it still isn’t displaying anything in serial.
Class code as an update:
#include "Arduino.h"
#include "Artnet2.h"
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
Artnet2::Artnet2()
{
bool matchArtnet = true; // Artnet global varification
char _artnetHeader[] = "Art-Net";// for Artnet varification
int _channel_position = 1;
int _number_of_channels = 512;
}
//find a way to integrate artnet.start in next version
void Artnet2::start()
{
pinMode(3,OUTPUT);
Udp.begin(6454);
}
bool Artnet2::read() //reads UDP packet to Artnet Buffer
{
Udp.parsePacket();
Udp.read(_AnetBuffer,1024);
/*for (int i=0; i<7; i++) {
if (_AnetBuffer[i]!=_artnetHeader[i])
{
matchArtnet = false;
break;
}
}
return (matchArtnet);*/
}
int Artnet2::interpret() //Decides if its a DMX information or Artnet poll
{
_Opcode=byte(_AnetBuffer[9]);
if(_Opcode==0x5000)
{
_opcode_is = 1;
}
else if(_Opcode==0x2000)
{
_opcode_is = 2;
}
return (_opcode_is);
}
void Artnet2::send() //sends Artpoll reply
{
//send reply somehow update in next version
}
void Artnet2::dMX() //Outputs DMX signal read from Artnet Buffer on pin3
{
/*for(int i=_channel_position-1;i< _number_of_channels;i++)//channel position
{
analogWrite(3,_AnetBuffer[i+17]);
}*/
}
I have an uno, however I am only making one instance of this class when this program is run and the buffers need to be that large to hold the information being sent. I wanted to build a library to see if I could.
Do you guys suggest something else?
I understand this is only an initial version of the library, but at the moment it seems it only does the following:
check for a header (whose content, I assume, is outside your control, but is 7 byte long);
check whether is a poll request, in which case send a reply;
or, alternatively, relay the packet through an analog port (presumably a DMX chain).
I see nothing in your code indicating a change of state in the Arduino, or the need to keep track of the configuration. If so (I'm exaggerating), you wouldn't need big-sized variables. How much RAM you'll really need will be determined by how complex are the states you'll need to manage, if any.
The main issue is buffering, because you'll be receiving data from Ethernet and sending it through an analog port. You'll need to rework your code, but after reading the first bytes (the header and the 'command'), you may enter a cycle of reading short chunks of data from Udp and writing them to analog, recycling the buffer (let's say a 256 byte array). You need to figure out whether the speed and timeout values allow you to make this reliably using a small buffer size. Maybe you could give us some background on these features.
Art-Net is a ethernet protocol for entertainment lighting. It carries in it a DMX chain, I would like to convert the Art-Net protocol to a DMX protocol. from what I can tell the DMX signal isn't encoded any farther in Art-Net then past all of the header stuff.
basically I would like to have a library that:
verifies packet received is Art-Net. (the first part of the Art-Net protocol sends "Art-Net")
interprets it between an Artpoll or DMX information
and sends via analog pin to DMX