|
|
Line 83: |
Line 83: |
|
| |
|
| |} | | |} |
|
| |
| This header is designed to connect to the [[KPAD]] accessory which uses the odd DIO on this header to scan a 4x4 keypad. This example scans the [[KPAD]] and prints out the pressed character. This requires a running tsctl server, and a libtsctl source directory on the board to compile:
| |
| <source lang=c>
| |
| /* KPAD 4x4 keypad example code
| |
| *
| |
| * To compile, copy to the libtsctl directory and run:
| |
| * gcc kpad.c -Inet -Its -I . -o kpad */
| |
|
| |
| #include <stdio.h>
| |
| #include <stdint.h>
| |
| #include <unistd.h>
| |
|
| |
| #include "nettsctl.h"
| |
|
| |
| int main()
| |
| {
| |
| int row, col, i, dio_blk[8];
| |
| tsctl *conn;
| |
| DIO *dio;
| |
| System *sys;
| |
|
| |
| char *keys[4][4] = {
| |
| { "1", "2", "3", "UP" },
| |
| { "4", "5", "6", "DOWN" },
| |
| { "7", "8", "9", "2ND" },
| |
| { "CLEAR", "0", "HELP", "ENTER" }
| |
| };
| |
|
| |
| conn = TsctlClient("127.0.0.1",NetModeBlocking);
| |
| if (!conn) {
| |
| perror("Couldn't connect to TSCTL server");
| |
| return 1;
| |
| }
| |
| dio = NetDIOInit(conn, 0);
| |
| sys = NetSystemInit(conn, 0);
| |
| if(sys->BaseBoardId(sys) != 0x8100) {
| |
| fprintf(stderr, "Wrong Baseboard.\n");
| |
| return 1;
| |
| }
| |
|
| |
| // Lookup DIO (odds) 1-15 on the TS-8100
| |
| for(i = 0; i < 8; i++) {
| |
| char nm[13];
| |
| snprintf(nm, 12, "8100_DIO_%d", (i*2)+1);
| |
| dio_blk[i] = sys->MapLookup(sys, ASCIIZ(nm));
| |
| // Set first 4 as outputs, last 4 as inputs
| |
| if(i < 4)
| |
| dio->SetAsync(dio, dio_blk[i], HIGH);
| |
| else
| |
| dio->SetAsync(dio, dio_blk[i], INPUT);
| |
| }
| |
|
| |
| while(1) {
| |
| for(row = 0; row < 4; row++) {
| |
| dio->SetAsync(dio, dio_blk[row], LOW);
| |
| usleep(1000);
| |
| for(col = 4; col < 8; col++) {
| |
| if(dio->GetAsync(dio, dio_blk[col]) == INPUT_LOW) {
| |
| usleep(80000);
| |
| if(dio->GetAsync(dio, dio_blk[col]) == INPUT_LOW) {
| |
| printf("%s ", keys[row][col - 4]);
| |
| fflush(stdout);
| |
| }
| |
| }
| |
| }
| |
| dio->SetAsync(dio, dio_blk[row], HIGH);
| |
| }
| |
| }
| |
|
| |
| return 0;
| |
| }
| |
| </source>
| |
|
| |
| If you receive a "socket error", see the [[#tsctl|tsctl]] chapter for information on setting up and running the tsctl server.
| |