Show Posts
Pages: 1 2 [3] 4 5 ... 9
31  Using Arduino / Storage / Re: file read times depending on directory file count? on: June 16, 2012, 04:05:30 pm
splendid smiley
32  Using Arduino / Storage / Re: file read times depending on directory file count? on: June 16, 2012, 08:25:09 am
Hi fatlib

I've tried calling file.rewind() after openNext has reached the end of the file list but it doesn't return the beginning. What am I doing wrong?

this code should just keep looping through files

Code:
while(1)
  {
      // open next file in root.  The volume working directory, vwd, is root
      while (file.openNext(sd.vwd(), O_READ)) {
        file.getFilename(name);
        cout << name << endl;
        file.close();
      }
   
      file.rewind();
 
  }
33  Using Arduino / Storage / Re: file read times depending on directory file count? on: June 13, 2012, 07:48:53 pm
Nice, so I should be able to remove my filename code too. 

Does OpenNext() loop when it reaches the last file?

One problem though, I need to cycle through something in the order of 5000 files. As I understand it, sdfat supports only fat16, which only allows 512 files per folder?

Is this irrelevent though if I'm using OpenNext() ? Can I just break up my files into folders with 512 each, and OpenNext() will skip between them all regardless?

34  Using Arduino / Storage / Re: file read times depending on directory file count? on: June 13, 2012, 06:32:44 pm
Yes I can create them in the order to be used. At the moment I'm just using the standard sd library as you can see above. Is there much involved in converting what I've done to fat16lib and using the sequential technique?  It sounds perfect for my needs.
35  Using Arduino / Storage / file read times depending on directory file count? on: June 13, 2012, 09:57:11 am
Hey. I'm using the following code to open files from an sd card in sequence, read the data, and send it to a dmx device.  It's working fine (<20ms between each file opening) with a small (around 100 or less) number of files, but when I increase the number of files the read speed increases dramatically.

What's actually happening now, with 900 files, is the first 10 or so files take ~300 ms each to open, then the next file opens in~70ms, then each consecutive file takes slightly longer to open, until it takes ~500 ms for each file to open.

Can anyone explain why this is happening, and how to get around it?

thanks

Code:

//   #define MEGA_SOFT_SPI 1 must be set in 2d3card.h to use dmx shield sd !!!!!

//   DmxSimple.h must be edited to include Arduino.h, not Wiring.h !!!!

#include <SD.h>
#include <DmxSimple.h>



File myFile;

unsigned long millisPre;
unsigned long millisPost;
unsigned long m1;
unsigned long m2;
int testLoopCount;

void setup()
{

  DmxSimple.usePin(2);
  DmxSimple.maxChannel(3);
  Serial.begin(57600);
  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
   pinMode(53, OUTPUT);
   
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
 

   
  unsigned char testBuff[513];

 
  Serial.println("beginning read");
  millisPre = millis();
  //Serial.println(millisPre);
   
 
 
//  testLoopCount = 1;
//  for(int cycles = 0; cycles < testLoopCount; cycles++)
// 
 
  while(1)
  {
    static char filename[] = "fr00000#.txt";
    static char filepath[] = "frames/";
    filename[7] = '#';
    filename[6] = '0';
    filename[5] = '0';
    filename[4] = '0';
 
   
     
    for (int thous = '0'; thous <= '9'; thous++)
    {
        filename[4] = thous;
       
        for (int hnds = '0'; hnds <= '9'; hnds++)
        {
            filename[5] = hnds;
           
            for (int tens = '0'; tens <= '9'; tens++)
            {
                filename[6] = tens;
               
                for (int ones = '0'; ones <='9'; ones++)
                {
                  filename[7] = ones;
                 
                  m1 = millis();
             
           
                    // open the file for reading:
                    myFile = SD.open(filename);
                    //myFile = SD.open(strcat(filepath,filename));
                    //myFile = SD.open(filename);
                    if (myFile)
                    {
                        Serial.println(filename);
                       
                        // read from the file until there's nothing else in it:
                        int byteCount = 1;
                        while (myFile.available())
                        {
                           
                            DmxSimple.write(byteCount, (int)myFile.read());
                            //testBuff[byteCount] = myFile.read();
                            //testBuffInt[byteCount] =(int)testBuff[byteCount];
                            byteCount++; 
                     
                        }
                        //testBuff[byteCount] = NULL;
       
                        ///////////////
                       
                       
                        // close the file:
                        myFile.close();
                    }
                    else
                    {
                      // if the file didn't open, print an error:
                      //Serial.print("error opening ");
                      //Serial.print(filename);
                      //Serial.println(" test.txt");
                      {goto bailout;}
                    }
                   
                   
                    m2 = millis()-m1;
                    Serial.print(" ");
                    Serial.println(m2);
                    m1 = m2;
                 
                }
                //filename[6]++;
                 
            }
        }
    }
    bailout:;

  }


   
 
   Serial.println();
   millisPost = millis();
   //Serial.println(millisPost);
   Serial.println((millisPost-millisPre));
   

   
   
}



void loop()
{
// nothing happens after setup
}
36  Using Arduino / Programming Questions / dmx simple speed issues on: June 08, 2012, 01:50:04 pm
Hi, I'm having trouble sending any large number ( 60+) of dmx channels simultaneously at a decent framerate (30+fps).  The library can supposedly handle the full 512 channel count of the dmx512 standard. 

I'm using a mega2560 and a dmx shield.

With this I'm using dmxsimple.cpp. The function to update a channel - DmxSimple.write(channel, brightness); only allows you to update one channel at a time, then another function in the lib updates the 'frame' with this new value. Is it possible to modify the library to allow the whole buffer array to be updated first, then the update function be called?

(I may have misunderstood how this is working btw)

Here is the code for dmxsimple.cpp. Hope you can help!...

Code:
/**
 * DmxSimple - A simple interface to DMX.
 *
 * Copyright (c) 2008-2009 Peter Knight, Tinker.it! All rights reserved.
 */
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "pins_arduino.h"

#include "Arduino.h"
#include "DmxSimple.h"

/** dmxBuffer contains a software copy of all the DMX channels.
  */
volatile uint8_t dmxBuffer[DMX_SIZE];
static uint16_t dmxMax = 16; /* Default to sending the first 16 channels */
static uint8_t dmxStarted = 0;
static uint16_t dmxState = 0;

static volatile uint8_t *dmxPort;
static uint8_t dmxBit = 0;
static uint8_t dmxPin = 3; // Defaults to output on pin 3 to support Tinker.it! DMX shield

void dmxBegin();
void dmxEnd();
void dmxSendByte(volatile uint8_t);
void dmxWrite(int,uint8_t);
void dmxMaxChannel(int);

/* TIMER2 has a different register mapping on the ATmega8.
 * The modern chips (168, 328P, 1280) use identical mappings.
 */
#if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega1280__)
#define TIMER2_INTERRUPT_ENABLE() TIMSK2 |= _BV(TOIE2)
#define TIMER2_INTERRUPT_DISABLE() TIMSK2 &= ~_BV(TOIE2)
#elif defined(__AVR_ATmega8__)
#define TIMER2_INTERRUPT_ENABLE() TIMSK |= _BV(TOIE2)
#define TIMER2_INTERRUPT_DISABLE() TIMSK &= ~_BV(TOIE2)
#else
#define TIMER2_INTERRUPT_ENABLE()
#define TIMER2_INTERRUPT_DISABLE()
/* Produce an appropriate message to aid error reporting on nonstandard
 * platforms such as Teensy.
 */
#warning "DmxSimple does not support this CPU"
#endif


/** Initialise the DMX engine
 */
void dmxBegin()
{
  dmxStarted = 1;

  // Set up port pointers for interrupt routine
  dmxPort = portOutputRegister(digitalPinToPort(dmxPin));
  dmxBit = digitalPinToBitMask(dmxPin);

  // Set DMX pin to output
  pinMode(dmxPin,OUTPUT);

  // Initialise DMX frame interrupt
  //
  // Presume Arduino has already set Timer2 to 64 prescaler,
  // Phase correct PWM mode
  // So the overflow triggers every 64*510 clock cycles
  // Which is 510 DMX bit periods at 16MHz,
  //          255 DMX bit periods at 8MHz,
  //          637 DMX bit periods at 20MHz
  TIMER2_INTERRUPT_ENABLE();
}

/** Stop the DMX engine
 * Turns off the DMX interrupt routine
 */
void dmxEnd()
{
  TIMER2_INTERRUPT_DISABLE();
  dmxStarted = 0;
  dmxMax = 0;
}

/** Transmit a complete DMX byte
 * We have no serial port for DMX, so everything is timed using an exact
 * number of instruction cycles.
 *
 * Really suggest you don't touch this function.
 */
void dmxSendByte(volatile uint8_t value)
{
  uint8_t bitCount, delCount;
  __asm__ volatile (
    "cli\n"
    "ld __tmp_reg__,%a[dmxPort]\n"
    "and __tmp_reg__,%[outMask]\n"
    "st %a[dmxPort],__tmp_reg__\n"
    "ldi %[bitCount],11\n" // 11 bit intervals per transmitted byte
    "rjmp bitLoop%=\n"     // Delay 2 clock cycles.
  "bitLoop%=:\n"\
    "ldi %[delCount],%[delCountVal]\n"
  "delLoop%=:\n"
    "nop\n"
    "dec %[delCount]\n"
    "brne delLoop%=\n"
    "ld __tmp_reg__,%a[dmxPort]\n"
    "and __tmp_reg__,%[outMask]\n"
    "sec\n"
    "ror %[value]\n"
    "brcc sendzero%=\n"
    "or __tmp_reg__,%[outBit]\n"
  "sendzero%=:\n"
    "st %a[dmxPort],__tmp_reg__\n"
    "dec %[bitCount]\n"
    "brne bitLoop%=\n"
    "sei\n"
    :
      [bitCount] "=&d" (bitCount),
      [delCount] "=&d" (delCount)
    :
      [dmxPort] "e" (dmxPort),
      [outMask] "r" (~dmxBit),
      [outBit] "r" (dmxBit),
      [delCountVal] "M" (F_CPU/1000000-3),
      [value] "r" (value)
  );
}

/** DmxSimple interrupt routine
 * Transmit a chunk of DMX signal every timer overflow event.
 *
 * The full DMX transmission takes too long, but some aspects of DMX timing
 * are flexible. This routine chunks the DMX signal, only sending as much as
 * it's time budget will allow.
 *
 * This interrupt routine runs with interrupts enabled most of the time.
 * With extremely heavy interrupt loads, it could conceivably interrupt its
 * own routine, so the TIMER2 interrupt is disabled for the duration of
 * the service routine.
 */
ISR(TIMER2_OVF_vect,ISR_NOBLOCK) {

  // Prevent this interrupt running recursively
  TIMER2_INTERRUPT_DISABLE();

  uint16_t bitsLeft = F_CPU / 31372; // DMX Bit periods per timer tick
  bitsLeft >>=2; // 25% CPU usage
  while (1) {
    if (dmxState == 0) {
      // Next thing to send is reset pulse and start code
      // which takes 35 bit periods
      uint8_t i;
      if (bitsLeft < 35) break;
      bitsLeft-=35;
      *dmxPort &= ~dmxBit;
      for (i=0; i<11; i++) _delay_us(8);
      *dmxPort |= dmxBit;
      _delay_us(8);
      dmxSendByte(0);
    } else {
      // Now send a channel which takes 11 bit periods
      if (bitsLeft < 11) break;
      bitsLeft-=11;
      dmxSendByte(dmxBuffer[dmxState-1]);
    }
    // Successfully completed that stage - move state machine forward
    dmxState++;
    if (dmxState > dmxMax) {
      dmxState = 0; // Send next frame
      break;
    }
  }
 
  // Enable interrupts for the next transmission chunk
  TIMER2_INTERRUPT_ENABLE();
}

void dmxWrite(int channel, uint8_t value) {
  if (!dmxStarted) dmxBegin();
  if ((channel > 0) && (channel <= DMX_SIZE)) {
    if (value<0) value=0;
    if (value>255) value=255;
    dmxMax = max((unsigned)channel, dmxMax);
    dmxBuffer[channel-1] = value;
  }
}

void dmxMaxChannel(int channel) {
  if (channel <=0) {
    // End DMX transmission
    dmxEnd();
    dmxMax = 0;
  } else {
    dmxMax = min(channel, DMX_SIZE);
    if (!dmxStarted) dmxBegin();
  }
}


/* C++ wrapper */


/** Set output pin
 * @param pin Output digital pin to use
 */
void DmxSimpleClass::usePin(uint8_t pin) {
  dmxPin = pin;
  if (dmxStarted && (pin != dmxPin)) {
    dmxEnd();
    dmxBegin();
  }
}

/** Set DMX maximum channel
 * @param channel The highest DMX channel to use
 */
void DmxSimpleClass::maxChannel(int channel) {
  dmxMaxChannel(channel);
}

/** Write to a DMX channel
 * @param address DMX address in the range 1 - 512
 */
void DmxSimpleClass::write(int address, uint8_t value)
{
dmxWrite(address, value);
}
DmxSimpleClass DmxSimple;



edit - I think actually the update function runs constantly, and only channels that have been changed between updates will be updated.  It seems awfully slow.  Might it be possible to update the dmxBuffer array (where channel info is stored) using something like memcopy?  This is a massively uneducated guess by the way smiley

edit2 - or maybe access dmxSendByte directly?
37  Using Arduino / Programming Questions / Re: Problem with DmxSimple.h on: June 08, 2012, 01:30:21 pm
Hi pylon. Are you getting reasonable speed with this setup?

I was hoping to be able to address all 512 channels at a decent fps, and while I can read from the sd fast enough for this, I can barely achieve 2 fps with dmx simple, even with all the sd stuff taken out!
38  Using Arduino / Storage / Re: SK Pang dmx / sd shield + mega2560 on: June 08, 2012, 12:08:41 pm
Whoa, it is working.

Was being a numpty smiley

Thanks


edit: seems it's way slower to open the file now though. I take it physically connecting to the hardware spi pins will be faster?
39  Using Arduino / Storage / Re: SK Pang dmx / sd shield + mega2560 on: June 08, 2012, 11:47:00 am
Ah, interesting.

Changed the line but no joy. 

Anything else to try or do I have to physically link the appropriate pins on the dmx shield to pins 50-53 on the mega?
40  Using Arduino / Storage / SK Pang dmx / sd shield + mega2560 on: June 08, 2012, 11:05:35 am
Hi, I'm having a strange issue with this sd shield, and have been trying different combinations, using the card info sd example -

Uno + Ethernet Shield - Chip Select(4), pinmode output(10) - working
Uno + SKP dmx/sd shield - Chip Select(10), pinmode output(10) - working
Mega2560 + Ethernet Shield - Chip Select(4), pinmode output(53) - working
Mega2560 + SKP dmx/sd shield - Chip Select(10), pinmode output(53) - NOT working..

With this last combo I've tried many combinations of CS and Pin Output values.  The dmx out of this shield is working fine, as does the sd when used with the Uno. Confusing!

Any ideas?
41  Using Arduino / Programming Questions / Re: reading text from an sd card on: June 07, 2012, 06:13:11 pm
Thanks Pylon, that's a good point about the array.

Turns out it's not freezing though (yet)  - I'd set it to only loop 30 times as a test. Duh
42  Using Arduino / Programming Questions / Re: reading text from an sd card on: June 07, 2012, 05:46:04 pm
Thanks Michael

I've actually got it working using ascii codes, running at 25 fps for a 512byte (and 512 value) file

Attached code shows it controlling a string of ws2801 led driver chips

Seems to be a memory leak though as it stops running after a few seconds.  So close smiley

Code:
#include <SD.h>
#include "SPI.h"
#include "Adafruit_WS2801.h"


int dataPin  = 2;    // Yellow wire on Adafruit Pixels
int clockPin = 3;    // Green wire on Adafruit Pixels

Adafruit_WS2801 strip = Adafruit_WS2801(170, dataPin, clockPin);



File myFile;

unsigned long millisPre;
unsigned long millisPost;
int testLoopCount;

void setup()
{
  strip.begin();
  // Update LED contents, to start they are all 'off'
  strip.show();
 
 
  Serial.begin(57600);
  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
   pinMode(53, OUTPUT);
   
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
 

   
  unsigned char testBuff[513];
  int testBuffInt[512];
 
  Serial.println("beginning read");
  millisPre = millis();
  //Serial.println(millisPre);
   
 
 
  testLoopCount = 30;
 
  for(int cycles = 0; cycles < testLoopCount; cycles++)
  {
   
    static char filename[] = "fr0#.txt";
 
   for (int tens = '0'; tens< '3'; tens++)
   {
     for (int ones = '0'; ones <='9'; ones++)
     {
        filename[3] = ones;
        // open the file for reading:
        myFile = SD.open(filename);
        if (myFile)
        {
          //Serial.println(filename);
         
          // read from the file until there's nothing else in it:
          int byteCount = 0;
          while (myFile.available())
          {
            testBuff[byteCount] = myFile.read();
            //testBuffInt[byteCount] =(int)testBuff[byteCount];
            byteCount++;
          }
          testBuff[byteCount] = NULL;
         
         
          //set ws2801 pixels
          for(int i=0;i<512;i++)//channel position
          {
            strip.setPixelColor(i,(int)testBuff[i*3],(int)testBuff[i*3+1],(int)testBuff[i*3+2]);
          }
          strip.show();
         
         
          // close the file:
          myFile.close();
        }
        else
        {
          // if the file didn't open, print an error:
          Serial.print("error opening ");
          Serial.print(filename);
          Serial.println(" test.txt");
        }
      }
        filename[2]++;
    }
   
    filename[2] = '0';
    filename[3] = '#';

   
  }
   Serial.println();
   millisPost = millis();
   //Serial.println(millisPost);
   Serial.println((millisPost-millisPre)/testLoopCount);
   

   
   
}



void loop()
{
// nothing happens after setup
}

43  Using Arduino / Programming Questions / Re: hex/byte to int conversion on: June 07, 2012, 05:17:06 pm
Code:
int convertFromHex(int ascii){
  if(ascii > 0x39) ascii -= 7; // adjust for hex letters upper or lower case
  return(ascii & 0xf);
}


but I'm trying to convert from a char array, of size 2, containing a hex in string form.... I don't get it
44  Using Arduino / Programming Questions / Re: hex/byte to int conversion on: June 07, 2012, 04:07:20 pm
Thanks guys, a stunning response!

Yes, "they're stored as their character representation" because they've just been read as characters from a text file

the ascii example works great.

However majenko's switch code to convert from hex gives me the following error -"invalid conversion from char* to unsigned char*"    Know what's up?

45  Using Arduino / Programming Questions / hex/byte to int conversion on: June 07, 2012, 03:17:54 pm
Hey, quick question.  How do I convert both of these into an int (which should have a value of 123) -

- a char[2] containing "7B"   (ie as hex)

and

- a char[1] containing "{"   (ie as an ascii byte)


thanks!
Pages: 1 2 [3] 4 5 ... 9