System.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. class System{
  3. public static function getInfos(){
  4. $return = self::commandSilent('cat /proc/cpuinfo');
  5. $lines = preg_split("/\\r\\n|\\r|\\n/", $return);
  6. $infos = array();
  7. foreach($lines as $line){
  8. if(strpos($line,':') === false ) continue;
  9. list($key,$value) = explode(':',$line);
  10. $infos[trim($key)] = trim($value);
  11. }
  12. return $infos;
  13. }
  14. public static function getPinForModel($model,$version){
  15. $model = $model.$version;
  16. //@TODO re-check pins mapping for revisions / types (it's fucking nightmare)
  17. //remi : It would be easier to use the output of gpio readall
  18. $pins = array();
  19. // rev 1.0 type B
  20. $pins['b1.0'] = array(
  21. array(
  22. //name,function,wiringPiNumber,bcmNumber,physicalNumber
  23. new Gpio('3.3V','Alimentation',-1,-1,1),
  24. new Gpio('SDA0','I2C',8,0,3),
  25. new Gpio('SCL0','I2C',9,1,5),
  26. new Gpio('GPIO 7','',7,4,7),
  27. new Gpio('DNC','Masse (GND)',-1,-1,9),
  28. new Gpio('GPIO 0','',0,17,11),
  29. new Gpio('GPIO 2','',2,21,13),
  30. new Gpio('GPIO 3','',3,22,15),
  31. new Gpio('DNC','Masse (GND)',-1,-1,17),
  32. new Gpio('MOSI','SPI',12,10,19),
  33. new Gpio('MISO','SPI',13,9,21),
  34. new Gpio('SCLK','SPI',14,11,23),
  35. new Gpio('DNC','',-1,-1,25),
  36. ),
  37. array(
  38. new Gpio('5V','Alimentation',-1,-1,2),
  39. new Gpio('DNC','Masse (GND)',-1,-1,4),
  40. new Gpio('0V','Masse (GND)',-1,-1,6),
  41. new Gpio('TxD','UART (Transmission)',14,14,8),
  42. new Gpio('RxD','UART (Réception)',15,15,10),
  43. new Gpio('GPIO 1','',1,18,12),
  44. new Gpio('DNC','Masse (GND)',-1,-1,14),
  45. new Gpio('GPIO 4','',4,23,16),
  46. new Gpio('GPIO 5','',5,24,18),
  47. new Gpio('DNC','Masse (GND)',-1,-1,20),
  48. new Gpio('GPIO 6','',6,25,22),
  49. new Gpio('CE 0','SPI',10,8,24),
  50. new Gpio('CE 1','SPI',11,7,26),
  51. )
  52. );
  53. $pins['a1.0'] = $pins['b1.0'];
  54. // rev 2.0 type B
  55. $pins['b2.0'] = array(
  56. array(
  57. //name,function,wiringPiNumber,bcmNumber,physicalNumber
  58. new Gpio('3.3V','Alimentation',-1,-1,1),
  59. new Gpio('SDA0','I2C',8,2,3),
  60. new Gpio('SCL0','I2C',9,3,5),
  61. new Gpio('GPIO 7','',7,4,7),
  62. new Gpio('DNC','Masse (GND)',-1,-1,9),
  63. new Gpio('GPIO 0','',0,17,11),
  64. new Gpio('GPIO 2','',2,27,13),
  65. new Gpio('GPIO 3','',3,22,15),
  66. new Gpio('DNC','Masse (GND)',-1,-1,17),
  67. new Gpio('MOSI','SPI',12,10,19),
  68. new Gpio('MISO','SPI',13,9,21),
  69. new Gpio('SCLK','SPI',14,11,23),
  70. new Gpio('DNC','',-1,-1,25),
  71. ),
  72. array(
  73. new Gpio('5V','Alimentation',-1,-1,2),
  74. new Gpio('DNC','Masse (GND)',-1,-1,4),
  75. new Gpio('0V','Masse (GND)',-1,-1,6),
  76. new Gpio('TxD','UART (Transmission)',14,14,8),
  77. new Gpio('RxD','UART (Réception)',15,15,10),
  78. new Gpio('GPIO 1','',1,18,12),
  79. new Gpio('DNC','Masse (GND)',-1,-1,14),
  80. new Gpio('GPIO 4','',4,23,16),
  81. new Gpio('GPIO 5','',5,24,18),
  82. new Gpio('DNC','Masse (GND)',-1,-1,20),
  83. new Gpio('GPIO 6','',6,25,22),
  84. new Gpio('CE 0','SPI',10,8,24),
  85. new Gpio('CE 1','SPI',11,7,26),
  86. )
  87. );
  88. $pins['a2.0'] = $pins['b2.0'];
  89. // type B+
  90. // @maditnerd I fix the gpio numbering , some were wrong.
  91. $pins['b+1.0'] = array(
  92. array(
  93. //name,function,wiringPiNumber,bcmNumber,physicalNumber
  94. new Gpio('3.3V','Alimentation',-1,-1,1),
  95. new Gpio('SDA1','I2C',8,2,3),
  96. new Gpio('SCL1','I2C',9,3,5),
  97. new Gpio('GPIO 7','',7,4,7),
  98. new Gpio('DNC','Masse (GND)',-1,-1,9),
  99. new Gpio('GPIO 0','',0,17,11),
  100. new Gpio('GPIO 2','',2,27,13),
  101. new Gpio('GPIO 3','',3,22,15),
  102. new Gpio('3.3V','Alimentation',-1,-1,1),
  103. new Gpio('MOSI','SPI',12,10,19),
  104. new Gpio('MISO','SPI',13,9,21),
  105. new Gpio('SCLK','SPI',14,11,23),
  106. new Gpio('DNC','Masse (GND)',-1,-1,25),
  107. new Gpio('SDA0','I2C',30,0,27),
  108. new Gpio('GPIO 21','',21,5,29),
  109. new Gpio('GPIO 22','',22,6,31),
  110. new Gpio('GPIO 23','',23,13,33),
  111. new Gpio('GPIO 24','',24,19,35),
  112. new Gpio('GPIO 25','',25,26,37),
  113. new Gpio('0V','Masse (GND)',-1,-1,39),
  114. ),
  115. array(
  116. new Gpio('5V','Alimentation',-1,-1,2),
  117. new Gpio('5V','Alimentation',-1,-1,4),
  118. new Gpio('0V','Masse',-1,-1,6),
  119. new Gpio('TxD','UART (Transmission)',15,15,8),
  120. new Gpio('RxD','UART (Réception)',16,16,10),
  121. new Gpio('GPIO 1','',1,18,12),
  122. new Gpio('0V','Masse (GND)',-1,-1,14),
  123. new Gpio('GPIO 4','',4,23,16),
  124. new Gpio('GPIO 5','',5,24,18),
  125. new Gpio('0V','Masse (GND)',-1,-1,20),
  126. new Gpio('GPIO 6','',6,25,22),
  127. new Gpio('CE 0','SPI',10,8,24),
  128. new Gpio('CE 1','SPI',11,7,26),
  129. new Gpio('SCL 0','I2C ID EEPROM',-1,-1,28),
  130. new Gpio('0V','Masse (GND)',-1,-1,30),
  131. new Gpio('GPIO 26','PWM0',26,12,32),
  132. new Gpio('0V','Masse (GND)',-1,-1,34),
  133. new Gpio('GPIO 27','',27,16,36),
  134. new Gpio('GPIO 28','',28,20,38),
  135. new Gpio('GPIO 29','',29,21,40),
  136. )
  137. );
  138. //type B2
  139. $pins['b21.0'] = $pins['b+1.0'];
  140. //A3
  141. $pins['a3.0'] = $pins['b21.0'];
  142. //Zero
  143. $pins['zero1.0'] = $pins['a3.0'];
  144. // Banana PI M1
  145. $pins['M11.0'] = array(
  146. array(
  147. new Gpio('3.3V','Alimentation',-1,-1,1),
  148. new Gpio('SDA0','I2C',8,0,3),
  149. new Gpio('SCL0','I2C',9,1,5),
  150. new Gpio('GPIO 7','',7,4,7),
  151. new Gpio('0V','Masse (GND)',-1,-1,9),
  152. new Gpio('GPIO 0','',0,17,11),
  153. new Gpio('GPIO 2','',2,21,13),
  154. new Gpio('GPIO 3','',3,22,15),
  155. new Gpio('3.3V','Alimentation',-1,-1,17),
  156. new Gpio('MOSI','SPI',12,10,19),
  157. new Gpio('MISO','SPI',13,9,21),
  158. new Gpio('SCLK','SPI',14,11,23),
  159. new Gpio('0V','Masse (GND)',-1,-1,25),
  160. new Gpio('',' ',-1,-1,-1),
  161. new Gpio('5V','Alimentation',-1,-1,1),
  162. new Gpio('GPIO 8','',17,28,3),
  163. new Gpio('GPIO 10','',19,30,5),
  164. new Gpio('0V','Masse (GND)',-1,-1,7),
  165. ),
  166. array(
  167. new Gpio('5V','Alimentation',-1,-1,2),
  168. new Gpio('5V','Alimentation',-1,-1,4),
  169. new Gpio('0V','Masse (GND)',-1,-1,6),
  170. new Gpio('TxD','UART (Transmission)',14,14,8),
  171. new Gpio('RxD','UART (Réception)',15,15,10),
  172. new Gpio('GPIO 1','',1,18,12),
  173. new Gpio('0V','Masse (GND)',-1,-1,14),
  174. new Gpio('GPIO 4','',4,23,16),
  175. new Gpio('GPIO 5','',5,24,18),
  176. new Gpio('0V','Masse (GND)',-1,-1,20),
  177. new Gpio('GPIO 6','',6,25,22),
  178. new Gpio('CE 0','SPI',10,8,24),
  179. new Gpio('CE 1','SPI',11,7,26),
  180. new Gpio('',' ',-1,-1,-1),
  181. new Gpio('3.3V','Alimentation',-1,-1,2),
  182. new Gpio('Gpio 9 Tx','UART (Transmission)',18,29,4),
  183. new Gpio('Gpio 11 Rx','UART (Réception)',20,31,6),
  184. new Gpio('0V','Masse (GND)',-1,-1,8),
  185. )
  186. );
  187. return isset($pins[$model])?$pins[$model]:$pins['b1.0'];
  188. }
  189. public static function getModel(){
  190. $infos = self::getInfos();
  191. $deductionArray = array(
  192. '0002' => array('ram'=>'256','version'=>'1.0','type'=>'b','revision'=>'0002'),
  193. '0003' => array('ram'=>'256','version'=>'1.0+ecn0001','type'=>'b','revision'=>'0003'),
  194. '0004' => array('ram'=>'256','version'=>'2.0','type'=>'b','revision'=>'0004'),
  195. '0005' => array('ram'=>'256','version'=>'2.0','type'=>'b','revision'=>'0005'),
  196. '0006' => array('ram'=>'256','version'=>'2.0','type'=>'b','revision'=>'0006'),
  197. '0007' => array('ram'=>'256','version'=>'1.0','type'=>'a','revision'=>'0007'),
  198. '0008' => array('ram'=>'256','version'=>'1.0','type'=>'a','revision'=>'0008'),
  199. '0009' => array('ram'=>'256','version'=>'1.0','type'=>'a','revision'=>'0009'),
  200. '0010' => array('ram'=>'512','version'=>'1.0','type'=>'b+','revision'=>'0010'),
  201. '0011' => array('ram'=>'512','version'=>'1.0','type'=>'compute','revision'=>'0011'),
  202. '0012' => array('ram'=>'256','version'=>'1.0','type'=>'a+','revision'=>'0012'),
  203. '0013' => array('ram'=>'512','version'=>'1.0','type'=>'b+','revision'=>'0013'),
  204. '000d' => array('ram'=>'512','version'=>'2.0','type'=>'b','revision'=>'000d'),
  205. '000e' => array('ram'=>'512','version'=>'2.0','type'=>'b','revision'=>'000e'),
  206. '000f' => array('ram'=>'512','version'=>'2.0','type'=>'b','revision'=>'000f'),
  207. '000f' => array('ram'=>'512','version'=>'2.0','type'=>'b','revision'=>'000f'),
  208. '900032' => array('ram'=>'512','version'=>'2.0','type'=>'b','revision'=>'900032'),
  209. 'a01041' => array('ram'=>'1024','version'=>'1.0','type'=>'b2','revision'=>'a01041'),
  210. '1a01041' => array('ram'=>'1024','version'=>'1.0','type'=>'b2','revision'=>'1a01041'),
  211. 'a21041' => array('ram'=>'1024','version'=>'1.0','type'=>'b2','revision'=>'a21041'),
  212. '2a01041' => array('ram'=>'1024','version'=>'1.0','type'=>'b2','revision'=>'2a01041'),
  213. 'a02082' => array('ram'=>'1024','version'=>'3.0','type'=>'a','revision'=>'a02082'),
  214. 'a22082' => array('ram'=>'1024','version'=>'3.0','type'=>'a','revision'=>'a22082'),
  215. '900092' => array('ram'=>'512','version'=>'1.0','type'=>'zero','revision'=>'900092'),
  216. '9000c1' => array('ram'=>'512','version'=>'1.0','type'=>'zero','revision'=>'9000c1'),
  217. '0000' => array('ram'=>'1024','version'=>'1.0','type'=>'M1','revision'=>'0000')
  218. );
  219. if(PHP_OS=='WINNT') $infos['Revision'] = 'a01041';//for dev mode on windows only
  220. return isset($deductionArray[$infos['Revision']]) ? $deductionArray[$infos['Revision']] :array('ram'=>'0','version'=>'0','type'=>'unknown','revision'=>$infos['Revision']);
  221. }
  222. public static function commandSilent($cmd){
  223. Functions::log('Launch system command (without output): '.$cmd);
  224. return shell_exec($cmd);
  225. }
  226. public static function command($cmd){
  227. Functions::log('Launch system command : '.$cmd);
  228. return system($cmd);
  229. }
  230. public static function gpio() {
  231. $model = self::getModel();
  232. $pinsRange = self::getPinForModel($model['type'],$model['version']);
  233. $gpios = array();
  234. foreach($pinsRange as $range){
  235. foreach($range as $pin){
  236. if(PHP_OS=='WINNT'){
  237. $gpios[$pin->wiringPiNumber] = rand(0,1);
  238. continue;
  239. }
  240. if($pin->wiringPiNumber<0) continue;
  241. $gpios[$pin->wiringPiNumber] = exec(GPIO::GPIO_DEFAULT_PATH." read ".$pin->wiringPiNumber, $out);
  242. }
  243. }
  244. return $gpios;
  245. }
  246. }
  247. ?>