mcp.digitalWrite

446 views
Skip to first unread message

Toshi Bass

unread,
Dec 5, 2013, 3:57:48 PM12/5/13
to web...@googlegroups.com
Hi

mcp.digitalWrite(1, 0) will set pin 1 on mcp to 0 - ok easy to understand, however is there a way to set pins 0 to 15 on mcp to 0, in one go ? 

I noticed mcp.portWrite(0x0F) # write channels 0-3 as HIGH  on the wiki, but I don't understand how this works or how to change it to do what I want.

appreciate any explanation thanks.

Toshi

trouch

unread,
Dec 5, 2013, 4:22:42 PM12/5/13
to web...@googlegroups.com
it's about binary

you need to compute an integer for which each bit is equal to the corresponding pin value.
assume you want output 0, 1, 5 set to high
in binary, it's 00010011 = (PIN7)(PIN6)...(PIN1)(PIN0). in hex, it's 0x13

if you want all 16 (0 to 15) output set to low : mcp.portWrite(0x0000)
if you want all 16 (0 to 15) output set to high : mcp.portWrite(0xFFFFF)
if you want output 0-7 set to high, 8-15 to low : mcp.portWrite(0x00FF)
if you want output 0-11 set to low, 12-15 to high : mcp.portWrite(0xF000)




--
You received this message because you are subscribed to the Google Groups "WebIOPi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webiopi+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Toshi Bass

unread,
Dec 5, 2013, 4:49:22 PM12/5/13
to web...@googlegroups.com
trouch

Thanks great explanation, works fine in python, don't suppose theirs something similar in java 

I have this  mcp0.digitalWrite(8, 0, macro_mcp0_callback);  but I would like to set all 16 (0 to 15) output set to low ?

trouch

unread,
Dec 5, 2013, 5:06:14 PM12/5/13
to web...@googlegroups.com
nop, there is not... yet



--

Toshi Bass

unread,
Dec 5, 2013, 5:52:59 PM12/5/13
to web...@googlegroups.com
Ok thanks

Toshi Bass 

Toshi Bass

unread,
Dec 7, 2013, 5:11:37 AM12/7/13
to web...@googlegroups.com
Hi trouch

Is it possible to write a bit sequence to mcp23017 like :_

function outputSequence() {
var sequence = "10010"
mcp0 = new GPIOPort("mcp0");
webiopi().outputSequence(mcp0.digitalWrite(10), 750, sequence);

//webiopi().outputSequence(25, 750, sequence);
}
writing to gpio 25 works fine!
 
Toshi Bass 

trouch

unread,
Dec 7, 2013, 1:10:04 PM12/7/13
to web...@googlegroups.com
Not (yet)


--

Toshi Bass

unread,
Dec 8, 2013, 11:18:42 AM12/8/13
to web...@googlegroups.com
trouch,

ok just one more question then, perhaps it should have been first question!, besides mcp.digitalWrite and mcp.digitalRead are there any other java helpers ?

Thanks

Toshi
 

trouch

unread,
Dec 8, 2013, 1:02:12 PM12/8/13
to web...@googlegroups.com
Java Client only have get/setFunction and digitalRead/Write.
But the local Python library and the REST API have more functions :

It easy to add few missing binding on the Java Client, just look on the the existing source.
I actually done it really quickly as an example and proof of concept to try up the CoAP underlying library.


--

Toshi Bass

unread,
Dec 13, 2013, 4:27:55 AM12/13/13
to web...@googlegroups.com
hi trouch

I have built a set of buttons to control mcp23017 using port.digitalRead / port.digitalWrite method instead of using python, its working nice however after searching this subject on this group I found a mention of port.refreshUI(); would it be possible to tell what this does and if it would be suitable to incorporate it into my code, i currently refresh the UI with the following:

function macro_check_security(){ // buttons for zones
mcp1 = new GPIOPort("mcp1");
for (var i=8;i<16;i++)
mcp1.digitalRead(i, macro_security_callback);
}

Toshi

Eric PTAK

unread,
Dec 13, 2013, 6:11:58 AM12/13/13
to web...@googlegroups.com
It's used to build and refresh the device UI showed in the device monitor.



--
Message has been deleted

Toshi Bass

unread,
Dec 23, 2013, 4:03:53 PM12/23/13
to web...@googlegroups.com

Hi

If your following this, I had a fault that was causing random pin functions to change from output to input, strange but true turns out it was a faulty serial to usb adapter, swapped it out and the buttons became stable only tried this because I had disconnected every other sensor and div board and it was the only thing left, anyway the following is my working code to switch all 16 outputs on a mcp23017 designated mcp0 in the config file, without calling any python macros, unlike buttons controlled via the restapi / ajax Post and Get it appears work fine via port forwarding, of coarse you need to be fully aware of the risks of port forwarding before you go down this route .


If you want to try this note the following.

There is a section that sets the function of all 16 pins on the mcp0 to OUTPUT 's the section can be deleted if you set the functions to OUTPUT in your script.py file.

Delete the line macro_set_mcp0(); // Call only once to set up functions on mcp0
and the lines between the ********************


Also the button styles are for use with chrome I didn't try them with other browsers, if you find it a problem just replace the 2 lines = background-image: bla bla to background-color: Red; Blue or whatever.


Hope it useful to someone


Toshi Bass


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" content = "height = device-height, width = 420, user-scalable = yes" />
        <title>mcp0</title>
        <script type="text/javascript" src="/webiopi.js"></script>
        <script type="text/javascript">
         webiopi().ready(function() {
              var content, button;
              content = $("#content");

button = webiopi().createButton("macro0", "mcp0 0 Off", callMacro_tog_mcp0_0);
$("#leftTop").append(button); // append button to left div

button = webiopi().createButton("macro1", "mcp0 1 Off", callMacro_tog_mcp0_1);
$("#left").append(button); // append button to left div

button = webiopi().createButton("macro2", "mcp0 2 Off", callMacro_tog_mcp0_2);
$("#rightTop").append(button); // append button to left div

button = webiopi().createButton("macro3", "mcp0 3 Off", callMacro_tog_mcp0_3);
$("#right").append(button); // append button to left div

button = webiopi().createButton("macro4", "mcp0 4 Off", callMacro_tog_mcp0_4);
$("#leftTop").append(button); // append button to right div

button = webiopi().createButton("macro5", "mcp0 5 Off", callMacro_tog_mcp0_5);
$("#left").append(button); // append button to right div

button = webiopi().createButton("macro6", "mcp0 6 Off", callMacro_tog_mcp0_6);
$("#rightTop").append(button); // append button to right div

button = webiopi().createButton("macro7", "mcp0 7 Off", callMacro_tog_mcp0_7);
$("#right").append(button); // append button to right div

button = webiopi().createButton("macro8", "mcp0 8 Off", callMacro_tog_mcp0_8);
$("#leftTop").append(button); // append button to left div

button = webiopi().createButton("macro9", "mcp0 9 Off", callMacro_tog_mcp0_9);
$("#left").append(button); // append button to left div

button = webiopi().createButton("macro10", "mcp0 10 Off", callMacro_tog_mcp0_10);
$("#rightTop").append(button); // append button to left div

button = webiopi().createButton("macro11", "mcp0 11 Off", callMacro_tog_mcp0_11);
$("#right").append(button); // append button to left div

button = webiopi().createButton("macro12", "mcp0 12 Off", callMacro_tog_mcp0_12);
$("#leftTop").append(button); // append button to right div

button = webiopi().createButton("macro13", "mcp0 13 Off", callMacro_tog_mcp0_13);
$("#left").append(button); // append button to right div

button = webiopi().createButton("macro14", "mcp0 14 Off", callMacro_tog_mcp0_14);
$("#rightTop").append(button); // append button to right div

button = webiopi().createButton("macro15", "mcp0 15 Off", callMacro_tog_mcp0_15);
$("#right").append(button); // append button to right div

              webiopi().refreshGPIO(true);
      });

       //************************************** i2c Buttons mcp0 pins 0 - 15 ***********************************************
       window.onload = function(){
macro_set_mcp0(); // Call only once to set up functions on mcp0 (see * note below)
macro_mcp0_callback();
}
//*******************************************************************************************************************
// Set all 16 pins on mcp0 to function type "OUTPUT" ( * only needed if you are not using script.py to set up functions)
function macro_set_mcp0(){
mcp0 = new GPIOPort("mcp0");
for (var i=0;i<16;i++)
mcp0.setFunction(i, "OUT", set_callback);
}
function set_callback(){
}
//*******************************************************************************************************************
// Get current state of mcp0 pin 0 to 15
function callMacro_tog_mcp0_0() {
mcp0 = new GPIOPort("mcp0");
mcp0.digitalRead(0, tog_callback);
}
function callMacro_tog_mcp0_1() {
mcp0 = new GPIOPort("mcp0");
mcp0.digitalRead(1, tog_callback);
}
function callMacro_tog_mcp0_2() {
mcp0 = new GPIOPort("mcp0");
mcp0.digitalRead(2, tog_callback);
}
function callMacro_tog_mcp0_3() {
mcp0 = new GPIOPort("mcp0");
mcp0.digitalRead(3, tog_callback);
}
function callMacro_tog_mcp0_4() {
mcp0 = new GPIOPort("mcp0");
mcp0.digitalRead(4, tog_callback);
}
function callMacro_tog_mcp0_5() {
mcp0 = new GPIOPort("mcp0");
mcp0.digitalRead(5, tog_callback);
}
function callMacro_tog_mcp0_6() {
mcp0 = new GPIOPort("mcp0");
mcp0.digitalRead(6, tog_callback);
}
function callMacro_tog_mcp0_7() {
mcp0 = new GPIOPort("mcp0");
mcp0.digitalRead(7, tog_callback);
}
function callMacro_tog_mcp0_8() {
mcp0 = new GPIOPort("mcp0");
mcp0.digitalRead(8, tog_callback);
}
function callMacro_tog_mcp0_9() {
mcp0 = new GPIOPort("mcp0");
mcp0.digitalRead(9, tog_callback);
}
function callMacro_tog_mcp0_10() {
mcp0 = new GPIOPort("mcp0");
mcp0.digitalRead(10, tog_callback);
}
function callMacro_tog_mcp0_11() {
mcp0 = new GPIOPort("mcp0");
mcp0.digitalRead(11, tog_callback);
}
function callMacro_tog_mcp0_12() {
mcp0 = new GPIOPort("mcp0");
mcp0.digitalRead(12, tog_callback);
}
function callMacro_tog_mcp0_13() {
mcp0 = new GPIOPort("mcp0");
mcp0.digitalRead(13, tog_callback);
}
function callMacro_tog_mcp0_14() {
mcp0 = new GPIOPort("mcp0");
mcp0.digitalRead(14, tog_callback);
}
function callMacro_tog_mcp0_15() {
mcp0 = new GPIOPort("mcp0");
mcp0.digitalRead(15, tog_callback);
}

       // Toggle mcp0 pin to opposite state
function tog_callback(mcp0, channel, value){
mcp0 = new GPIOPort("mcp0");
if (value==1){
mcp0.digitalWrite(channel, 0, mcp0_callback);
}
else{
mcp0.digitalWrite(channel, 1, mcp0_callback);
}
}

// Set mcp0 button color & label (needs to be separate from above function to keep buttons on other computers in sinc)

function mcp0_callback(mcp0, channel, value){
var i = channel; //Callback returns channel 0 - 15 

// Make label for bespoke button i==x (x being button number)
if (i==0){ // if separate label for button x is required use this code for the first bespoke button. 
if (value==1){
webiopi().setClass("macro"+i, "ON");
webiopi().setLabel("macro"+i, " TEST 0 On");
}
else{
webiopi().setClass("macro"+i, "OFF");
webiopi().setLabel("macro"+i, "TEST 0 Off");
}
}
// Make label for bespoke button i==x
else if (i==8){  // if separate labels for more bespoke buttons are required recreate this code for each button.
if (value==1){
webiopi().setClass("macro"+i, "ON");
webiopi().setLabel("macro"+i, " TEST 8 On");
}
else{
webiopi().setClass("macro"+i, "OFF");
webiopi().setLabel("macro"+i, "TEST 8 Off");
}
}
// Make label for bespoke button i==x
//else if (i==x    recreate the above code for each additional bespoke button here.

// Make label for all the rest of the generic buttons not specified above
else{
if (value==1){
webiopi().setClass("macro"+i, "ON");
webiopi().setLabel("macro"+i, mcp0 +"_"+channel+" On");
}
else{
webiopi().setClass("macro"+i, "OFF");
webiopi().setLabel("macro"+i, mcp0 +"_"+channel+" Off");
}
}
}

setInterval ("macro_mcp0_callback()", 5000);{
}
// Check state of mcp0 to reconfirm button color & label

function macro_mcp0_callback(){
mcp0 = new GPIOPort("mcp0");
for (var i=0;i<16;i++)
mcp0.digitalRead(i, mcp0_callback);
}

   </script>

  <style type="text/css">
  *:focus {     outline: none; }
  button { margin: -4px -2px 0px 0px; 
width: 100px; 
height: 40px;
//border-radius:1px;
//border:hidden;
font-size: 8pt; 
font-weight: 600;
color: Gray; }
 .OFF { color: Red;
background-image: -webkit-linear-gradient(top, #FCFCFC -50%, #778899 110.00000000000001%);
}
 .ON { color: Green;
background-image: -webkit-linear-gradient(#778899, #FCFCFC);
}
  </style>

</head>

<body>
    <div id="content" align="left">
        <div id="leftTop"></div>
        <div id="left"></div>
        <div id="rightTop"></div>
        <div id="right"></div>
    </div>
</body>

</html>

Eric PTAK

unread,
Dec 25, 2013, 5:49:13 PM12/25/13
to web...@googlegroups.com
topic starred ;)


--
Reply all
Reply to author
Forward
0 new messages