home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3934 < prev    next >
Encoding:
Internet Message Format  |  1991-08-29  |  7.9 KB

  1. Path: wupost!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!ucbvax!bloom-beacon!eru!hagbard!sunic!lth.se!newsuser
  2. From: d89cb@efd.lth.se (Christian Brunschen)
  3. Newsgroups: alt.sources
  4. Subject: LED's on a vt100
  5. Message-ID: <1991Aug29.075659.360@lth.se>
  6. Date: 29 Aug 91 07:56:59 GMT
  7. Sender: newsuser@lth.se (LTH network news server)
  8. Reply-To: d89cb@efd.lth.se (Christian Brunschen)
  9. Organization: Lund Institute of Technology, Sweden
  10. Lines: 332
  11.  
  12.  
  13. Here comes a little program that will do some interesting things with the
  14. LED's on a vt100-compatible. The Control sequences are hardcoded, so it
  15. isn't easily portable to other terminal types, and it requires a system that 
  16. has the usleep() system call, but other than that, it should run pretty much
  17. everywhere .. I hope. It has only been tested on a few Sun 3/50 and 3/80s
  18. under SunOS 4.1 ... 
  19.  
  20.  
  21. // Christian
  22.  
  23. --
  24. +----------------------------------+------------------------+
  25. | Christian Brunschen              |   SnailMail:           |
  26. | Internet : d89cb@efd.lth.se      |   Husmansv{gen 26      |
  27. | IRC      : snooker               |   S - 222 38 Lund      |
  28. | Phone #  : +46 46 131984         |   Sweden               |
  29. +----------------------------------+------------------------+
  30. | Unix is a registered bell of AT&T trademark laboratories. |
  31. +-----------------------------------------------------------+
  32. | All above mentioned opinions are (c) Christian Brunschen, |
  33. | unless otherwise stated. Feel free to copy & distribute.  |
  34. +-----------------------------------------------------------+
  35.  
  36. --- cut here ---
  37. #!/bin/sh
  38. # This is a shell archive.  Remove anything before the "#!/bin/sh" line
  39. # then unpack it by saving it in a file and typing "sh file"
  40. # (Files unpacked will be owned by you and have default permissions).
  41. # This archive contains the following files:
  42. #    ./leds.c
  43. #    ./leds.mk
  44. #    ./README.leds
  45. #
  46. if `test ! -s ./leds.c`
  47. then
  48. echo "writing ./leds.c"
  49. sed 's/^Z//' > ./leds.c << '___END_OF_THIS_FILE'
  50. Z#include <stdio.h>
  51. Z#include <sys/types.h>
  52. Z#include <sys/time.h>
  53. Z
  54. Zenum {BOUNCE, ROLL_LEFT, ROLL_RIGHT, RANDOM, N_MODES};
  55. Z
  56. Zchar * led_progname;
  57. Z
  58. Zvoid led_error (s1, s2)
  59. Zchar * s1, s2;
  60. Z{
  61. Z   fprintf (stderr, "%s: %s%s\n", led_progname, s1, s2);
  62. Z   exit (-1);
  63. Z}
  64. Z
  65. Zvoid light (i)
  66. Zint i;
  67. Z{
  68. Z   printf ("\033[%iq", i);
  69. Z}
  70. Z
  71. Zvoid flash (i)
  72. Zint i;
  73. Z{
  74. Z   printf ("\033[?%it", i);
  75. Z}
  76. Z
  77. Zvoid clear ()
  78. Z{
  79. Z   printf ("\033[0q\033[?0t");
  80. Z}
  81. Z
  82. Zvoid load (v)
  83. Zint v;
  84. Z{
  85. Z   int i, y;
  86. Z   y=v;
  87. Z   clear();
  88. Z   for (i=4; i>0; i--)
  89. Z   {
  90. Z      switch (y % 10)
  91. Z      {
  92. Z      case 0: /* dark */
  93. Z     /* do nothing */
  94. Z     break;
  95. Z      case 1: /* lit */
  96. Z     light (i);
  97. Z     break;
  98. Z      case 2: /* flashing */
  99. Z     flash (i);
  100. Z     break;
  101. Z      }
  102. Z      y /= 10;
  103. Z   }
  104. Z}
  105. Z
  106. Zvoid bounce (t)
  107. Zunsigned int t;
  108. Z{
  109. Z   int i;
  110. Z   unsigned int s;
  111. Z   s = t / 6;
  112. Z   for (i=1; i<4; i++)
  113. Z   {
  114. Z      clear();
  115. Z      light (i);
  116. Z      fflush (stdout);
  117. Z      usleep (s);
  118. Z   }
  119. Z   for (i=4; i>1; i--)
  120. Z   {
  121. Z      clear();
  122. Z      light (i);
  123. Z      fflush (stdout);
  124. Z      usleep (s);
  125. Z   }
  126. Z}
  127. Z
  128. Zvoid roll (t, min, max, step)
  129. Zunsigned int t;
  130. Zint min, max, step;
  131. Z{
  132. Z   int i;
  133. Z   unsigned int s;
  134. Z   s = t / ((abs (max - min)) / abs (step));
  135. Z   for (i = min; i != max; i += step)
  136. Z   {
  137. Z      clear();
  138. Z      light (i);
  139. Z      fflush (stdout);
  140. Z      usleep (s);
  141. Z   }
  142. Z}
  143. Z
  144. Zvoid random (t)
  145. Zunsigned int t;
  146. Z{
  147. Z   int i, ran, mran;
  148. Z   clear();
  149. Z   ran = rand();
  150. Z   for (i=1; i<5; i++)
  151. Z   {
  152. Z      mran = ran % 3;
  153. Z      switch (mran)
  154. Z      {
  155. Z      case 0: /* dark */
  156. Z     /* do nothing -- cleared */
  157. Z     break;
  158. Z      case 1: /* lit */
  159. Z     light (i);
  160. Z     break;
  161. Z      case 2: /* flashing */
  162. Z     flash (i);
  163. Z     break;
  164. Z      }
  165. Z      ran /= 3;
  166. Z   }
  167. Z   fflush (stdout);
  168. Z   usleep (t);
  169. Z}
  170. Z
  171. Zmain(argc, argv)
  172. Zint argc;
  173. Zchar ** argv;
  174. Z{
  175. Z   unsigned int time_between=1000000, argn, mode=0, random_mode = 0;
  176. Z   led_progname = argv[0];
  177. Z   srand (time(NULL));
  178. Z   for (argn = 1; argn < argc && argv[argn][0] == '-'; argn++)
  179. Z   {
  180. Z      switch (argv[argn][1])
  181. Z      {
  182. Z      case 't':
  183. Z     time_between = 1000 * atoi(argv[++argn]);
  184. Z     break;
  185. Z      case 'm':
  186. Z     argn++;
  187. Z     if (!strcmp(argv[argn], "bounce"))
  188. Z        mode = BOUNCE;
  189. Z     else if (!strcmp(argv[argn], "left"))
  190. Z        mode = ROLL_LEFT;
  191. Z     else if (!strcmp(argv[argn], "right"))
  192. Z        mode = ROLL_RIGHT;
  193. Z     else if (!strcmp(argv[argn], "random"))
  194. Z        mode = RANDOM;
  195. Z     else if (!strcmp(argv[argn], "mode"))
  196. Z        random_mode = 1;
  197. Z     else
  198. Z        led_error ("Unknown Mode : ", argv[argn]);
  199. Z     break;
  200. Z      case 'l':
  201. Z     load (atoi(argv[++argn]));
  202. Z     exit (0);
  203. Z      default:
  204. Z     led_error ("Unkown Flag : ", argv[argn]);
  205. Z      }
  206. Z   }
  207. Z   if (argn < argc)
  208. Z      led_error ("Unknown Option : ", argv[argn]);
  209. Z
  210. Z   while (1)
  211. Z   {
  212. Z      if (random_mode)
  213. Z     mode = rand () % N_MODES;
  214. Z      switch (mode)
  215. Z      {
  216. Z      case BOUNCE:
  217. Z     bounce (time_between);
  218. Z     break;
  219. Z      case ROLL_LEFT:
  220. Z     roll (time_between, 1, 5, 1);
  221. Z     break;
  222. Z      case ROLL_RIGHT:
  223. Z     roll (time_between, 4, 0, -1);
  224. Z     break;
  225. Z      case RANDOM:
  226. Z     random (time_between);
  227. Z     break;
  228. Z      }
  229. Z   }
  230. Z}
  231. Z
  232. ___END_OF_THIS_FILE
  233. else
  234.   echo "will not over write ./leds.c"
  235. fi
  236. if [ `wc -c ./leds.c | awk '{printf $1}'` -ne 2943 ]
  237. then
  238. echo `wc -c ./leds.c | awk '{print "Got " $1 ", Expected " 2943}'`
  239. fi
  240. if `test ! -s ./leds.mk`
  241. then
  242. echo "writing ./leds.mk"
  243. sed 's/^Z//' > ./leds.mk << '___END_OF_THIS_FILE'
  244. ZCC=    gcc
  245. ZCFLAGS=    -O -s
  246. Z
  247. Zleds : leds.c
  248. Z    $(CC) $(CFLAGS) -o leds leds.c
  249. ___END_OF_THIS_FILE
  250. else
  251.   echo "will not over write ./leds.mk"
  252. fi
  253. if [ `wc -c ./leds.mk | awk '{printf $1}'` -ne 69 ]
  254. then
  255. echo `wc -c ./leds.mk | awk '{print "Got " $1 ", Expected " 69}'`
  256. fi
  257. if `test ! -s ./README.leds`
  258. then
  259. echo "writing ./README.leds"
  260. sed 's/^Z//' > ./README.leds << '___END_OF_THIS_FILE'
  261. Zleds.README
  262. Z===========
  263. Z
  264. ZAn introduction to the `leds' program.
  265. Z
  266. ZWhat does it do ?
  267. Z=================
  268. ZNot much. It plays with the 4 little LED's that can be found on the keyboard
  269. Zof a vt100-compatible terminal. It can display a number of different pattern,
  270. Zand can be used to load your own ones as well.
  271. Z
  272. ZSynopsis:
  273. Z
  274. Z    leds [[-t time] [-m mode] | [-l pattern]]
  275. Z
  276. ZOptions:
  277. Z
  278. Z    -t time
  279. Z        sets the time it takes for leds to conclude 1 `round'
  280. Z        of the current pattern. Used only with  -m mode.
  281. Z
  282. Z    -m mode
  283. Z        sets `leds' to run continuously, displaying a patter according
  284. Z        to `mode'. `mode' may be one of:
  285. Z
  286. Z        `bounce'
  287. Z            one lit led bounces back and forth, left-right.
  288. Z
  289. Z        `left'
  290. Z            one lit led `rolls' from right to left, again and 
  291. Z            again.
  292. Z
  293. Z        `right'
  294. Z            as `left', but miving left-to-right instead.
  295. Z
  296. Z        `random'
  297. Z            after each interval, a new random pattern of lit,
  298. Z            dark and flashing led's is loaded.
  299. Z
  300. Z        `mode'
  301. Z            after each interval, one of the other modes is
  302. Z            randomly chosen, and displayed for the next interval.
  303. Z
  304. Z    -l value
  305. Z        loads the led's with `value', which is a 4-digit decimal
  306. Z        number, where each digit corresponds to one led, and is
  307. Z        interpreted as:
  308. Z
  309. Z        0    dark
  310. Z        1    lit
  311. Z        2    flashing
  312. Z
  313. Z        All other digits are ignored.
  314. Z
  315. ZBUGS
  316. Z    No much error checking. Bogus options usually result in a core dump.
  317. Z
  318. Z    No real manual page -- just this README file.
  319. Z
  320. ZFEATURES
  321. Z    Since this program does an `fflush' after each change in the LED's,
  322. Z    it can be run as a background process, and , even over a serial line,
  323. Z    the output from `leds', does not interfere with other output (since it
  324. Z    is intercepted by the terminal), as long as you are running something
  325. Z    interactive (like a shell or so). However, it quite naturally _would_
  326. Z    interfere with any files you tried to download :) This, btw, is not
  327. Z    something I have researched -- but it _is_ observed behaviour on a
  328. Z    Sun 3/80 with a vt100 over a modem at 2400 baud.
  329. Z
  330. ZAUTHOR
  331. Z    Christian Brunschen (d89cb@efd.lth.se)
  332. Z
  333. ___END_OF_THIS_FILE
  334. else
  335.   echo "will not over write ./README.leds"
  336. fi
  337. if [ `wc -c ./README.leds | awk '{printf $1}'` -ne 1945 ]
  338. then
  339. echo `wc -c ./README.leds | awk '{print "Got " $1 ", Expected " 1945}'`
  340. fi
  341. echo "Finished archive 1 of 1"
  342. # if you want to concatenate archives, remove anything after this line
  343. exit
  344.