reform-tiny-fw.ino 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * MNT Reform 0.3+ ATtiny 841 controller firmware
  3. * Copyright 2018 MNT Media and Technology UG, Berlin
  4. * SPDX-License-Identifier: GPL-3.0-or-later
  5. */
  6. #define SCL_PIN 0
  7. #define SCL_PORT PORTA
  8. #define SDA_PIN 1
  9. #define SDA_PORT PORTA
  10. #define HALL_SENSOR_PIN A5
  11. #define HALL_SENSOR_SUPPLY_PIN 4
  12. #define INA_ADDR 0x4e
  13. #include <SoftI2CMaster.h>
  14. #include <SoftwareSerial.h>
  15. int16_t ina_read16(unsigned char reg) {
  16. uint16_t val = 0;
  17. if (i2c_start((INA_ADDR << 1) | I2C_WRITE)) {
  18. i2c_write(reg);
  19. i2c_rep_start((INA_ADDR << 1) | I2C_READ);
  20. val = ((uint16_t)i2c_read(false)) << 8;
  21. val |= i2c_read(true);
  22. i2c_stop();
  23. }
  24. return val;
  25. }
  26. #define ST_EXPECT_DIGIT_0 0
  27. #define ST_EXPECT_DIGIT_1 1
  28. #define ST_EXPECT_DIGIT_2 2
  29. #define ST_EXPECT_DIGIT_3 3
  30. #define ST_EXPECT_CMD 4
  31. #define ST_SYNTAX_ERROR 5
  32. #define ST_EXPECT_RETURN 6
  33. #define LID_CLOSED 1
  34. #define LID_OPEN 0
  35. SoftwareSerial softSerial(8, 3);
  36. float ampSecs = 5*3600.0;
  37. unsigned char hallState = LID_OPEN;
  38. int thresh = 500;
  39. int window = 10;
  40. int hallSense = 0;
  41. unsigned char state = ST_EXPECT_DIGIT_0;
  42. unsigned int inputNumber = 0;
  43. unsigned long lastTime = 0;
  44. float volts = 0;
  45. float current = 0;
  46. char cmd = 'a';
  47. unsigned char echo = 1;
  48. // TODO if there is no battery power, ignore lid sensor (values >900)
  49. void handleLidSensor() {
  50. hallSense = analogRead(HALL_SENSOR_PIN);
  51. if (hallSense>(thresh+window) && hallState==LID_OPEN) {
  52. hallState = LID_CLOSED;
  53. }
  54. if (hallSense<(thresh+window) && hallState==LID_CLOSED) {
  55. softSerial.println("event:wake");
  56. hallState = LID_OPEN;
  57. }
  58. }
  59. void handleCommands() {
  60. char chr = softSerial.read();
  61. if (echo) softSerial.print(chr);
  62. // states:
  63. // 0-3 digits of optional command argument
  64. // 4 command letter expected
  65. // 5 syntax error (unexpected character)
  66. // 6 command letter entered
  67. if (state>=ST_EXPECT_DIGIT_0 && state<=ST_EXPECT_DIGIT_3) {
  68. // read number or command
  69. if (chr >= '0' && chr <= '9') {
  70. inputNumber*=10;
  71. inputNumber+=(chr-'0');
  72. state++;
  73. } else if (chr >= 'a' && chr <= 'z') {
  74. // command entered instead of digit
  75. cmd = chr;
  76. state = ST_EXPECT_RETURN;
  77. } else if (chr == '\n' || chr == ' ') {
  78. // ignore newlines or spaces
  79. } else if (chr == '\r') {
  80. softSerial.println("error:syntax");
  81. state = ST_EXPECT_DIGIT_0;
  82. inputNumber = 0;
  83. } else {
  84. // syntax error
  85. state = ST_SYNTAX_ERROR;
  86. }
  87. }
  88. else if (state == ST_EXPECT_CMD) {
  89. // read command
  90. if (chr >= 'a' && chr <= 'z') {
  91. cmd = chr;
  92. state = ST_EXPECT_RETURN;
  93. } else {
  94. state = ST_SYNTAX_ERROR;
  95. }
  96. }
  97. else if (state == ST_SYNTAX_ERROR) {
  98. // syntax error
  99. if (chr == '\r') {
  100. softSerial.println("error:syntax");
  101. state = ST_EXPECT_DIGIT_0;
  102. inputNumber = 0;
  103. }
  104. }
  105. else if (state == ST_EXPECT_RETURN) {
  106. if (chr == '\n' or chr == ' ') {
  107. // ignore newlines or spaces
  108. }
  109. else if (chr == '\r') {
  110. // execute
  111. if (cmd == 'p') {
  112. // print power stats
  113. softSerial.print("power(Ah,V,A):");
  114. softSerial.print(ampSecs/3600.0);
  115. softSerial.print("\t");
  116. softSerial.print(volts);
  117. softSerial.print("\t");
  118. softSerial.println(current);
  119. }
  120. else if (cmd == 'c') {
  121. // set battery capacity
  122. if (inputNumber>0) {
  123. ampSecs = inputNumber*60.0;
  124. }
  125. softSerial.print("capacity(Ah):");
  126. softSerial.println(ampSecs/3600.0);
  127. }
  128. else if (cmd == 'h') {
  129. // print hall sensor analog reading
  130. softSerial.print("sensor:");
  131. softSerial.println(hallSense);
  132. }
  133. else if (cmd == 'l') {
  134. // print lid open/close state
  135. softSerial.print("lid:");
  136. softSerial.println(hallState);
  137. }
  138. else if (cmd == 't') {
  139. // set open/closed threshold
  140. if (inputNumber>0) {
  141. thresh = inputNumber;
  142. }
  143. softSerial.print("threshold:");
  144. softSerial.println(thresh);
  145. }
  146. else if (cmd == 'w') {
  147. // set open/closed threshold fuzz window
  148. if (inputNumber>0) {
  149. window = inputNumber;
  150. }
  151. softSerial.print("window:");
  152. softSerial.println(window);
  153. }
  154. else if (cmd == 'u') {
  155. // uptime of attiny in seconds
  156. softSerial.print("uptime(s):");
  157. softSerial.println(millis()/1000);
  158. }
  159. else if (cmd == 'e') {
  160. // toggle serial echo
  161. echo = inputNumber?1:0;
  162. softSerial.print("echo:");
  163. softSerial.println(echo);
  164. }
  165. else {
  166. softSerial.println("error:command");
  167. }
  168. state = ST_EXPECT_DIGIT_0;
  169. inputNumber = 0;
  170. } else {
  171. state = ST_SYNTAX_ERROR;
  172. }
  173. }
  174. }
  175. void handleBattery() {
  176. float raw_volts = (float)ina_read16(0x2);
  177. float raw_current = (float)ina_read16(0x1);
  178. volts = raw_volts * 0.00125;
  179. current = raw_current * 0.001;
  180. if (current>-0.02 && current<0.02) current = 0; // clamp to zero
  181. unsigned long thisTime = millis();
  182. if (lastTime>0 && thisTime>lastTime) {
  183. unsigned long millisPassed = thisTime - lastTime;
  184. if (millisPassed >= 1000) {
  185. lastTime = thisTime;
  186. // decrease estimated battery capacity
  187. ampSecs -= current*(millisPassed/1000);
  188. }
  189. } else {
  190. // timer uninitialized or timer wrap
  191. lastTime = thisTime;
  192. }
  193. }
  194. void loop() {
  195. handleBattery();
  196. handleLidSensor();
  197. if (softSerial.available() > 0) {
  198. handleCommands();
  199. }
  200. }
  201. void setup() {
  202. softSerial.begin(2400);
  203. softSerial.println("reform:attiny:0.4.0:boot");
  204. if (!i2c_init()) {
  205. softSerial.println("error:i2c");
  206. }
  207. // physical pin 8 (J34 pin 3) for hall effect sensor
  208. pinMode(HALL_SENSOR_PIN, INPUT);
  209. // physical pin 7 (J34 pin 5) for hall effect sensor supply voltage
  210. pinMode(HALL_SENSOR_SUPPLY_PIN, OUTPUT);
  211. digitalWrite(HALL_SENSOR_SUPPLY_PIN, HIGH);
  212. // PWRON output (TODO: ULVO)
  213. pinMode(7, OUTPUT);
  214. digitalWrite(7, HIGH);
  215. }