Skip to content
This repository has been archived by the owner on Jan 2, 2022. It is now read-only.

Commit

Permalink
implement Digital/Analog I/O.
Browse files Browse the repository at this point in the history
  • Loading branch information
nakkaya committed Aug 4, 2010
1 parent 7c1f221 commit 7c894e7
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 35 deletions.
10 changes: 4 additions & 6 deletions resources/SocketApp/SocketApp.pde
Expand Up @@ -49,12 +49,10 @@ unsigned char ssid_len;
unsigned char security_passphrase_len;
//---------------------------------------------------------------------------

void setup()
{
WiFi.init();
void setup(){
WiFi.init();
}

void loop()
{
WiFi.run();
void loop(){
WiFi.run();
}
76 changes: 64 additions & 12 deletions resources/SocketApp/handler.h
@@ -1,40 +1,92 @@
#include "WProgram.h"
#include <string.h>
#include <WProgram.h>

int parsePin(char* msg){
char pin[] = {msg[2],msg[3]};
return atoi(pin);
}

int cmdCmpr(char* msg, char* expected){
if (msg[0] == expected[0] && msg[1] == expected[1])
return true;
else
return false;
}

char* setPinMode(char* msg){
char pin[] = {msg[2],msg[3]};
int pin = parsePin(msg);
char mode = msg[4];

if (mode == 'o'){
pinMode(atoi(pin), OUTPUT);
digitalWrite(pin, LOW); // disable PWM
pinMode(pin, OUTPUT);
return "OK\n";
}else if (mode == 'i'){
pinMode(atoi(pin), INPUT);
pinMode(pin, INPUT);
return "OK\n";
}else if (mode == 'a'){
digitalWrite(pin + 14, LOW); // disable internal pull-ups
pinMode(pin + 14, INPUT);
return "OK\n";
}else if (mode == 'p'){
pinMode(pin, OUTPUT);
return "OK\n";
}else
return "Error.\n";
return "Error\n";
}

char* setDigitalWrite(char* msg){
char pin[] = {msg[2],msg[3]};
int pin = parsePin(msg);
char mode = msg[4];

if (mode == 'h'){
digitalWrite(atoi(pin), HIGH);
digitalWrite(pin, HIGH);
return "OK\n";
}else if (mode == 'l'){
digitalWrite(atoi(pin), LOW);
digitalWrite(pin, LOW);
return "OK\n";
}else
return "Error.\n";
return "Error\n";
}

char* setAnalogWrite(char* msg){
int pin = parsePin(msg);
char val[] = {msg[4], msg[5], msg[6]};

analogWrite(pin, atoi(val));
return "OK\n";
}

char* getDigitalRead(char* msg){
int pin = parsePin(msg);

char buff [20];
itoa(digitalRead(pin),buff,10);

strncat(buff,"\n",1);
return buff;
}

int getAnalogRead(char* msg){
int pin = parsePin(msg);

char buff [20];
itoa(analogRead(pin),buff,10);

strncat(buff,"\n",1);
return buff;
}

char* process(char* msg){
if (msg[0] == 'p' && msg[1] == 'm')
if (cmdCmpr(msg,"pm"))
return setPinMode(msg);
else if (msg[0] == 'd' && msg[1] == 'w')
else if (cmdCmpr(msg,"dw"))
return setDigitalWrite(msg);
else if (cmdCmpr(msg,"aw"))
return setAnalogWrite(msg);
else if (cmdCmpr(msg,"dr"))
return getDigitalRead(msg);
else if (cmdCmpr(msg,"ar"))
return getAnalogRead(msg);
else
return "Unknown Command.\n";
}
18 changes: 9 additions & 9 deletions src/clodiuno/core.clj
Expand Up @@ -16,17 +16,17 @@
"Write a HIGH or a LOW value to a digital pin."
(fn [type _ _] (type :interface)))

;; (defmulti digital-read
;; "Read a HIGH or a LOW value from a digital pin."
;; (fn [type] ((meta type) :interface)))
(defmulti digital-read
"Read a HIGH or a LOW value from a digital pin."
(fn [type _] (type :interface)))

;; (defmulti analog-read
;; "Reads the value from the specified analog pin."
;; (fn [type] ((meta type) :interface)))
(defmulti analog-read
"Reads the value from the specified analog pin."
(fn [type _] (type :interface)))

;; (defmulti analog-write
;; "Write an analog value (PWM-wave) to a digital pin."
;; (fn [type] ((meta type) :interface)))
(defmulti analog-write
"Write an analog value (PWM-wave) to a digital pin."
(fn [type _ _] (type :interface)))

(defmulti close
"Close serial interface."
Expand Down
43 changes: 35 additions & 8 deletions src/clodiuno/wishield.clj
Expand Up @@ -2,10 +2,12 @@
#^{:author "Nurullah Akkaya",
:doc "WiShield Library for Clojure."}
(:use clodiuno.core :reload-all)
(:import (java.net Socket)
(:import (java.text DecimalFormat)
(java.net Socket)
(java.io PrintWriter InputStreamReader BufferedReader)))

(derive ::wishield ::interface)
(def pin-format (DecimalFormat. "00"))
(def pwm-format (DecimalFormat. "000"))

(def INPUT 0) ;;pin to input mode
(def OUTPUT 1) ;;pin to output mode
Expand All @@ -26,22 +28,34 @@
(defmethod disable-pin :wishield [board type pin])

(defmethod pin-mode :wishield [board pin mode]
(let [pin (if (< pin 10) (str "0" pin) (str pin))
(let [pin (.format pin-format pin)
mode (cond (= mode INPUT) "i"
(= mode OUTPUT) "o"
(= mode ANALOG) "a"
(= mode PWM) "p"
(= mode SERVO) "s"
;;(= mode SERVO) "s"
:default (throw (Exception. "Invalid Mode.")))]
(send-command board (str "pm" pin mode))))

(defmethod digital-write :wishield [board pin value]
(let [pin (if (< pin 10) (str "0" pin) (str pin))
(let [pin (.format pin-format pin)
value (cond (= value HIGH) "h"
(= value LOW) "l"
:default (throw (Exception. "Invalid Value.")))]
(send-command board (str "dw" pin value))))

(defmethod analog-write :wishield [board pin value]
(send-command
board (str "aw" (.format pin-format pin) (.format pwm-format value))))

(defmethod analog-read :wishield [board pin]
(read-string
(send-command board (str "ar" (.format pin-format pin)))))

(defmethod digital-read :wishield [board pin]
(read-string
(send-command board (str "dr" (.format pin-format pin)))))

(defmethod close :wishield [board]
(let [{:keys [in out socket]} board]
(send-command board "bye")
Expand All @@ -60,9 +74,22 @@
(comment
(def board (arduino "10.0.2.100" 1000))

(pin-mode board 6 OUTPUT)
(digital-write board 6 HIGH)
(digital-write board 6 LOW)
(do
(pin-mode board 3 OUTPUT)
(pin-mode board 6 INPUT)
(while true
(let [i (digital-read board 6)]
(println i)
(digital-write board 3 i))))

(let [map (fn [x in-min in-max out-min out-max]
(+ (/ (* (- x in-min) (- out-max out-min))
(- in-max in-min)) out-min))]
(pin-mode board 3 PWM)
(pin-mode board 5 ANALOG)

(while true (analog-write board 3
(map (analog-read board 5) 0 1023 0 255))))

(close board)
)

0 comments on commit 7c894e7

Please sign in to comment.