#ifndef TM1640_GPIO_H
#define TM1640_GPIO_H

#include "sysfs_gpio.h"

/* commands */
#define TM1640_ADDR(v)  (0xc0 | ((v)&0x0f)) /* move cursor to digit v */
#define TM1640_PWM(v)   (0x88 | ((v)&0x07)) /* PWM 0=1/16, 7=14/16 */
#define TM1640_OFF       0x80

typedef struct _tm1640_gpio
{
   sysfs_gpio *gpio_sclk;
   sysfs_gpio *gpio_din;
} tm1640_gpio;

/* open a tm1640 connected to the given GPIO pins, /sys/class/gpio/gpio<N>/
   must be accessible, will return -1 on error, 0 on success, some half-useful
   stuff will be written to stderr on failure */
tm1640_gpio* tm1640_gpio_init(int n_gpio_din, int n_gpio_sclk);

/* close the tm1640 and associated IOs */
void tm1640_gpio_close(tm1640_gpio *gpio_handler);

/* send a command to the TM1640, see defines on top of this header file,
   return -1 on failure, 0 on success */
int tm1640_gpio_command(tm1640_gpio *gpio_handler, unsigned char v);

/* send a byte to TM1640, cursor has been positioned with TM1640_ADDR
   preceeding this call, returns -1 on failure, 0 on success */
int tm1640_gpio_byte(tm1640_gpio *gpio_handler, const unsigned char group_id, const unsigned char data);

/* send data to TM1640, cursor has been positioned with TM1640_ADDR
   preceeding this call, returns -1 on failure, 0 on success */
int tm1640_gpio_data(tm1640_gpio *gpio_handler,const unsigned char *data, int len);

#endif