Cosa: An Object-Oriented Platform for Arduino programming

Hi sirhax. Seems like you missed the compile error. Just too many ripple errors after that initial error message. Need to figure out a way to get the Arduino build to stop at once on that error message.

sirhax:
In file included from E:\Portable Apps\arduino-1.0.3\hardware\arduino\cores\arduino/Cosa/Pins.hh:40,
from E:\Portable Apps\arduino-1.0.3\hardware\arduino\cores\arduino/Cosa/VWI.hh:37,
from CosaVWIreceiver.ino:34:
E:\Portable Apps\arduino-1.0.3\hardware\arduino\cores\arduino/Cosa/Board.hh:52:2: error: #error "Cosa/Board.hh: board not supported"

I haven't yet implemented ATmega32u4 based boards; Micro, Leronardo, etc. Need to implement the CDC/USB drivers. This will take a while as I want them to work together with IOStream and the Event handler. Might do an intermediate solution and reuse some of the Arduino code to get this up and running earlier than planned.

Fixed the comment :wink: It is easy to add review marks in github if you find more.

Cheers!

Thanks for the reply! I did see the "board not supported" error; I just didn't know if that was by design or not. :~
I will continue to keep an eye on the github commits :wink:

sirhax:
Thanks for the reply! I did see the "board not supported" error; I just didn't know if that was by design or not. :~
I will continue to keep an eye on the github commits :wink:

Thanks for your contributions! Even the smallest remark on inconsistency in documentation are of value.

BW: I recently implemented Pin Change Interrupt Handling (InterruptPin class in Pins.hh https://github.com/mikaelpatel/Cosa/blob/master/Cosa/Pins.hh) so that basically any pin can be used to detect change and issue an interrupt. This should be compared to the limited number of pins that may be used as External Interrupt Pins.

This might be of value for your ATtiny85 sketch. I haven't updated the CosaVWIkey.ino example sketch with Pin Change Interrupts instead of the External Interrupt Pin but you can find some example code in the CosaPins.ino Cosa/CosaPins.ino at master · mikaelpatel/Cosa · GitHub

Cheers!

Hi kowalski

I tried to grab doc.zip but got a "Error: blob is too big" error. Don't know if that's something at my end or Git's end.


Rob

Graynomad:
I tried to grab doc.zip but got a "Error: blob is too big" error. Don't know if that's something at my end or Git's end.

Hi Graynomad, seems to be a problem with downloading of single large files on github's end. I tried it on my machine with the same results as you got. The work-around would be to download the whole master zip archive for Cosa and then unzip the documentation.

https://github.com/mikaelpatel/Cosa/archive/master.zip

Or even better git clone and follow updates :slight_smile:

Cheers!

download the whole master zip archive

Oh, 50MB, maybe some other time, we don't all have unlimited data plans :slight_smile:

I have no immediate use for this because I only (well mostly) work with 32-bit CPUs these days, however if you port this to the Due I'll get much keener as we're designing a new version of it and the fresh hardware platform may like a fresh software platform.

I was thinking of doing one myself but I've got my hands full designing hardware and will do for quite some time I think.


Rob

Graynomad:
Oh, 50MB, maybe some other time, we don't all have unlimited data plans :slight_smile:

I have no immediate use for this because I only (well mostly) work with 32-bit CPUs these days, however if you port this to the Due I'll get much keener as we're designing a new version of it and the fresh hardware platform may like a fresh software platform.

Thanks for the observation on the download size. I have cleaned this mess up and got the master zip from github down from 50 M to 343 K ;-).

The documentation and references have been moved to Dropbox. They shouldn't have been in the repository at github from the start. Sorry about that!

You can now read the documentation online http://dl.dropbox.com/u/993383/Cosa/doc/html/index.html and download http://dl.dropbox.com/u/993383/Cosa/doc.zip

Full details on this update is available in the github README. GitHub - mikaelpatel/Cosa: An Object-Oriented Platform for Arduino/AVR

Cheers!

Some news on the latest improvements:

  1. New driver for the Honeywell HMC5883L 3-Axis Digital Compass with interface to all modes and settings. https://github.com/mikaelpatel/Cosa/blob/master/Cosa/TWI/Driver/HMC5883L.hh and http://dl.dropbox.com/u/993383/Cosa/doc/html/d0/d40/classHMC5883L.html

  2. Major improvement to the AT24CXX 2-Wire Serial EEPROM driver. Removed block size limitation. Page alignment and block write are now handled within the class. https://github.com/mikaelpatel/Cosa/blob/master/Cosa/TWI/Driver/AT24CXX.hh and http://dl.dropbox.com/u/993383/Cosa/doc/html/d2/db3/classAT24CXX.html

  3. Adding support for fast byte swap (16/32-bit) in Cosa/Types.h. Adding support for byte swap (16/32-bit). · mikaelpatel/Cosa@4247d75 · GitHub

  4. New Pin sub-class for pin change interrupt handling; class InterruptPin. Allows interrupt/event handling on changes on any Arduino pin http://dl.dropbox.com/u/993383/Cosa/doc/html/db/d05/classInterruptPin.html.

Don't forget to follow the blog.

Have fun!

More news on the latest improvements:

  1. New EEPROM class with device abstraction allowing handling of common data types. Default device is the internal EEPROM. https://dl.dropboxusercontent.com/u/993383/Cosa/doc/html/dc/d83/classEEPROM.html. See example sketch CosaEEPROM.ino Cosa/CosaEEPROM.ino at master · mikaelpatel/Cosa · GitHub

  2. Refactoring of AT24CXX device driver to implement EEPROM::Device. Support for AT24C32, -64, -128, -256 and -512. See example sketch CosaAT24CXX.ino; uses EEMEM attribute even for external EEPROM. https://github.com/mikaelpatel/Cosa/blob/master/examples/TWI/CosaAT24CXX/CosaAT24CXX.ino.

  3. Major update to the online documentation https://dl.dropboxusercontent.com/u/993383/Cosa/doc/html/index.html. Improved detailed description of the classes.

  4. Evaluation of different methods of coding bitfield access for C/C++ as alternative to the _BV() and bit set/clear macros (Bits.h). https://github.com/mikaelpatel/Cosa/blob/master/examples/Sandbox/CosaBitfields/CosaBitfields.ino. Bit-field struct definitions are useful for larger bit-fields (> 1 bit) and the compiler can produce better code than with shift/and/or-operators.

First version with shift/and/or-operators.

uint16_t
color16b(uint8_t red, uint8_t green, uint8_t blue)
{
  return ((((red >> 3) & 0x1f) << 11) | (((green >> 2) & 0x3f) << 5) | ((blue >> 3) & 0x1f));
}

00000284 <_Z8color16bhhh>:
     284:	66 95       	lsr	r22
     286:	66 95       	lsr	r22
     288:	70 e0       	ldi	r23, 0x00	; 0
     28a:	95 e0       	ldi	r25, 0x05	; 5
     28c:	66 0f       	add	r22, r22
     28e:	77 1f       	adc	r23, r23
     290:	9a 95       	dec	r25
     292:	e1 f7       	brne	.-8      	; 0x28c <_Z8color16bhhh+0x8>
     294:	46 95       	lsr	r20
     296:	46 95       	lsr	r20
     298:	46 95       	lsr	r20
     29a:	50 e0       	ldi	r21, 0x00	; 0
     29c:	64 2b       	or	r22, r20
     29e:	75 2b       	or	r23, r21
     2a0:	38 2f       	mov	r19, r24
     2a2:	38 7f       	andi	r19, 0xF8	; 248
     2a4:	20 e0       	ldi	r18, 0x00	; 0
     2a6:	62 2b       	or	r22, r18
     2a8:	73 2b       	or	r23, r19
     2aa:	cb 01       	movw	r24, r22
     2ac:	08 95       	ret

Second version with union bit-field data type and struct member access. Note the order of the fields. AVR is little-endian (LSB to MSB) in struct.

union color16_t {
  uint16_t rgb;
  struct {
    unsigned int blue:5;
    unsigned int green:6;
    unsigned int red:5;
  };
};

uint16_t
color16a(uint8_t red, uint8_t green, uint8_t blue)
{
  color16_t c;
  c.red = red >> 3;
  c.green = green >> 2;
  c.blue = blue >> 3;
  return (c.rgb);
}

000001e8 <_Z8color16ahhh>:
     1e8:	70 e0       	ldi	r23, 0x00	; 0
     1ea:	75 95       	asr	r23
     1ec:	67 95       	ror	r22
     1ee:	75 95       	asr	r23
     1f0:	67 95       	ror	r22
     1f2:	26 2f       	mov	r18, r22
     1f4:	26 95       	lsr	r18
     1f6:	26 95       	lsr	r18
     1f8:	26 95       	lsr	r18
     1fa:	27 70       	andi	r18, 0x07	; 7
     1fc:	98 2f       	mov	r25, r24
     1fe:	98 7f       	andi	r25, 0xF8	; 248
     200:	50 e0       	ldi	r21, 0x00	; 0
     202:	83 e0       	ldi	r24, 0x03	; 3
     204:	55 95       	asr	r21
     206:	47 95       	ror	r20
     208:	8a 95       	dec	r24
     20a:	e1 f7       	brne	.-8      	; 0x204 <_Z8color16ahhh+0x1c>
     20c:	4f 71       	andi	r20, 0x1F	; 31
     20e:	62 95       	swap	r22
     210:	66 0f       	add	r22, r22
     212:	60 7e       	andi	r22, 0xE0	; 224
     214:	86 2f       	mov	r24, r22
     216:	84 2b       	or	r24, r20
     218:	92 2b       	or	r25, r18
     21a:	08 95       	ret
  1. Refactoring of Canvas::color16_t handling to union with bit-field struct after the sandboxing. Improving coding style of bit-fields; Canvas color16_t handling. · mikaelpatel/Cosa@6f2e541 · GitHub

Don't forget to follow the blog. http://cosa-arduino.blogspot.se/

Have fun!

A new Cosa blog posting is now available. It presents the Cosa support for internal and external EEPROM.

Cheers!

Amazing amount of work here kowalski. I haven't looked too deep yet but it seems well documented as well.


Rob

Hi,
have you planed to make a power "vcc" class, with by example
int vcc = readVcc(); // with code below
setlowbat(VccMin, &functiontocall); // function to call when vcc down to VccMin

example code :

long readVcc() {
  long result;
  // Read 1.1V reference against AVcc
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);  //ATTINY85
//  ADMUX = _BV(MUX5) | _BV(MUX0);                                          //ATTINY84
//  ADMUX = ADMUX = (0<<REFS1) | (1<<REFS0) | (0<<ADLAR) | (1<<MUX3) | (1<<MUX2) | (1<<MUX1) | (0<<MUX0);  //ATMEGA328 168
// ADMUX = (0<<REFS1) | (1<<REFS0) | (0<<ADLAR)| (0<<MUX5) | (1<<MUX4) | (1<<MUX3) | (1<<MUX2) | (1<<MUX1) | (0<<MUX0);  // ATMEGA1280 2560
  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Convert
  while (bit_is_set(ADCSRA,ADSC));
  result = ADCL;
  result |= ADCH<<8;
  result = 1126400L / result; // Back-calculate AVcc in mV
  return result;
}

guillaume.

guiguid, that is a nice idea! Below is a snippet of an example sketch how to do the monitoring

class VCC : public Periodic {
private:
  uint16_t m_threshold;
  uint16_t m_vcc;
  virtual void run()
  {
    m_vcc = AnalogPin::bandgap();
    if (m_vcc > m_threshold) return;
    on_low_voltage();
  }
public:
  VCC(uint16_t mv, uint16_t ms = 1024) :
    Periodic(ms),
    m_threshold(mv),
    m_vcc(0)
  {}
  virtual void on_low_voltage()
  {
    trace << Watchdog::get_millis() / 1000 << ':' << m_vcc << PSTR(" mV\n");
  }
};

VCC lowPower(4900);

The callback, the virtual function on_low_voltage(), is called when the threshold is exceeded. The default implementation will print the measurement. Sub-class VCC and override the virtual function to implement your own action.

Please see the full the sketch Cosa/CosaVCC.ino at master · mikaelpatel/Cosa · GitHub

In a later update of Cosa I might bring the VCC class into the library.

Cheers!

Graynomad:
Amazing amount of work here kowalski. I haven't looked too deep yet but it seems well documented as well.

Rob, thanks for the encouragement!

Yes, Cosa is a lot of work but also a lot of fun. I did some changes to the documentation so hopefully you can get an overview easier. There are many components, drivers, etc, in Cosa, so some extra help navigating is needed. Please see the "Related Pages" tab http://dl.dropboxusercontent.com/u/993383/Cosa/doc/html/pages.html

Cheers!

A new Cosa blog posting is now available. It presents some of the Cosa support for 1-Wire devices (with/without parasite powering), the DS18B20 driver, battery monitoring and sending sensor data using the Cosa Virtual Wire Interface (VWI).

Cheers!

Big THANKS to guiguid for great suggestions and testing a lot of this code.

More good work.

I assume the 1-wire is handled with bit-banging the signal(s), any idea how much CPU time that takes?


Rob

Graynomad:
More good work.
I assume the 1-wire is handled with bit-banging the signal(s), any idea how much CPU time that takes?


Rob

Thanks!

Yes, the Cosa 1-Wire driver does bit-bang the protocol as most micro-controller implementations. I think Wiki explains the protocol very well 1-Wire - Wikipedia. There are all the numbers.

The reset or presence signal is the big waste in CPU-cycles if using a simple delay loop. Each bit in the protocol is short (shortest a 5 us pulse, 75 us per bit) so an alternative solution such as timer interrupt sampler is not really possible. UART would be possible.

Reading the temperature from DS18B20 is in the order of 2 ms (reset plus 1 byte command, 8 byte address, 1 byte command followed by read of 7 byte, 960 + 17*75 = 2235 us). Most of this time is delay loops. Typically interrupt handling is allowed between bit read/writes. The actual conversion takes 750 ms at full resolution. During this period the processor can do something else or take a nap.

To answer your question; typically less than 1% at 1 second sample rate (per device).

Cheers!

OK, thanks for that. I'm trying to decide if it's worth putting an I2C to 1-wire bridge chip on a new design, I think I will as that also gives active pullup.


Rob

Graynomad:
OK, thanks for that. I'm trying to decide if it's worth putting an I2C to 1-wire bridge chip on a new design, I think I will as that also gives active pullup.


Rob

I had a look at the DS2482-100 Single-Channel 1-Wire Master, I²C to 1-Wire Bridge Device. Interesting challenge to get the OWI device driver to run over the Cosa TWI, i.e. make the Cosa OWI driver hide the fact that a bridge was used and allow code written as an OWI::Driver to run unchanged.

Actually reading the sensor data from a DS18B20 will take a large number of TWI commands (1-Wire search is even worse). The sequencing is a challenge if to avoid polling the status register in a busy-loop over the TWI driver. This could actually increase the CPU load instead of decreasing it. Though there are a number of advantages with this chip I should add.

Cheers!

Interesting observations, I know naff-all about 1-wire and am flying by data sheet here. I'll add the chip and figure it out later :slight_smile:


Rob