Skip to content

Instantly share code, notes, and snippets.

@mardini1974
Created November 11, 2017 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mardini1974/0f6a896cfe83a588284939a020da7c7a to your computer and use it in GitHub Desktop.
Save mardini1974/0f6a896cfe83a588284939a020da7c7a to your computer and use it in GitHub Desktop.
component pokeys "PoKeys IO driver, by Mit Zot";
option userspace yes;
pin out bit in-# [55];
pin out bit inext-# [16];
pin out unsigned ain-# [3];
pin out bit err;
pin in unsigned devSerial;
pin out bit alive;
license "GPL";
;;
#include "PoKeysLib.h"
#include <unistd.h> /* UNIX standard function definitions */
sPoKeysDevice * dev=0;
int i=0;
bool notsetup=true;
unsigned char readBuffer[1];
int status=0;
int writeI2C(sPoKeysDevice *PK,uint8_t Reg,uint8_t Value)
{
uint8_t status;
usleep(1000);
unsigned char cmd1[]= {Reg,Value};
PK_I2CWriteStart(PK, 0x20, cmd1, 2);
usleep(1000);
PK_I2CWriteStatusGet(PK, &status);
return status;
}
int readI2C(sPoKeysDevice *PK,uint8_t Reg,unsigned char* buffer,uint8_t length)
{
unsigned char cmd[] = {Reg};
int i;
uint8_t status;
PK_I2CWriteStart(PK, 0x20, cmd, 1);
// Read bytes
PK_I2CReadStart(PK, 0x20, length);
unsigned char readBytes;
unsigned char readBuffer[length];
usleep(1000);
PK_I2CReadStatusGet(PK, &status, &readBytes, readBuffer, length);
if (status == PK_I2C_STAT_COMPLETE)
{
for (i = 0; i < length ; i++)
{
buffer[i] = readBuffer[i];
}
}
return status;
}
void user_mainloop(void)
{
while(0xb){
FOR_ALL_INSTS() {
while(dev == NULL)dev = PK_ConnectToDeviceWSerial(devSerial, 2000); //waits for usb device
alive=1;
if ((PK_DigitalIOGet(dev) == PK_OK) && (PK_AnalogIOGet(dev) == PK_OK)){ //gets IO data and checks return value
err=0;
for(i=0;i<54;i++)in(i)=!dev->Pins[i].DigitalValueGet; //just transfers values
for(i=0;i<3;i++)ain(i)=dev->Pins[i+44].AnalogValue;
//if (notsetup)
//{
status = writeI2C(dev,0x00,255);
status |= writeI2C(dev,0x01,255);
status |= writeI2C(dev,0x0C,255);
status |= writeI2C(dev,0x0D,255);
//if (status == PK_I2C_STAT_COMPLETE)
//{notsetup = false;}}
//if (!notsetup)
//{
status = readI2C(dev,0x12,readBuffer,sizeof readBuffer);
for (i=0;i<8;i++)inext(i) = (readBuffer[0]>>i)&0x01;
status = readI2C(dev,0x13,readBuffer,sizeof readBuffer);
for (i=0;i<8;i++)inext(i+8) = (readBuffer[0]>>i)&0x01;
//}
//}
}
else{ //on connection error
PK_DisconnectDevice(dev);
dev=NULL; //tries to reconnect
err=1;
for(i=0;i<54;i++)in(i)=0;
for(i=0;i<3;i++)ain(i)=0;
}
alive=0;
usleep(40000);
}
}
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment