static int lua_spi_close(lua_State *L) { spi_t *spi; int ret; spi = luaL_checkudata(L, 1, "periphery.SPI"); if ((ret = spi_close(spi)) < 0) return lua_spi_error(L, ret, spi_errno(spi), "Error: %s", spi_errmsg(spi)); return 0; }
static int lua_spi_transfer(lua_State *L) { spi_t *spi; uint8_t *buf; unsigned int i, len; int ret; spi = luaL_checkudata(L, 1, "periphery.SPI"); lua_spi_checktype(L, 2, LUA_TTABLE); len = luaL_len(L, 2); if ((buf = malloc(len)) == NULL) return lua_spi_error(L, SPI_ERROR_ALLOC, errno, "Error: allocating memory"); /* Convert byte table to byte buffer */ for (i = 0; i < len; i++) { lua_pushunsigned(L, i+1); lua_gettable(L, -2); if (!lua_isnumber(L, -1)) { free(buf); return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: invalid element index %d in bytes table.", i+1); } buf[i] = lua_tounsigned(L, -1); lua_pop(L, 1); } if ((ret = spi_transfer(spi, buf, buf, len)) < 0) { free(buf); return lua_spi_error(L, ret, spi_errno(spi), "Error: %s", spi_errmsg(spi)); } /* Convert byte buffer back to bytes table */ for (i = 0; i < len; i++) { lua_pushunsigned(L, i+1); lua_pushunsigned(L, buf[i]); lua_settable(L, -3); } free(buf); return 1; }
static int lua_spi_open(lua_State *L) { spi_t *spi; const char *device; unsigned int mode; uint32_t max_speed; spi_bit_order_t bit_order; uint8_t bits_per_word; uint8_t extra_flags; int ret; spi = luaL_checkudata(L, 1, "periphery.SPI"); /* Initialize file descriptor to an invalid value, in case an error occurs * below and gc later close()'s this object. */ spi->fd = -1; /* Default settings of optional arguments */ bit_order = MSB_FIRST; bits_per_word = 8; extra_flags = 0; /* Arguments passed in table form */ if (lua_istable(L, 2)) { lua_getfield(L, 2, "device"); if (!lua_isstring(L, -1)) return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: invalid type on table argument 'device', should be string"); lua_getfield(L, 2, "mode"); if (!lua_isnumber(L, -1)) return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: invalid type on table argument 'mode', should be number"); lua_getfield(L, 2, "max_speed"); if (!lua_isnumber(L, -1)) return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: invalid type on table argument 'max_speed', should be number"); device = lua_tostring(L, -3); mode = lua_tounsigned(L, -2); max_speed = lua_tounsigned(L, -1); /* Optional bit_order */ lua_getfield(L, 2, "bit_order"); if (lua_isstring(L, -1)) { const char *s; s = lua_tostring(L, -1); if (strcmp(s, "msb") == 0) bit_order = MSB_FIRST; else if (strcmp(s, "lsb") == 0) bit_order = LSB_FIRST; else return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: invalid table argument 'bit_order', should be 'msb' or 'lsb'"); } else if (!lua_isnil(L, -1)) { return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: invalid type on table argument 'bit_order', should be string"); } /* Optional bits_per_word */ lua_getfield(L, 2, "bits_per_word"); if (lua_isnumber(L, -1)) bits_per_word = lua_tounsigned(L, -1); else if (!lua_isnil(L, -1)) return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: invalid type on table argument 'bits_per_word', should be number"); /* Optional extra_flags */ lua_getfield(L, 2, "extra_flags"); if (lua_isnumber(L, -1)) extra_flags = lua_tounsigned(L, -1); else if (!lua_isnil(L, -1)) return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: invalid type on table argument 'extra_flags', should be number"); /* Arguments passed normally */ } else { lua_spi_checktype(L, 2, LUA_TSTRING); lua_spi_checktype(L, 3, LUA_TNUMBER); lua_spi_checktype(L, 4, LUA_TNUMBER); device = lua_tostring(L, 2); mode = lua_tounsigned(L, 3); max_speed = lua_tounsigned(L, 4); } if ((ret = spi_open_advanced(spi, device, mode, max_speed, bit_order, bits_per_word, extra_flags)) < 0) return lua_spi_error(L, ret, spi_errno(spi), spi_errmsg(spi)); return 0; }
static int lua_spi_newindex(lua_State *L) { spi_t *spi; const char *field; spi = luaL_checkudata(L, 1, "periphery.SPI"); if (!lua_isstring(L, 2)) return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: unknown property"); field = lua_tostring(L, 2); if (strcmp(field, "fd") == 0) return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: immutable property"); else if (strcmp(field, "mode") == 0) { unsigned int mode; int ret; lua_spi_checktype(L, 3, LUA_TNUMBER); mode = lua_tounsigned(L, 3); if ((ret = spi_set_mode(spi, mode)) < 0) return lua_spi_error(L, ret, spi_errno(spi), "Error: %s", spi_errmsg(spi)); return 0; } else if (strcmp(field, "max_speed") == 0) { uint32_t max_speed; int ret; lua_spi_checktype(L, 3, LUA_TNUMBER); max_speed = lua_tounsigned(L, 3); if ((ret = spi_set_max_speed(spi, max_speed)) < 0) return lua_spi_error(L, ret, spi_errno(spi), "Error: %s", spi_errmsg(spi)); return 0; } else if (strcmp(field, "bit_order") == 0) { const char *s; spi_bit_order_t bit_order; int ret; lua_spi_checktype(L, 3, LUA_TSTRING); s = lua_tostring(L, 3); if (strcmp(s, "msb") == 0) bit_order = MSB_FIRST; else if (strcmp(s, "lsb") == 0) bit_order = LSB_FIRST; else return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: invalid bit_order, should be 'msb' or 'lsb'"); if ((ret = spi_set_bit_order(spi, bit_order)) < 0) return lua_spi_error(L, ret, spi_errno(spi), "Error: %s", spi_errmsg(spi)); return 0; } else if (strcmp(field, "bits_per_word") == 0) { uint8_t bits_per_word; int ret; lua_spi_checktype(L, 3, LUA_TNUMBER); bits_per_word = lua_tounsigned(L, 3); if ((ret = spi_set_bits_per_word(spi, bits_per_word)) < 0) return lua_spi_error(L, ret, spi_errno(spi), "Error: %s", spi_errmsg(spi)); return 0; } else if (strcmp(field, "extra_flags") == 0) { uint8_t extra_flags; int ret; lua_spi_checktype(L, 3, LUA_TNUMBER); extra_flags = lua_tounsigned(L, 3); if ((ret = spi_set_extra_flags(spi, extra_flags)) < 0) return lua_spi_error(L, ret, spi_errno(spi), "Error: %s", spi_errmsg(spi)); return 0; } return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: unknown property"); }
static int lua_spi_index(lua_State *L) { spi_t *spi; const char *field; if (!lua_isstring(L, 2)) return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: unknown method or property"); field = lua_tostring(L, 2); /* Look up method in metatable */ lua_getmetatable(L, 1); lua_getfield(L, -1, field); if (!lua_isnil(L, -1)) return 1; spi = luaL_checkudata(L, 1, "periphery.SPI"); if (strcmp(field, "fd") == 0) { lua_pushinteger(L, spi_fd(spi)); return 1; } else if (strcmp(field, "mode") == 0) { unsigned int mode; int ret; if ((ret = spi_get_mode(spi, &mode)) < 0) return lua_spi_error(L, ret, spi_errno(spi), "Error: %s", spi_errmsg(spi)); lua_pushunsigned(L, mode); return 1; } else if (strcmp(field, "max_speed") == 0) { uint32_t speed; int ret; if ((ret = spi_get_max_speed(spi, &speed)) < 0) return lua_spi_error(L, ret, spi_errno(spi), "Error: %s", spi_errmsg(spi)); lua_pushunsigned(L, speed); return 1; } else if (strcmp(field, "bit_order") == 0) { spi_bit_order_t bit_order; int ret; if ((ret = spi_get_bit_order(spi, &bit_order)) < 0) return lua_spi_error(L, ret, spi_errno(spi), "Error: %s", spi_errmsg(spi)); switch (bit_order) { case MSB_FIRST: lua_pushstring(L, "msb"); break; case LSB_FIRST: lua_pushstring(L, "lsb"); break; default: lua_pushstring(L, "unknown"); break; } return 1; } else if (strcmp(field, "bits_per_word") == 0) { uint8_t bits_per_word; int ret; if ((ret = spi_get_bits_per_word(spi, &bits_per_word)) < 0) return lua_spi_error(L, ret, spi_errno(spi), "Error: %s", spi_errmsg(spi)); lua_pushunsigned(L, bits_per_word); return 1; } else if (strcmp(field, "extra_flags") == 0) { uint8_t extra_flags; int ret; if ((ret = spi_get_extra_flags(spi, &extra_flags)) < 0) return lua_spi_error(L, ret, spi_errno(spi), "Error: %s", spi_errmsg(spi)); lua_pushunsigned(L, extra_flags); return 1; } return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: unknown property"); }
int main(void) { spi_t spi; spi_bit_order_t MSB_FIRST; uint8_t buf[1] = {0x00}; uint8_t buf0[1]; int8_t buf2[7]; uint8_t buf1[7] = {0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0x80}; // To change the gain to 64, change the last byte(0x80) to (0xA8) or 32 => (0xA0) char buffer [8]; unsigned char byte; int32_t desired_bits; int i, j; int size = 6; int32_t offset; int nsamples=N_SAMPLES; long samples[nsamples]; float spread_percent = SPREAD / 100.0 /2.0; float filter_low, filter_high; long tmp_avg=0; long tmp_avg2; /* Open spidev0.0 with mode 0 and max speed 1MHz */ if (spi_open(&spi, "/dev/spidev0.0", 0, 1000000) < 0) { fprintf(stderr, "spi_open(): %s\n", spi_errmsg(&spi)); exit(1); } /* Set_Offset */ i=0; j=0; printf("Wait: Getting Tare\n"); while(i<=nsamples) { /* Shift out 1 byte of 0x00 and read DOUT */ if (spi_transfer(&spi, buf, buf0, sizeof(buf)) < 0) { fprintf(stderr, "spi_transfer(): %s\n", spi_errmsg(&spi)); exit(1); } if(buf0[0] == 0x00) { spi_transfer(&spi, buf1, buf2, sizeof(buf2)); desired_bits = get_bits(buf2); samples[i] = desired_bits; i++; } } for(i=0;i<nsamples;i++) { tmp_avg += samples[i]; } //usleep(6000); tmp_avg = tmp_avg / nsamples; tmp_avg2 = 0; j=0; filter_low = (float) tmp_avg * (1.0 - spread_percent); filter_high = (float) tmp_avg * (1.0 + spread_percent); // printf("%d %d\n", (int) filter_low, (int) filter_high); for(i=0;i<nsamples;i++) { if ((samples[i] < filter_high && samples[i] > filter_low) || (samples[i] > filter_high && samples[i] < filter_low) ) { tmp_avg2 += samples[i]; j++; } } if (j == 0) { printf("No data to consider: Change the spread or the number of samples\n"); exit(255); } offset = tmp_avg2/j; //offset = tmp_avg; printf("Offset: %d\n", offset); printf("Starting...\n" ); usleep(5000000); while(1) { /* Shift out 1 byte of 0x00 and read DOUT */ if (spi_transfer(&spi, buf, buf0, sizeof(buf)) < 0) { fprintf(stderr, "spi_transfer(): %s\n", spi_errmsg(&spi)); exit(1); } /*If DOUT is 0x00, Shift out 4 bytes that represents 24 pulses + 1 of set gain(128) */ if(buf0[0] == 0x00){ spi_transfer(&spi, buf1, buf2, sizeof(buf2)); desired_bits = get_bits(buf2); } printf("Value: %d grams\n", (desired_bits - offset)/scale); } spi_close(&spi); return 0; }