Show Posts
Pages: 1 2 [3] 4 5 ... 11
31  Using Arduino / Programming Questions / Re: Questions about class syntax. on: February 01, 2014, 11:51:58 am
Thanks PaulS, I didn't see your first post until I was done writing my last post. Got it, constructor has same name as class.  smiley Another question, just out of curiosity. Consructors have no return type (not even void), but methods/functions must, correct?

Thanks,
DJ
32  Using Arduino / Programming Questions / Re: Questions about class syntax. on: February 01, 2014, 11:11:08 am
@pylon,

Thanks for your reply. I didn't assume it was a constructor, it was a guess. However, I did make that guess based on the empty brackets because I couldn't think of a reason for a method/function that does nothing. Now I know that empty brackets doesn't necessarily mean it does nothing. After taking another look, would it be correct to assume that it's a constructor because it has no return type?

@pYro_65

Thanks to you too. I'm learning some very useful tips here  smiley. I know things like this will come in handy for coding future libraries. So, if I understand correctly, x(y) is an assignment of y to x? In a constructor, a colon after the parameter list denotes an initializer list, correct? Why isn't it called an assignment list? Aren't all of the variables (in this case) initialized in the parameter list and outside the method, or is that instantiation of the variables?
33  Using Arduino / Programming Questions / Questions about class syntax. on: February 01, 2014, 09:40:35 am
First, thanks in advance for any replies. I'm not having a problem with programming (at the moment  smiley-roll). I'm just curious and I like to learn things. So, I was going through the VoltageMeter library and noticed a couple things I hadn't seen before. I thought perhaps someone here could explain.

Here's the library (I removed the preceding comments to save space),
VoltageMeter.h
Code:
#ifndef VOLTAGE_METER_INC
#define VOLTAGE_METER_INC

#include "WProgram.h"
#include "analog_reference.h"

class VoltageMeter {
  byte _pin;
  float _vMax;
 
public:
  VoltageMeter(byte pin, float vMax)
    : _pin(pin), _vMax(vMax)
  {}
 
  int analogReadVoltage(int nSamples = 1) const {
    byte currentReference = getAnalogReference();
    setAnalogReference(INTERNAL);
    float sum = 0;
    for (int i=0; i< nSamples; i++)
      sum += analogRead(_pin);
    setAnalogReference(currentReference);
    return (sum / nSamples);
  }
 
  float voltage(int nSamples = 1) const {
    return analogReadVoltage(nSamples) / 1023.0 * _vMax;
  }
};

#endif

My first question, is VoltageMeter(byte pin, float vMax): _pin(pin), _vMax(vMax) {} listed under public: a constructor or a method? It looks like a constructor to me because there's nothing inside the brackets {}. Next question, what does : _pin(pin), _vMax(vMax) do? I've never seen variableX(variableY) before. I don't think it's a cast like int(variable) because a: neither is a type, and b: both variables used are of the same type. Last question, what does the const just before the opening bracket of the methods do, and why is it placed before the bracket as opposed to somewhere else?

Thanks again for any replies,
DigitalJohnson
34  Using Arduino / Programming Questions / PROGMEM question on: February 28, 2013, 11:04:47 pm
I know storing strings in flash memory saves on RAM, but does the same hold true for numeric constants?

For example:
Code:

// Say you have a function that takes four numbers

void foo(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
{
    /* do something here */
}

// Then for whatever reason it's used in a loop with data from an array

const uint8_t array[3][4] =
{
    {240, 54, 12, 53},
    {122, 8, 59, 22},
    {8, 0, 255, 127}
};

for (uint8_t i = 0; 3 > i; i++)
{
    foo(array[i][0], array[i][1], array[i][2], array[i][3]);
}

// This example is small, but let's say the array is much larger and with more loop iterations


Is there any benefit in putting the array in flash memory via PROGMEM, or is the array already stored there because it's a const data type?
Would it make a difference if it was not a const data type?

Thanks for any replies,
DJ
35  Products / Arduino Due / A question about the two USB jacks. on: February 18, 2013, 04:44:17 am
I recently received my DUE smiley-grin and I have a couple micro USB cables on their way. I was just looking over the board and noticed that the USB slots are shaped just slightly different. The native port is rectangular and the programming port id "D" shaped. Will I need two different USB plugs?

Thanks for any replies,
DigitalJohnson
36  Using Arduino / Storage / Re: Very large bitmap to SD problem. [Solved] on: February 18, 2013, 04:36:21 am
Thanks PeteA,
  Actually, I figured it out yesterday and as you stated I needed to transfer the .raw image file to the card. Previously I was putting a .c file on the card. Thanks for the reply anyway.

DJ
37  Using Arduino / Storage / Very large bitmap to SD problem. on: February 15, 2013, 12:02:22 pm
I have a 5" TFT (800x480) w/ SD card (128MB), a Mega 1280 and a Mega 2560 (DUE on the way). I want to save a full screen bitmap to the SD card. However, none of the arduino's have enough memory (RAM or Flash) to store the bitmap to write it to the card. I noticed if I write a small bitmap to the card with the arduino and also write the same file to the card with my PC, it is not the same format. The data in the two files are different. Is there a way I can write a large bitmat to the card with my PC and be able to read the file with the arduino? I have very little experience with SD cards, but I learn quickly.

Thanks for any help,
DJ
38  Development / Other Software Development / Re: External Memory Interface library for the Mega. (can someone review my code?) on: December 31, 2012, 08:37:19 pm
@slomobile
I looked at the data sheet for the AT90USB1286 and the register names are the same so it should work.

DJ
39  Development / Other Software Development / External Memory Interface library for the Mega. (can someone review my code?) on: December 07, 2012, 10:30:00 am
  I've just finished a library to use the External Memory Interface module on the Mega1280/2560. You can use it to access external memory via parallel address/data bus. The address bus width is from 8-bits upto 16-bits and the data bus width is 8-bits. You'll need a 74AHC573 transparent latch to multiplex the Port A address/data lines. I've tested the library with ti's bq3287EA RTC and an Atmel AT28HC256 EEPROM both connected. I could access the RTC as the lower sector and the EEPROM as the upper sector. The library has four functions; enable(), disable(), read() write(). The library can read/write to any valid data type (including string literals) to any external memory address. The lirary zip file is on google code here: http://code.google.com/p/arduino-exmem-library/downloads/list and attached below. If anyone can review my code or better yet test it out and let me know what they think, I would apreciate it. Enjoy! smiley-wink

Thanks for your time,
DigitalJohnson
40  Using Arduino / Programming Questions / Re: Using templates in a library (SOLVED) on: December 06, 2012, 06:33:22 am
@tuxduino
All I need to do is e.g.:
Code:
#include <ExMem.h>

void setup()
{
    Serial.begin(115200);
    ExMem.enable(8, 0x0000, 0, 0);
    char* textToMemory = "Hello World!";
    char* textFromMemory;
    ExMem.write(LOWER, 0x0000, textToMemory); // Will return size of pointer(2).
    ExMem.read(LOWER, 0x0000, textFromMemory); // Will return size of pointer(2).
    Serial.println(textFromMemory);
}

void loop()
{
}

// Will print "Hello World!" to the serial monitor.

The library is on google code here: http://code.google.com/p/arduino-exmem-library/downloads/list. If anyone has the time can you please review or better yet test the library and let me know what you think.

Thanks for your time,
DigitalJohnson
41  Using Arduino / Programming Questions / Re: Using templates in a library (SOLVED) on: December 03, 2012, 07:57:44 pm
@PaulS
I suspected it had to do with the pointer to the string. Thanks for the reply.

@tuxduino
With this library I only need to know the location of the first address of the string. The write function, or I believe the string literal variable I pass to the function already adds the terminating null for me and the function writes all of it to memory. All I have to do to read the string back is pass the read function the first address of the string and an empty string literal variable to hold it. The read function puts every byte from the first address to the null byte into the variable. I only need to know the length of the string when writing to keep track of where the next unused address is at for the next write to avoid overlap.

Thanks guys, you've help me a lot,
DJ

BTW How can I add karma for you.

Never mind, just click on karma. Duh. smiley-roll
42  Using Arduino / Programming Questions / Re: Using templates in a library (SOLVED) on: December 03, 2012, 05:56:49 pm
I've found a small bug. If I pass data type char* to the write function, say char *c1 = "hello world", it will return with a value of 2, meaning it wrote 2 bytes. Then I pass the read function a variable of char*, say char *c2, and read from the same address written to. It also returns with 2, meaning it read 2 bytes. But when I use Serial.print(c2), It prints "hello world" as it should. Why is it returning a value of 2 when it clearly is more. Float returns 4, as do double, long long returns 8, and uint8_t = 1 and uint16_t = 2, all as expected. So what's with char*?

Can anyone shed some light on this?

DJ
43  Using Arduino / Programming Questions / Re: Using templates in a library (SOLVED) on: December 03, 2012, 12:36:32 pm
After I get all the comments back in I'll post it on google code. Enjoy.

DJ
44  Using Arduino / Programming Questions / Re: Using templates in a library (solved(almost)) on: December 03, 2012, 12:24:03 pm
@tuxduino

 smiley-grin Thank you very much! It finally works. As for the double cast, I'm not sure. Copied it from EEPROMWriteAnything. You've been a big help. If there's anything I can do for you...

Thanks again,
DigitalJohnson

PS. I'll share for anyone that can use it.

External Memory library

ExMem.h
Code:
#ifndef EXMEM_H
#define EXMEM_H

#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

#include <inttypes.h>
#include <avr/io.h>

class exmem
{
    public:
    exmem();
    void enable(uint8_t addressWidth = 16,
    uint16_t lowerLimit = 0x0000,
    uint8_t upperSectorWaitState = 0x00,
    uint8_t lowerSectorWaitState = 0x00);
    void disable(void);
    template <class TYPE> uint16_t write(bool sector, uint16_t address, const TYPE &data);
    template <class TYPE> uint16_t read(bool sector, uint16_t address, TYPE &store);
};


extern exmem ExMem;

static uint16_t upperSectorOffset = 0x0000;
static uint16_t upperLimit = 0xFFFF;
static uint16_t upperSize = 0xDE00;
static uint16_t lowerSize = 0xDE00;

exmem::exmem()
{
}

void exmem::enable(uint8_t addressWidth, uint16_t upperSectorBoundry,
uint8_t upperSectorWaitState, uint8_t lowerSectorWaitState)
{
    switch (addressWidth)
    {
        XMCRB &= 0xF8;
        case 8:
        upperLimit = 0x00FF;
        XMCRB = 0x07;
        break;
        case 10:
        upperLimit = 0x03FF;
        XMCRB = 0x06;
        break;
        case 11:
        upperLimit = 0x07FF;
        XMCRB = 0x05;
        break;
        case 12:
        upperLimit = 0x0FFF;
        XMCRB = 0x04;
        break;
        case 13:
        upperLimit = 0x1FFF;
        XMCRB = 0x03;
        break;
        case 14:
        upperLimit = 0x3FFF;
        XMCRB = 0x02;
        break;
        case 15:
        upperLimit = 0x7FFF;
        XMCRB = 0x01;
        break;
        default:
        upperLimit = 0xFFFF;
        break;
    }
    uint16_t boundry;
    if (addressWidth < 15)
    {
        boundry = 0x0000;
    }
    else if (addressWidth < 16)
    {
        boundry = constrain(upperSectorBoundry, 0x0000, 0x6000);
    }
    else
    {
        boundry = upperSectorBoundry;
    }
    XMCRA &= 0x8F;
    switch (boundry)
    {
        case 0x4000:
        upperSectorOffset = 0x1E00;
        XMCRA |= 0x20;
        break;
        case 0x6000:
        upperSectorOffset = 0x3E00;
        XMCRA |= 0x30;
        break;
        case 0x8000:
        upperSectorOffset = 0x5E00;
        XMCRA |= 0x40;
        break;
        case 0xA000:
        upperSectorOffset = 0x7E00;
        XMCRA |= 0x50;
        break;
        case 0xC000:
        upperSectorOffset = 0x9E00;
        XMCRA |= 0x60;
        break;
        case 0xE000:
        upperSectorOffset = 0xBE00;
        XMCRA |= 0x70;
        break;
        default:
        upperSectorOffset = 0x0000;
        break;
    }
    if (0 < upperSectorOffset)
    {
        lowerSize = upperSectorOffset;
        upperSize = ((1 + upperLimit) - (0x2200 + upperSectorOffset));
    }
    else
    {
        lowerSize = upperSize = ((1 + upperLimit) - 0x2200);
    }
    switch (upperSectorWaitState)
    {
        XMCRA &= 0xF3;
        case 1:
        XMCRA |= 0x04;
        break;
        case 2:
        XMCRA |= 0x08;
        break;
        case 3:
        XMCRA |= 0x0C;
        break;
        default:
        break;
    }
    switch (lowerSectorWaitState)
    {
       
        XMCRA &= 0xFC;
        case 1:
        XMCRA |= 0x01;
        break;
        case 2:
        XMCRA |= 0x02;
        break;
        case 3:
        XMCRA |= 0x03;
        break;
        default:
        break;
    }
    XMCRA |= 0x80;
}

void exmem::disable(void)
{
    XMCRA &= 0x7F;
}

template <class TYPE>
uint16_t exmem::write(bool sector, uint16_t address, const TYPE &data)
{
    uint8_t *pExtMemory = reinterpret_cast <uint8_t *> (0x2200);
    const uint8_t *pData = (const uint8_t *)(const void *)&data;
    uint16_t i;
    i = 0;
    if (sector)
    {
        if (address >= upperSize)
        {
            return i;
        }
    }
    else
    {
        if (address >= lowerSize)
        {
            return i;
        }
    }
    for (; i < sizeof(data); i++)
    {
        if (sector)
        {
            uint8_t *pMemory;
            pMemory = (uint8_t *)(pExtMemory + upperSectorOffset + address++);
            *pMemory = (uint8_t)(*pData++);
        }
        else
        {
            uint8_t *pMemory;
            pMemory = (uint8_t *)(pExtMemory + address++);
            *pMemory = (uint8_t)(*pData++);
        }
    }
    return i;
}

template <class TYPE>
uint16_t exmem::read(bool sector, uint16_t address, TYPE &store)
{
    uint8_t *pExtMemory = reinterpret_cast <uint8_t *> (0x2200);
    uint8_t *pStore = (uint8_t *)(void *)&store;
    uint16_t i;
    i = 0;
    if (sector)
    {
        if (address >= upperSize)
        {
            return i;
        }
    }
    else
    {
        if (address >= lowerSize)
        {
            return i;
        }
    }
    for (; i < sizeof(store); i++)
    {
        if (sector)
        {
            uint8_t *pMemory = (pExtMemory + upperSectorOffset + address++);
            *pStore++ = (uint8_t)(*pMemory);
        }
        else
        {
            uint8_t *pMemory = (pExtMemory + address++);
            *pStore++ = (uint8_t)(*pMemory);
        }
    }
    return i;
}

exmem ExMem = exmem();

#endif
45  Using Arduino / Programming Questions / Re: Using templates in a library (solved(almost)) on: December 03, 2012, 11:28:36 am
If I break up the uint16_t type into two uint8_t types and write them seperately. It will read back in a uint16_t type as it should. So, the problem is in the write function. I've almost got it. I'll test some more and post back.

DJ 
Pages: 1 2 [3] 4 5 ... 11