Evo jedan primjer danas zašto je open source zajednica puna u najmanju ruku, kretena.
Dakle, imamo ovaj kod u pythonu
#!/usr/bin/env python
# Written by Limor "Ladyada" Fried for Adafruit Industries, (c) 2015
# This code is released into the public domain
import time
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
DEBUG = 1
# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
def readadc(adcnum, clockpin, mosipin, misopin, cspin):
if ((adcnum > 7) or (adcnum < 0)):
return -1
GPIO.output(cspin, True)
GPIO.output(clockpin, False) # start clock low
GPIO.output(cspin, False) # bring CS low
commandout = adcnum
commandout |= 0x18 # start bit + single-ended bit
commandout <<= 3 # we only need to send 5 bits here
for i in range(5):
if (commandout & 0x80):
GPIO.output(mosipin, True)
else:
GPIO.output(mosipin, False)
commandout <<= 1
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
adcout = 0
# read in one empty bit, one null bit and 10 ADC bits
for i in range(12):
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
adcout <<= 1
if (GPIO.input(misopin)):
adcout |= 0x1
GPIO.output(cspin, True)
adcout >>= 1 # first bit is 'null' so drop it
return adcout
# change these as desired - they're the pins connected from the
# SPI port on the ADC to the Cobbler
SPICLK = 11
SPIMISO = 9
SPIMOSI = 10
SPICS = 8
GNU nano 2.2.6 File: smoke.py
SPICS = 8
# set up the SPI interface pins
GPIO.setup(SPIMOSI, GPIO.OUT)
GPIO.setup(SPIMISO, GPIO.IN)
GPIO.setup(SPICLK, GPIO.OUT)
GPIO.setup(SPICS, GPIO.OUT)
# 10k trim pot connected to adc #0
potentiometer_adc = 0;
last_read = 0 # this keeps track of the last potentiometer value
tolerance = 5 # to keep from being jittery we'll only change
# volume when the pot has moved more than 5 'counts'
while True:
# we'll assume that the pot didn't move
trim_pot_changed = False
# read the analog pin
smokeLevel = readadc(potentiometer_adc, SPICLK, SPIMOSI, SPIMISO, SPICS)
# how much has it changed since the last read?
value = smokeLevel - tolerance
if value >= last_read:
print ("smoke %i" % value)
if value > 230:
print("smoke detected")
last_read = value
time.sleep(0.5)
I onda imamo u Javi ovaj kod (koji bi trebao raditi istu j8enu stvar jer ima ISTE PINOVE)
public class Main {
//final GpioController gpio = GpioFactory.getInstance();
//final GpioPinDigitalOutput led = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_06);
//final GpioPinDigitalInput sensor = gpio.provisionDigitalInputPin(RaspiPin.GPIO_01, PinPullResistance.PULL_DOWN);
public static void main(String[] args) {
System.out.print("test");
boolean run = true;
mcp3008.initMCP3008();
int cntr = 0;
while (run)
{
int value = mcp3008.readMCP3008(0);
System.out.println(value);
cntr++;
if (cntr >= 100)
{
run = false;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
mcp3008.shutdownMCP3008();
}
}
public class mcp3008
{
private final static boolean DISPLAY_DIGIT = "true".equals(System.getProperty("display.digit", "false"));
// Note: "Mismatch" 23-24. The wiring says DOUT->#23, DIN->#24
// 23: DOUT on the ADC is IN on the GPIO. ADC:Slave, GPIO:Master
// 24: DIN on the ADC, OUT on the GPIO. Same reason as above.
// SPI: Serial Peripheral Interface
private static Pin spiClk = RaspiPin.GPIO_11; // Pin #18, clock
private static Pin spiMiso = RaspiPin.GPIO_09; // Pin #23, data in. MISO: Master In Slave Out
private static Pin spiMosi = RaspiPin.GPIO_10; // Pin #24, data out. MOSI: Master Out Slave In
private static Pin spiCs = RaspiPin.GPIO_08; // Pin #25, Chip Select
public enum MCP3008_input_channels
{
CH0(0),
CH1(1),
CH2(2),
CH3(3),
CH4(4),
CH5(5),
CH6(6),
CH7(7);
private int ch;
MCP3008_input_channels(int chNum)
{
this.ch = chNum;
}
public int ch() { return this.ch; }
}
private static GpioController gpio;
private static GpioPinDigitalInput misoInput = null;
private static GpioPinDigitalOutput mosiOutput = null;
private static GpioPinDigitalOutput clockOutput = null;
private static GpioPinDigitalOutput chipSelectOutput = null;
public static void initMCP3008()
{
gpio = GpioFactory.getInstance();
mosiOutput = gpio.provisionDigitalOutputPin(spiMosi, "MOSI", PinState.LOW);
clockOutput = gpio.provisionDigitalOutputPin(spiClk, "CLK", PinState.LOW);
chipSelectOutput = gpio.provisionDigitalOutputPin(spiCs, "CS", PinState.LOW);
misoInput = gpio.provisionDigitalInputPin(spiMiso, "MISO");
}
public static void shutdownMCP3008()
{
gpio.shutdown();
}
public static int readMCP3008(int channel)
{
chipSelectOutput.high();
clockOutput.low();
chipSelectOutput.low();
int adccommand = channel;
if (DISPLAY_DIGIT)
System.out.println("1 - ADCCOMMAND: 0x" + lpad(Integer.toString(adccommand, 16).toUpperCase(), "0", 4) +
", 0&" + lpad(Integer.toString(adccommand, 2).toUpperCase(), "0", 16));
adccommand |= 0x18; // 0x18: 00011000
if (DISPLAY_DIGIT)
System.out.println("2 - ADCCOMMAND: 0x" + lpad(Integer.toString(adccommand, 16).toUpperCase(), "0", 4) +
", 0&" + lpad(Integer.toString(adccommand, 2).toUpperCase(), "0", 16));
adccommand <<= 3;
if (DISPLAY_DIGIT)
System.out.println("3 - ADCCOMMAND: 0x" + lpad(Integer.toString(adccommand, 16).toUpperCase(), "0", 4) +
", 0&" + lpad(Integer.toString(adccommand, 2).toUpperCase(), "0", 16));
// Send 5 bits: 8 - 3. 8 input channels on the MCP3008.
for (int i=0; i<5; i++) //
{
if (DISPLAY_DIGIT)
System.out.println("4 - (i=" + i + ") ADCCOMMAND: 0x" + lpad(Integer.toString(adccommand, 16).toUpperCase(), "0", 4) +
", 0&" + lpad(Integer.toString(adccommand, 2).toUpperCase(), "0", 16));
if ((adccommand & 0x80) != 0x0) // 0x80 = 0&10000000
mosiOutput.high();
else
mosiOutput.low();
adccommand <<= 1;
// Clock high and low
tickOnPin(clockOutput);
}
int adcOut = 0;
for (int i=0; i<12; i++) // Read in one empty bit, one null bit and 10 ADC bits
{
tickOnPin(clockOutput);
adcOut <<= 1;
if (misoInput.isHigh())
{
// System.out.println(" " + misoInput.getName() + " is high (i:" + i + ")");
// Shift one bit on the adcOut
adcOut |= 0x1;
}
if (DISPLAY_DIGIT)
System.out.println("ADCOUT: 0x" + lpad(Integer.toString(adcOut, 16).toUpperCase(), "0", 4) +
", 0&" + lpad(Integer.toString(adcOut, 2).toUpperCase(), "0", 16));
}
chipSelectOutput.high();
adcOut >>= 1; // Drop first bit
return adcOut;
}
private static void tickOnPin(GpioPinDigitalOutput pin)
{
pin.high();
pin.low();
}
private static String lpad(String str, String with, int len)
{
String s = str;
while (s.length() < len)
s = with + s;
return s;
}
}
ALI NARAVNO DA NE RADI ISTU STVAR!!!
Ajd sad ti reci da li tu ima 2 pametna u cijeloj toj priči... I onda netko kaže da je open source pristup dobar. Je da, super. odličan. Sva sreća da ne rade nuklearne bombe tako....
[edit] - vizualni prikaz j8ene razlike, ako nekome nije jasno. Lijevo je Python, desno je Java. Po svoj logici stvari bi trebalo biti potpuno isto ali naravno da nije.
Tko je tu lud?