The Device Kit Table of Contents | The Device Kit Index

# BD2A

Derived from: none

Declared in: be/device/D2A.h

Library: libdevice.so

###### Summary

The BD2A class lets you write to (and, less importantly, read from) the GeekPort's digital-to-analog converter (DAC). Each BD2A object can write a single channel at a time; if you want to read all four channels simultaneously, you have to create four separate objects.

To write a value to one of the d/a channels, you create a new BD2A object, open it on the channel you want, and then (repeatedly) invoke the object's `Write()` function. When you're through reading, you call `Close()` so some other object can open the channel. The `Write()` function writes a single byte at each call and returns 1 if it was successful:

       #include <D2A.h>

       void WriteD2A1()
       {
          uint8 val;
          BD2A d2a;

          if (d2a.Open("D2A1") <= 0)
             return;

          while ( /* whatever */ ) {
             /* Get a byte from somewhere. */
             val = ...;

             if (d2a.Write(val) != 1)
                break;
             snooze(1000);
          }
          d2a.Close();
       }

The DAC performs a "sample and hold": The voltage that the DAC produces is maintained until another `Write()` call (on that channel) changes the setting. Furthermore, the "hold" persists across BD2A objects: Neither closing nor deleting a BD2A object affects the voltage that's held by the corresponding GeekPort pin.

The BD2A class also implements a `Read()` function. This function returns the value that was most recently written to the particular DAC channel.

## Constructor and Destructor

### BD2A()

`BD2A(`void`)`

Creates a new object that can open a DAC channel. The particular channel is specified in a subsequent `Open()` call. Constructing a new BD2A object doesn't affect the state of the DAC.

### ~BD2A()

virtual `~BD2A()`

Closes the channel that the object holds open (if any) and then destroys the object.

Deleting a BD2A object doesn't affect the DAC channel's output voltage. If you want the voltage cleared (for example), you have to set it to 0 explicitly before deleting (or otherwise closing) the BD2A object.

## Member Functions

### Open() , IsOpen() , Close()

status_t `Open(`const char *name`)`
bool `IsOpen(`void`)` ``

Open() opens the named DAC (BD2A) channel. The channel names are:

BD2A Channels
"D2A1"
"D2A2"
"D2A3"
"D2A4"

Each channel can only be held open by one object at a time; you should close the channel as soon as you're finished with it. Furthermore, each BD2A object can only hold one channel open at a time. When you invoke `Open()`, the channel that the object currently has open is automatically closed—even if the channel that you're attempting to open is the channel that the object already has open.

Opening a DAC channel doesn't affect the data in the channel itself. In particular, when you open a DAC channel, the channel's output voltage isn't changed.

`IsOpen()` returns `true` if the object holds a DAC channel open. Otherwise, it returns `false`.

`Close()` does the obvious without affecting the state of the ADC or DAC channel. If you want to set a DAC channel's output voltage to 0 (for example), you must explicitly write the value before invoking `Close()`.

RETURN CODES

`Open()` returns a positive integer if the channel is successfully opened; otherwise, it returns `B_ERROR`.

### Read()

ssize_t `Read(`uint8 *dac_8_bit`)`

Returns, by reference in dac_8_bit, the value that was most recently written to the object's DAC channel. The value needn't have been written by this object—it could have been written by the channel's previous opener.

| The BD2A `Read()` function returns a value that's cached by the DAC driver—it doesn't actually tap the GeekPort pin to see what value it's currently carrying. This should only matter to the clever few who will attempt (unsuccessfully) to use the d/a pins as input paths.

The object must open a DAC channel before calling `Read()`.

RETURN CODES

`Read()` return `B_ERROR` if a channel isn't open, or if, for any other reason, the read failed. Otherwise it returns 1 (the number of bytes that were read).

### Write()

ssize_t `Write(`uchar dac_8_bit`)`

Sends the dac_8_bit value to the object's DAC channel. The DAC converts the value to an analog voltage in the range [0, +4.080] Volts and sets the corresponding GeekPort pin. The pin continues to produce the voltage until another`Write()` call—possibly by a different BD2A object—changes the setting.

The DAC's conversion is linear: Each digital step corresponds to 16 mV at the output. The analog voltage midpoint, +2.040V, can be approximated by a digital input of 0x7F (which produces +2.032V) or 0x80 (+2.048V).

RETURN CODES

If the object isn't open, this function returns `B_ERROR`, otherwise it returns 1 (the number of bytes that were written).

The Device Kit Table of Contents | The Device Kit Index

The B e Book,
...in lovely HTML...
for BeOS Release 5.

Copyright © 2000 Be, Inc. All rights reserved.

Text last modified March 21, 2000.