MoonshineSG
11/4/2016 - 3:59 AM

smart hotend

#!/usr/bin/env python
import smbus
import time, struct

# for RPI version 1, use "bus = smbus.SMBus(0)"
bus = smbus.SMBus(1)

LED_STATUS = 0
LED_WIFI = 1

OFF = 0
ON = 1
BLINK = 2
BLINK_FAST = 3
BLINK_BEEP_BEEP = 4

REQUEST_RID = 1

# This is the address we setup in the Arduino Program
address = 0x04

rids = {"empty": [], "a7297":[22000], "bjkk": [10000] }
for v in rids.values():
	v.append(10000)

def get_():
	pass
	
def send_led_command(led, status):
	bus.write_word_data(address, led, status)


def get_resistor_value():
	val =  bus.read_word_data(address, REQUEST_RID)
	return val

def almost_equal(a, b, tolerance = 15):
	bt = b * tolerance / 100
	#print "tolerance on %s = %s"%(b, bt)
	if a < b - bt: return False
	if a > b + bt: return False
	return True

def find_rid(value):
	for k,v in rids.items():
		if len(v) > 1 :
			rs =  reduce(lambda x, y: x*y, v) / sum(v) 
		else:
			rs = v[0]
		#print rs
		#print "almost_equal %s %s"%(rs, value)
		if almost_equal(value, rs):
			print "%s = %s"%(v, rs)
			return k
	return "Unknown [%s]"%value
	
while True:
	LED = LED_STATUS
	var = input("Enter 1 - 5, 9: ")
	if not var:
		continue
	if var == 1:
		send_led_command(LED, ON)
	elif var == 2:	
		send_led_command(LED, OFF)
	elif var == 3:	
		send_led_command(LED, BLINK)
	elif var == 4:	
		send_led_command(LED, BLINK_FAST)
	elif var == 5:	
		send_led_command(LED, BLINK_BEEP_BEEP)
	elif var == 9:	
		number = get_resistor_value()
		print "get_resistor_value : ", number
		print(find_rid(number))
	print
	
#include <Wire.h>

//i2c
#define SLAVE_ADDRESS 0x04

//types of led values
#define LED_OFF 0
#define LED_ON 1
#define LED_BLINK 2
#define LED_BLINK_FAST 3
#define LED_BLINK_BEEP_BEEP 4

#define SPEED 16

//actual GPIO pin #
#define LED_STATUS 2
#define LED_WIFI 3

//request for data type
#define NONE -1
#define RID 1

#define timer 100

#define count_led 2

int led_pins[count_led][2] = {
  {LED_STATUS, LED_BLINK_FAST},
  {LED_WIFI, LED_BLINK}
 }; 

int send_data = -1;

int analogPin = 0;
int raw = 0;
int Vin = 5;
float Vout = 0;
float R1 = 10000;
float buffer = 0;


void setup()
{
  Serial.begin(9600); // start serial for output
  // initialize i2c as slave
  Wire.begin(SLAVE_ADDRESS);
  
  // define callbacks for i2c communication
  Wire.onReceive(receiveData);
  Wire.onRequest(sendData);

  for (int thisPin = 0; thisPin < count_led; thisPin++) {
    pinMode( led_pins[thisPin][0], OUTPUT);
  }
  
  Serial.println("Ready!");
}

void change_status(int thisPin, int cycle){
  switch (led_pins[thisPin][1]) {
      case LED_OFF:
        led_off( led_pins[thisPin][0] );
        break;
      case LED_ON:
        led_on( led_pins[thisPin][0] ); 
        break;       
      case LED_BLINK:
        if ( cycle == 1) {
          led_on( led_pins[thisPin][0] );
        }         
        if ( cycle == SPEED/2) {
          led_off( led_pins[thisPin][0] );
        }
       break;
      case LED_BLINK_FAST :
        if ( cycle % 2 == 0) {
          led_on( led_pins[thisPin][0] );
        } else {
          led_off( led_pins[thisPin][0] );
        }
        break;
      case LED_BLINK_BEEP_BEEP :
        if ( cycle == 1 || cycle == 5 ) {
          led_on( led_pins[thisPin][0] );
        }         
        if ( cycle == 3 || cycle == 7) {
          led_off( led_pins[thisPin][0] );
        }
        break;
  }
}

void loop()
{  
  for (int cycle = 1; cycle <= SPEED; cycle++) {
     for (int thisPin = 0; thisPin < count_led; thisPin++) {
       change_status(thisPin, cycle);      
    }
    delay(2000/SPEED);
  }  
}

void led_on(int pin) {
  digitalWrite(pin, HIGH);
}

void led_off(int pin) {
  digitalWrite(pin, LOW);
}
 
int read_resistor(){
  raw = analogRead(analogPin);
  if (raw)   
  {
    int R2 = 0;
    buffer = raw * Vin;
    Vout = (buffer)/1024.0;
    buffer = (Vin/Vout) - 1;
    R2 = R1 * buffer;
    Serial.print("RID: ");
    Serial.println(R2);
    return R2;
   }
}
// callback for received data
void receiveData(int byteCount){
  int command, value;
  while(Wire.available()) {
    switch (byteCount) {
       case 1: //read 
        command = Wire.read();
        Wire.read(); //read last 0
        
        Serial.print ("request for data: ");
        Serial.println(command);

        send_data = command;
        break;
      case 3: //write
        command = Wire.read();
        value = Wire.read();
        Wire.read(); //read last 0
        
        Serial.print("request for LED: ");        
        Serial.print(command);
        Serial.print( " - ");
        Serial.println(value);
        
        led_pins[command][1] = value;
        break;
     }
  }
}

// callback for sending data
void sendData() {
  int value;
  switch (send_data) {
    case RID:
      value = read_resistor();
      Wire.write((byte *) &value, sizeof (value));
      break;
    default:
      Serial.println ("unknown request");
      break;
  }
  send_data = NONE;
}