void* blinkerThread(void* param) { struct threadParams * p = (struct threadParams *) param; int tid = pthread_self() ; int pin6=6, pin7=7; int bit6 = 1 << (pin6 & 7) ; int bit7 = 1 << (pin7 & 7) ; int fd = p->fd ; int value = LOW; // int old = p->old ; printf("%d: Blinker thread started...\n", tid) ; wiringPiI2CWriteReg8 (fd, MCP23x17_IOCON, 0b01100100) ; // mirror interrupts, disable sequential mode, open drain unsigned int nInt = wiringPiI2CReadReg8 (fd, MCP23x17_IOCON ); // Read printf("IOCONA="); bin_prnt_byte(nInt); wiringPiI2CWriteReg8 (fd, MCP23x17_IODIRA, 0x0) ; // Set direction as output - 5 B ports nInt = wiringPiI2CReadReg8 (fd, MCP23x17_IODIRA ); // Read printf("IODIRA="); bin_prnt_byte(nInt); wiringPiI2CWriteReg8 (fd, MCP23x17_IPOLA, 0x0) ; // Set POLARITY bit same as GPIO nInt = wiringPiI2CReadReg8 (fd, MCP23x17_IPOLA ); // Read printf("IPOLA="); bin_prnt_byte(nInt); p->old = wiringPiI2CReadReg8 (fd, MCP23x17_OLATA ); // Read printf("OLATA="); bin_prnt_byte(nInt); printf ("Port A setup completed.\n") ; while (g_ShouldRun) { while(!g_EndBlink) { if (value == LOW) p->old &= (~bit6) ; else p->old |= bit6 ; wiringPiI2CWriteReg8 (fd, MCP23x17_GPIOA, p->old); delay(1000); if (value == LOW) p->old &= (~bit7) ; else p->old |= bit7 ; wiringPiI2CWriteReg8 (fd, MCP23x17_GPIOA, p->old); value = !value ; //printf ("Value=") ; bin_prnt_byte(old) ; } wiringPiI2CWriteReg8 (fd, MCP23x17_GPIOA, 0); delay (1000); //grace sleep } printf("%d: Blinker thread: Cleaning and exiting...\n", tid) ; wiringPiI2CWriteReg8 (fd, MCP23x17_GPIOA, 0); return NULL; }
void* myThread(void* param) { unsigned int nInt = 0; struct threadParams* p = (struct threadParams*) param; int fd = p->fd ; unsigned int nStamp; int tid = pthread_self() ; int value=HIGH ; int longpress = 0; // set up to generate interrupts on INTB on pin B0 <> default value (1) //wiringPiI2CWriteReg8 (fd, MCP23x17_IODIRB, 0x03) ; wiringPiI2CWriteReg8 (fd, MCP23x17_IOCONB, 0b01101100) ; // 7. register banks same, 6.mirror interrupts, 5.enable sequential mode, 4.DSSLW enabled, 3.HAEN=1, 2.open drain=0, 1.active-low, 0.unimpl nInt = wiringPiI2CReadReg8 (fd, MCP23x17_IOCONB ); // Read printf("IOCONB="); bin_prnt_byte(nInt); nInt = wiringPiI2CReadReg8 (fd, MCP23x17_INTCONB ); // Read printf("INTCONB(Before)="); bin_prnt_byte(nInt); wiringPiI2CWriteReg8 (fd, MCP23x17_INTCONB, 0b00000001) ; nInt = wiringPiI2CReadReg8 (fd, MCP23x17_INTCONB ); // Read printf("INTCONB="); bin_prnt_byte(nInt); wiringPiI2CWriteReg8 (fd, MCP23x17_GPPUB, 0x01) ; // pull-up resistor for switches - 5 B ports nInt = wiringPiI2CReadReg8 (fd, MCP23x17_GPPUB ); // Read printf("GPPUB="); bin_prnt_byte(nInt); wiringPiI2CWriteReg8 (fd, MCP23x17_IPOLB, 0x01) ; // invert polarity of signal nInt = wiringPiI2CReadReg8 (fd, MCP23x17_IPOLB ); // Read printf("IOPOLB="); bin_prnt_byte(nInt); wiringPiI2CWriteReg8 (fd, MCP23x17_GPINTENB, 0x01) ; // enable interrupts nInt = wiringPiI2CReadReg8 (fd, MCP23x17_GPINTENB ); // Read printf("GPINTENB="); bin_prnt_byte(nInt); nInt = wiringPiI2CReadReg8 (fd, MCP23x17_INTCAPB ); // clear interrupt flag printf("INTCAPB-") ; bin_prnt_byte(nInt) ; nInt = wiringPiI2CReadReg8 (fd, MCP23x17_GPIOB ); // clear interrupt printf("GPIOB="); bin_prnt_byte(nInt); printf("Port B setup completed.\n") ; //wiringPiI2CWriteReg8 (fd, MCP23x17_IODIRB, 0x1f) ; //wiringPiI2CWriteReg8 (fd, MCP23x17_INTCONB, 0x1f) ; //wiringPiI2CWriteReg8 (fd, MCP23x17_DEFVALB, 0x1f) ; //wiringPiI2CWriteReg8 (fd, MCP23x17_GPINTENB, 0x1f) ; //wiringPiI2CReadReg8 (fd, MCP23x17_INTCAPB ); // clear interrupt flag printf("%d: Interrupt thread started...\n", tid) ; while (g_ShouldRun) { if (g_Interrupt) { nStamp = millis() ; printf("\n\nInterrupt handling @ %d\n", nStamp) ; while (wiringPiI2CReadReg8 (fd, MCP23x17_GPIOB) == 0x01 ) { // Loop to detect long press //break ; printf(".") ; delay (100) ; } g_Interrupt = 0; printf("\n") ; longpress = (millis() - nStamp); if ( longpress <= 500) g_EndBlink = !(g_EndBlink); else { printf("Time interval (for longpress) - %d\n", longpress) ; invLED(p, value) ; value = !value; } printf("Processed in %dms\n",longpress) ; delay (100); //debounce printf("Clearing registers...\n") ; nInt = wiringPiI2CReadReg8 (fd, MCP23x17_INTCAPB ); // clear interrupt printf("INTCAPB="); bin_prnt_byte(nInt); nInt = wiringPiI2CReadReg8 (fd, MCP23x17_GPIOB ); // clear interrupt printf("GPIOB="); bin_prnt_byte(nInt); // nInt = wiringPiI2CReadReg8 (fd, MCP23x17_GPINTENB ); // Read // printf("GPINTENB(Before)="); bin_prnt_byte(nInt); // wiringPiI2CWriteReg8 (fd, MCP23x17_GPINTENB, 0b00000001) ; // nInt = wiringPiI2CReadReg8 (fd, MCP23x17_GPINTENB ); // Read // printf("GPINTENB="); bin_prnt_byte(nInt); // nInt = wiringPiI2CReadReg8 (fd, MCP23x17_INTCAPB ); // clear interrupt // printf("INTCAPB="); bin_prnt_byte(nInt); // nInt = wiringPiI2CReadReg8 (fd, MCP23x17_GPIOB ); // clear interrupt // printf("GPIOB="); bin_prnt_byte(nInt); } delay(500); } printf("%d: Interrupt thread cleaning up...\n", tid) ; return NULL; }
/*Convenience function for making buttons with icons on them*/ Window make_unmapped_icon_window(Window root, int x, int y, int width, int height, int bw, int icx, int icy, unsigned char *icdata) { Window win = make_plain_unmapped_window(root, x, y, width, height, bw); Pixmap pmap = XCreatePixmap(display, root, width, height, DefaultDepth(display, DefaultScreen(display))); Colormap cmap = DefaultColormap(display, DefaultScreen(display)); if (UserGradients == 1) { draw_gradient(pmap, width, height); } else { XColor bcolor; XParseColor(display, cmap, UserWhite, &bcolor); XAllocColor(display, cmap, &bcolor); XSetForeground(display, gc, bcolor.pixel); XDrawRectangle(display, pmap, gc, 0, 0, width, height); } int z = 0, row = 0, col = 0; if (icdata != NULL) { XColor diffcol; XParseColor(display, cmap, UserBlack, &diffcol); XAllocColor(display, cmap, &diffcol); XSetForeground(display, gc, diffcol.pixel); unsigned char *ps = icdata; int intstack[8]; col = 0; row = -1; z = 0; while (row < height) { col = 0; row++; while (1) { bin_prnt_byte(*ps, intstack); ps++; int q = 0; for (q = 0; q < 8; q++) /*8 bits per byte*/ { if (col >= width) { /*myint[z] = 0;*/ } else { if (intstack[q] == 1) { XDrawPoint(display, pmap, gc, col, row); } } z++; col++; } if (col >= width) { break; } } } } XSetWindowBackgroundPixmap(display, win, pmap); XFreePixmap(display, pmap); return win; }