aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/vector.c
blob: 086390893775686a7ea21fc24da8b8ae7eb049e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "vector.h"

static int grow(struct vector *vec, int gently)
{
	size_t new_alloc;
	void *new_data;

	new_alloc = vec->alloc * 3 / 2;
	if (!new_alloc)
		new_alloc = 8;
	new_data = realloc(vec->data, new_alloc * vec->size);
	if (!new_data) {
		if (gently)
			return ENOMEM;
		perror("vector.c:grow()");
		exit(1);
	}
	vec->data = new_data;
	vec->alloc = new_alloc;
	return 0;
}

int vector_push(struct vector *vec, const void *data, int gently)
{
	int rc;

	if (vec->count == vec->alloc && (rc = grow(vec, gently)))
		return rc;
	if (data)
		memmove(vec->data + vec->count * vec->size, data, vec->size);
	else
		memset(vec->data + vec->count * vec->size, 0, vec->size);
	vec->count++;
	return 0;
}
="n">iSDA, iSCL; // pin numbers (0xff = disabled) uint8_t bWire; // use the Wire library uint8_t iSDABit, iSCLBit; // bit numbers of the ports uint32_t iDelay; } BBI2C; // // Read N bytes // int I2CRead(BBI2C *pI2C, uint8_t iAddr, uint8_t *pData, int iLen); // // Read N bytes starting at a specific I2C internal register // int I2CReadRegister(BBI2C *pI2C, uint8_t iAddr, uint8_t u8Register, uint8_t *pData, int iLen); // // Write I2C data // quits if a NACK is received and returns 0 // otherwise returns the number of bytes written // int I2CWrite(BBI2C *pI2C, uint8_t iAddr, uint8_t *pData, int iLen); // // Scans for I2C devices on the bus // returns a bitmap of devices which are present (128 bits = 16 bytes, LSB first) // // Test if an address responds // returns 0 if no response, 1 if it responds // uint8_t I2CTest(BBI2C *pI2C, uint8_t addr); // A set bit indicates that a device responded at that address // void I2CScan(BBI2C *pI2C, uint8_t *pMap); // // Initialize the I2C BitBang library // Pass the pin numbers used for SDA and SCL // as well as the clock rate in Hz // void I2CInit(BBI2C *pI2C, uint32_t iClock); // // Figure out what device is at that address // returns the enumerated value // int I2CDiscoverDevice(BBI2C *pI2C, uint8_t i); #endif //__BITBANG_I2C__