Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

serialHelper.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * unixHelper.c
  3. *
  4. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  5. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  6. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  7. *
  8. * This file is part of LED-Cube.
  9. *
  10. * LED-Cube is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * LED-Cube is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <jni.h>
  26. #include "serialInterface.h"
  27. #ifdef winHelper
  28. #include "winSerial.c"
  29. #else
  30. #include "unixSerial.c"
  31. #endif
  32. JNIEXPORT jstring JNICALL Java_HelperUtility_getThePorts(JNIEnv *env, jclass class, jstring s) {
  33. jboolean tmp;
  34. char **ports = getSerialPorts((*env)->GetStringUTFChars(env, s, &tmp));
  35. char *string = NULL;
  36. int length = 0, leng2 = 0, lengthabs = 0;
  37. // printf("JNI: Got serial ports...\n");
  38. // Count how much memory we need for string of all ports, with \n in between
  39. while (ports[length] != NULL) {
  40. // printf("JNI: Starting count... (%d at %p)\n", length, (void *)ports[length]);
  41. while (ports[length][leng2] != '\0') {
  42. leng2++;
  43. }
  44. // printf("JNI: Counted %s\n", ports[length]);
  45. lengthabs += leng2;
  46. leng2 = 0;
  47. length++;
  48. }
  49. length += lengthabs;
  50. // printf("JNI: Counted serial ports...\n");
  51. string = (char *)malloc((length + 1) * sizeof(char));
  52. if (string == NULL) {
  53. printf("JNI: Not enough memory!\n");
  54. return (*env)->NewStringUTF(env, NULL);
  55. }
  56. length = 0;
  57. lengthabs = 0;
  58. while (ports[length] != NULL) {
  59. leng2 = 0;
  60. while (ports[length][leng2] != '\0') {
  61. string[lengthabs++] = ports[length][leng2++];
  62. }
  63. string[lengthabs++] = '\n';
  64. length++;
  65. }
  66. string[lengthabs] = '\0';
  67. // printf("JNI: %s\n", string);
  68. jstring ret = (*env)->NewStringUTF(env, string);
  69. return ret;
  70. }
  71. JNIEXPORT jshortArray JNICALL Java_HelperUtility_readDataNative(JNIEnv *env, jclass class, jint length) {
  72. jshortArray arr = (*env)->NewShortArray(env, length);
  73. int toBeRead = 0, read, i, error = 0;
  74. char *data = (char *)malloc(length * sizeof(char));
  75. jshort *data2 = (jshort *)malloc(length * sizeof(jshort));
  76. while (length > 0) {
  77. read = serialRead(data + toBeRead, length);
  78. if (read == -1) {
  79. error++;
  80. if (error > 10) {
  81. return (*env)->NewShortArray(env, 0);
  82. }
  83. } else {
  84. toBeRead += read;
  85. length -= read;
  86. }
  87. }
  88. for (i = 0; i < (*env)->GetArrayLength(env, arr); i++) {
  89. data2[i] = data[i];
  90. }
  91. (*env)->SetShortArrayRegion(env, arr, 0, (*env)->GetArrayLength(env, arr), data2);
  92. return arr;
  93. }
  94. JNIEXPORT jboolean JNICALL Java_HelperUtility_writeDataNative(JNIEnv *env, jclass class, jshortArray data, jint length) {
  95. int toWrite = length, written = 0, now, i, error = 0;
  96. char *dat = (char *)malloc(length * sizeof(char));
  97. jshort *dat2 = (jshort *)malloc(length * sizeof(jshort));
  98. // Get unwritten data from argument, place in dat2
  99. // move from dat2 into dat1
  100. // write dat1
  101. // repeat
  102. while (toWrite > 0) {
  103. (*env)->GetShortArrayRegion(env, data, written, length, dat2); // Unwritten part of data in dat2
  104. for (i = 0; i < (length - written); i++) {
  105. dat[i] = dat2[i];
  106. }
  107. now = serialWrite(dat, toWrite);
  108. if (now == -1) {
  109. error++;
  110. if (error > 10) {
  111. return JNI_FALSE;
  112. }
  113. } else {
  114. written += now;
  115. toWrite -= now;
  116. }
  117. }
  118. return JNI_TRUE;
  119. }
  120. JNIEXPORT void JNICALL Java_HelperUtility_closePortNative(JNIEnv * env, jclass class) {
  121. serialClose();
  122. }
  123. JNIEXPORT jboolean JNICALL Java_HelperUtility_openPortNative(JNIEnv *env, jclass class, jstring name) {
  124. jboolean isCopy;
  125. const char *path = (*env)->GetStringUTFChars(env, name, &isCopy);
  126. int ret = serialOpen((char *)path);
  127. if (ret == 0) {
  128. return JNI_TRUE;
  129. } else {
  130. return JNI_FALSE;
  131. }
  132. }