Skip to content

Instantly share code, notes, and snippets.

@iH8c0ff33
Created May 1, 2017 09:53
Show Gist options
  • Save iH8c0ff33/a34e00212552a7546ebe23734f183f0c to your computer and use it in GitHub Desktop.
Save iH8c0ff33/a34e00212552a7546ebe23734f183f0c to your computer and use it in GitHub Desktop.
#include "fw/src/mgos_app.h"
#include "fw/src/mgos_wifi.h"
#include "fw/src/mgos_i2c.h"
#include "fw/src/mgos_gpio.h"
#include "fw/src/mgos_timers.h"
#include "fw/src/mgos_hal.h"
#include "fw/src/mgos_bitbang.h"
#define ADDR 0x68
float temps[64];
uint8_t colors[24] = {0x0};
bool enabled = false;
mgos_timer_id timer = 0;
static void rgb() {
mgos_gpio_write(16, 0);
mgos_usleep(60);
mgos_bitbang_write_bits(16, MGOS_DELAY_100NSEC, 3, 8, 7, 6, colors, 24);
mgos_gpio_write(16, 0);
mgos_usleep(60);
mgos_gpio_write(16, 1);
}
static void on_wifi_event(enum mgos_wifi_status event, void *data) {
(void) data;
switch (event) {
case MGOS_WIFI_IP_ACQUIRED:
break;
case MGOS_WIFI_CONNECTED:
break;
case MGOS_WIFI_DISCONNECTED:
break;
}
}
short read_pixel(struct mgos_i2c *i2c, uint8_t pixel) {
uint8_t tmp[2];
uint8_t reg = 0x80 + pixel * 2;
if (!(mgos_i2c_write(i2c, ADDR, &reg, 1, false) && mgos_i2c_read(i2c, ADDR, &tmp, 2, true))) {
return -1;
}
return (tmp[1] << 8) | tmp[0];
}
float read_temp(struct mgos_i2c *i2c, uint8_t pixel) {
return read_pixel(i2c, pixel) * 0.25F;
}
void read_pixels(struct mgos_i2c *i2c, uint8_t pixel, short *data, uint8_t len) {
for (uint8_t i = 0; i < len; i++) {
data[i] = read_pixel(i2c, pixel + i);
}
}
void read_temps(struct mgos_i2c *i2c, uint8_t pixel, float *data, uint8_t len) {
for (uint8_t i = 0; i < len; i++) {
data[i] = read_temp(i2c, pixel + i);
}
}
float temp_avg(float *temps) {
float avg = 0;
for (uint8_t i = 0; i < 64; i++) {
avg += temps[i] / 64;
}
return avg;
}
void debug_temps(float *temps) {
for (uint8_t i = 0; i < 8; i++) {
for (uint8_t j = 0; j < 8; j++) {
printf("%.2f ", temps[j + 8 * i]);
}
printf("\n");
}
}
static void calculate_colors(float *temps, uint8_t *colors) {
float col_average[8];
for (uint8_t i = 0; i < 8; i++) {
for (uint8_t j = 0; j < 8; j++) {
if (col_average[i] < temps[i + j * 8]) {
col_average[i] = temps[i + j * 8];
}
}
}
float tot_average = temp_avg(temps);
for (uint8_t i = 0; i < 8; i++) {
float diff = col_average[i] - tot_average;
if (diff > 2) {
colors[i * 3] = 0x00;
colors[i * 3 + 1] = 0xff;
colors[i * 3 + 2] = 0x00;
} else if (diff > 1.5) {
colors[i * 3] = 0x60;
colors[i * 3 + 1] = 0x60;
colors[i * 3 + 2] = 0x00;
} else {
colors[i * 3] = 0x00;
colors[i * 3 + 1] = 0x00;
colors[i * 3 + 2] = 0x00;
}
}
}
void debug_avg(float *temps, float avg) {
printf(".----------------.----------------.----------------.\n");
for (uint8_t i = 0; i < 8; i++) {
printf("|");
for (uint8_t j = 0; j < 8; j++) {
if (temps[j + 8 * i] > avg + 1.7) {
printf("* ");
} else {
printf(" ");
}
}
printf("|");
for (uint8_t j = 0; j < 8; j++) {
if (temps[j + 8 * i] > avg + 1.5) {
printf("* ");
} else {
printf(" ");
}
}
printf("|");
for (uint8_t j = 0; j < 8; j++) {
if (temps[j + 8 * i] > avg + 1) {
printf("* ");
} else {
printf(" ");
}
}
printf("|\n");
}
printf(".----------------.----------------.----------------.\n");
}
static void timer_handler(void *arg) {
struct mgos_i2c *i2c = mgos_i2c_get_global();
read_temps(i2c, 0, temps, 64);
calculate_colors(temps, colors);
rgb();
(void) arg;
}
static void button_press_handler(int pin, void *arg) {
struct mgos_i2c *i2c = mgos_i2c_get_global();
int lower = mgos_i2c_read_reg_b(i2c, 0x68, 0x80);
int upper = mgos_i2c_read_reg_b(i2c, 0x68, 0x81);
int temp = (upper << 8) | lower;
if (upper != 0) {
temp = -(2048 - temp);
}
float celsius = temp * 0.25F;
LOG(LL_INFO, ("lower: %x, upper: %x", lower, upper));
LOG(LL_INFO, ("temp: %.2f", celsius));
LOG(LL_INFO, ("full temp: %.2f", read_temp(i2c, 0)));
read_temps(i2c, 0, temps, 64);
debug_temps(temps);
float average = temp_avg(temps);
LOG(LL_INFO, ("average: %.2f", average));
debug_avg(temps, average);
if (enabled) {
mgos_clear_timer(timer);
for (uint8_t i = 0; i < 24; i++) {
colors[i] = 0;
}
rgb();
enabled = false;
} else {
timer = mgos_set_timer(100, true, timer_handler, NULL);
enabled = true;
}
calculate_colors(temps, colors);
rgb();
(void) pin;
(void) arg;
}
static void interrupt_handler(int pin, void *arg) {
LOG(LL_INFO, ("interrupt triggered"));
(void) pin;
(void) arg;
}
enum mgos_app_init_result mgos_app_init(void) {
mgos_wifi_add_on_change_cb(on_wifi_event, 0);
mgos_gpio_set_button_handler(0, MGOS_GPIO_PULL_UP, MGOS_GPIO_INT_EDGE_POS, 50, button_press_handler, NULL);
mgos_gpio_set_int_handler(5, MGOS_GPIO_INT_EDGE_NEG, interrupt_handler, NULL);
mgos_gpio_enable_int(5);
mgos_gpio_set_mode(16, MGOS_GPIO_MODE_OUTPUT);
colors[0] = 0x00;
colors[1] = 0xff;
colors[2] = 0x00;
rgb();
return MGOS_APP_INIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment